打包jar文件发布程序以后中文乱码

Blade 未结 3 1574
kane
kane 剑圣 2021-01-13 11:08
悬赏:10

一、该问题的重现步骤是什么?

1. boot程序打包jar文件,java -jar .\SpringBlade.jar

2. 接收数据内容

{

           "geoip" => {

        "coordinates" => [

            [0] "%{[geoip][longitude]}",

            [1] "%{[geoip][latitude]}"

        ]

    },

            "path" => "C:/Users/kane/Desktop/test.txt",

            "host" => "kane",

        "username" => "用户测试",

         "dstport" => "8080",

         "message" => "<7>[access resource][TCP]用户测试 from IP 16.52.161.178: access 1.18.39.76:8080 success!\r",

            "type" => "txt",

      "@timestamp" => 2021-01-13T02:55:57.829Z,

    "operatortype" => "TCP",

     "loginregion" => {

              "latitude" => 37.751,

          "country_name" => "United States",

         "country_code3" => "US",

        "continent_code" => "NA",

             "longitude" => -97.822,

                    "ip" => "16.52.161.178",

         "country_code2" => "US",

              "location" => {

            "lon" => -97.822,

            "lat" => 37.751

        },

              "timezone" => "America/Chicago"

    },

     "messagetype" => "7",

           "dstip" => "1.18.39.76",

             "res" => "success!\r",

        "@version" => "1",

           "srcip" => "16.52.161.178",

        "protocol" => "access resource"

}

3.接口接收内容

@RestController
@AllArgsConstructor
@RequestMapping("app")
/**
* logstash同步数据接口
*/
@PostMapping("/SyncData")
@ApiOperationSupport(order = 1)
@ResponseBody
public void SyncData(@RequestBody String jsonString) {
  try {
     if (jsonString == "" || jsonString == null) {
        return;
     }

     JSONObject jsonObject = JSON.parseObject(jsonString);


4、使用idea启动调试正常,


5、pom中build代码如下

<build>
   <finalName>SpringBlade</finalName>
   <resources>
       <resource>
           <directory>src/main/resources</directory>
       </resource>
       <resource>
           <directory>src/main/java</directory>
           <includes>
               <include>**/*.xml</include>
           </includes>
       </resource>
   </resources>
   <pluginManagement>
       <plugins>
           <plugin>
               <groupId>org.springframework.boot</groupId>
               <artifactId>spring-boot-maven-plugin</artifactId>
               <version>${spring.boot.version}</version>
               <configuration>
                   <finalName>${project.build.finalName}</finalName>
               </configuration>
               <executions>
                   <execution>
                       <goals>
                           <goal>repackage</goal>
                       </goals>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>com.spotify</groupId>
               <artifactId>docker-maven-plugin</artifactId>
               <version>${docker.plugin.version}</version>
               <configuration>
                   <imageName>${docker.registry.url}/blade/${project.artifactId}:${project.version}</imageName>
                   <dockerDirectory>${pom.basedir}</dockerDirectory>
                   <dockerHost>${docker.registry.host}</dockerHost>
                   <resources>
                       <resource>
                           <targetPath>/</targetPath>
                           <directory>${project.build.directory}</directory>
                           <include>${project.build.finalName}.jar</include>
                       </resource>
                   </resources>
                   <registryUrl>${docker.registry.url}</registryUrl>
                   <serverId>${docker.registry.url}</serverId>
                   <pushImage>true</pushImage>
               </configuration>
           </plugin>
       </plugins>
   </pluginManagement>
   <plugins>
       <plugin>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-maven-plugin</artifactId>
           <configuration>
               <executable>true</executable>
               <includeSystemScope>true</includeSystemScope>
           </configuration>
       </plugin>
       <plugin>
           <artifactId>maven-compiler-plugin</artifactId>
           <configuration>
               <source>${java.version}</source>
               <target>${java.version}</target>
               <encoding>UTF-8</encoding>
               <compilerArgs>
                   <arg>-parameters</arg>
               </compilerArgs>
           </configuration>
       </plugin>
   </plugins>
</build>

二、你期待的结果是什么?实际看到的又是什么?

jsonobject 对象中 username 期待为中文

实际看到jasonobject为乱码

image.png

idea运行,调试正常


2021-01-13 10:55:58.014 DEBUG 62684 --- [  XNIO-1 task-3] o.s.baselog.mapper.TcpMapper.selectOne   : ==>  Preparing: SELECT id, user_name, dst_ip, src_ip, count, data, count_time, category, content FROM vpn_tcp WHERE user_name = ? AND dst_ip = ? AND src_ip = ? AND data = ?

2021-01-13 10:55:58.020 DEBUG 62684 --- [  XNIO-1 task-3] o.s.baselog.mapper.TcpMapper.selectOne   : ==> Parameters: 用户测试(String), 1.18.39.76(String), 16.52.161.178(String), 2021-01-13(Date)

三、你正在使用的是什么产品,什么版本?在什么操作系统上?

boot版本,在linux和windows都进行过测试,问题相同


四、请提供详细的错误堆栈信息,这很重要。

无堆栈错误信息。yml文件


#服务器配置
server:
 port: 80
 undertow:
   # 设置IO线程数, 它主要执行非阻塞的任务,它们会负责多个连接, 默认设置每个CPU核心一个线程
   io-threads: 16
   # 阻塞任务线程池, 当执行类似servlet请求阻塞操作, undertow会从这个线程池中取得线程,它的值设置取决于系统的负载
   worker-threads: 400
   # 以下的配置会影响buffer,这些buffer会用于服务器连接的IO操作,有点类似netty的池化内存管理
   buffer-size: 1024
   # 是否分配的直接内存
   direct-buffers: true
   servlet:
     encoding:
       charset: UTF-8
       force: true

#spring配置
spring:
 cache:
   ehcache:
     config: classpath:config/ehcache.xml
 http:
   encoding:
     charset: UTF-8
     force: true
 servlet:
   multipart:
     max-file-size: 256MB
     max-request-size: 1024MB
 mvc:
   throw-exception-if-no-handler-found: true
 resources:
   add-mappings: false
 datasource:
   driver-class-name: com.mysql.cj.jdbc.Driver



五、若有更多详细信息,请在下面提供。

3条回答
  • 2021-01-13 20:05

    此问题,已解决,在windows下运行jar文件需要 保证windows需要是中文环境

    1、运行第一步  chcp 65001

    2、然后在运行java -jar SpringBlade.jar -Dfile.encoding=utf-8

    linux下直接运行

    可直接运行java -jar SpringBlade.jar -Dfile.encoding=utf-8

    0 讨论(0)
  • 2021-01-13 20:44

    感谢投稿

    0 讨论(0)
  • 2021-01-19 14:04

    为什么我 试过了 这种方式依旧还是不行,有没有其他有效的方式呢

    0 讨论(0)
提交回复