java http post 上传json 数据,utf8编码的中文 保存到数据库后都变成/uxxxx那种unicode格式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java http post 上传json 数据,utf8编码的中文 保存到数据库后都变成/uxxxx那种unicode格式相关的知识,希望对你有一定的参考价值。
①console的内容
ENTRY_CREATE -> 的发生.txt
旧文件null
新文件26f57ad44539a704d03e8bc71b1a5026
======>读取[的发生.txt]开始
param:param=的发生.txt涩谷涩谷
事后:
Array
(
[act] => invoice_notify
[param] => 的发生.txt涩谷涩谷
)
"state":1,"msg":"success"
数据已正确上传
======>读取[的发生.txt]完成
②数据库里的内容
"act":"invoice_notify","param":"[2017-09-26 16:58:33] u5355u636eu53f7uff1a30E017090227u5f00u5177u7ed3u679cuff1a0uff0cu5f00u5177u5931u8d25u539fu56e0uff1au7f16u7801u8868u7248u672cu53f7u540cu5f53u524du7248u672cu4e0du4e00u81f4"
是什么问题导致的呢,怎么解决?
java模拟post请求发送json数据
import com.alibaba.fastjson.JSONObject; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; public class HttpRequest2 { public static String sendPost(String url,String param){ OutputStreamWriter out =null; BufferedReader reader = null; String response = ""; //创建连接 try { URL httpUrl = null; //HTTP URL类 用这个类来创建连接 //创建URL httpUrl = new URL(url); //建立连接 HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("connection", "keep-alive"); conn.setUseCaches(false);//设置不要缓存 conn.setInstanceFollowRedirects(true); conn.setDoOutput(true); conn.setDoInput(true); conn.connect(); //POST请求 out = new OutputStreamWriter( conn.getOutputStream()); out.write(param); out.flush(); //读取响应 reader = new BufferedReader(new InputStreamReader( conn.getInputStream())); String lines; while ((lines = reader.readLine()) != null) { lines = new String(lines.getBytes(), "utf-8"); response+=lines; } reader.close(); // 断开连接 conn.disconnect(); } catch (Exception e) { System.out.println("发送 POST 请求出现异常!"+e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(reader!=null){ reader.close(); } } catch(IOException ex){ ex.printStackTrace(); } } return response; } public static String sendPost2(String url, String data) { String response = null; try { CloseableHttpClient httpclient = null; CloseableHttpResponse httpresponse = null; try { httpclient = HttpClients.createDefault(); HttpPost httppost = new HttpPost(url); StringEntity stringentity = new StringEntity(data, ContentType.create("text/json", "UTF-8")); httppost.setEntity(stringentity); httpresponse = httpclient.execute(httppost); response = EntityUtils .toString(httpresponse.getEntity()); } finally { if (httpclient != null) { httpclient.close(); } if (httpresponse != null) { httpresponse.close(); } } } catch (Exception e) { e.printStackTrace(); } return response; } public static void main(String[] args) { JSONObject jsonParam = new JSONObject(); jsonParam.put("id", "12306"); jsonParam.put("name", "zhangsan"); String param = jsonParam.toJSONString(); String url="http://localhost:8080/demo/one"; String sendPost = sendPost2(url, param); System.out.println(sendPost); } }
下面是后台的代码
@RestController @RequestMapping("/demo") public class PostController { @Resource protected HttpServletRequest request; @RequestMapping(value = "/one",method = RequestMethod.POST) public String getResult(String str)throws Exception{ InputStreamReader reader = new InputStreamReader(request.getInputStream(),"UTF-8"); char[] buff = new char[1024]; int length =0; while((length =reader.read(buff))!=-1){ String message = new String(buff,0,length); System.out.println("接收到的信息 "+ message); } return JSON.toJSONString("这是post请求"); } }
以上是关于java http post 上传json 数据,utf8编码的中文 保存到数据库后都变成/uxxxx那种unicode格式的主要内容,如果未能解决你的问题,请参考以下文章