springboot程序间传递数据乱码

使用两个springboot程序运行程序,发现向程序A发送请求,A接收到之后转发给B,再使用从B中获取的数据返回给用户,中文数据会出现乱码。

  • 直接从A或B中获取数据不会有问题
  • 在intellij idea中运行不会有问题,编译后会有问题
  • 使用maven编译为jar包
  • 在pom文件及properties中均设置了一些utf-8信息
  • jdk为zulu8 openjdk
  • windows 1903 64 pro 中文版

一下是相关设置(两个项目设置基本一致):
pom.xml:

...
<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
</properties>
...
<groupId>org.apache.maven.plugins</groupId>  
<artifactId>maven-compiler-plugin</artifactId>  
<configuration>  
    <source>1.8</source>  
    <target>1.8</target>  
    <encoding>UTF-8</encoding>  
</configuration>
...
<groupId>org.springframework.boot</groupId>  
<artifactId>spring-boot-maven-plugin</artifactId>  
<configuration>  
    <fork>true</fork>  
    <mainClass>com.prd.approval.ApprovalApplication</mainClass> 
    <jvmArguments>\-Dfile.encoding=UTF-8</jvmArguments>  
    <layout>ZIP</layout>   
</configuration>
...

application.yml:

server:  
  port: 4001  
  
  http:  
    encoding:  
      charset: UTF-8  
      force: true  
      enabled: true  
  
  tomcat:  
    uri-encoding: UTF-8
...

转发部分1:

@PostMapping("/doVisit")  
public Object doVisit(@RequestBody Map<String, String> map) {  
    System.out.println("map:  "+map);  
  
  String method = map.get("method");  
  String url = map.get("url");  
 if(method == null||url==null||"".equals(method)||"".equals(url)){  
        return new ResponseUtil<>(0, "参数缺少内容");  
  }  
    map.remove(method);  
  map.remove(url);  
 if("post".equals(method)){  
        return HttpRequest.sendPost(url,JSONObject.toJSONString(map));  
  }else{  
       return HttpRequest.sendGet(url);  
  }  
}

转发部分2(仅post部分):

public static String sendPost(String url, String param) {  
    PrintWriter out = null;  
  BufferedReader in = null;  
  String result = "";  
 try {  
        URL realUrl = new URL(url);  
  // 打开和URL之间的连接  
  URLConnection conn = realUrl.openConnection();  
  // 设置通用的请求属性  
  conn.setRequestProperty("accept", "\*/\*");  
  conn.setRequestProperty("connection", "Keep-Alive");  
  conn.setRequestProperty("user-agent",  
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");  
  conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");  
  // 发送POST请求必须设置如下两行  
  conn.setDoOutput(true);  
  conn.setDoInput(true);  
  // 获取URLConnection对象对应的输出流  
  out = new PrintWriter(conn.getOutputStream());  
  // 发送请求参数  
  out.print(param);  
  // flush输出流的缓冲  
  out.flush();  
  // 定义BufferedReader输入流来读取URL的响应  
  in = new BufferedReader(  
                new InputStreamReader(conn.getInputStream()));  
  String line;  
 while ((line = in.readLine()) != null) {  
            result += line;  
  }  
    } catch (Exception e) {  
        System.out.println("发送 POST 请求出现异常!"+e);  
  e.printStackTrace();  
  }  
    //使用finally块来关闭输出流、输入流  
  finally{  
        try{  
            if(out!=null){  
                out.close();  
  }  
            if(in!=null){  
                in.close();  
  }  
        }  
        catch(IOException ex){  
            ex.printStackTrace();  
  }  
    }  
    return result;  
}
阅读 3.7k
3 个回答

应用运行环境是服务器么?


显示指定编码试试

BufferedReader input = new BufferedReader(
            new InputStreamReader(urlConn.getInputStream(), "UTF-8")); 

不知道你是不是直接 java -jar xx.jar 启动的, 你可以尝试一下 java -Dfile.encoding=UTF-8 -jar xx.jar

默认会从服务器环境变量读取字符集,建议你把这个要设置一下,不然类似的问题不可能每次都在代码里面手动设置字符集

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题