解決springboot responseentity<string>亂碼問題
springboot responseentity<string>亂碼
亂碼
<200,{"result":"[{\"field\":\"name\",\"objectName\":\"driver\",\"defaultMessage\":\"å??ç§°ä¸?能为空ï¼?\"},{\"field\":\"address\",\"objectName\":\"driver\",\"defaultMessage\":\"系统idä¸?能为空ï¼?\"},{\"field\":\"authcode\",\"objectName\":\"driver\",\"defaultMessage\":\"认è¯?ç ?ä¸?能为空ï¼?\"}]"},{Connection=[keep-alive], Set-Cookie=[JSESSIONID=lUFZC2gIOg0eoUdfdmWW6KSYCP7aY8FErr6BBu9T; path=/], Content-Type=[text/plain], Content-Length=[302], X-Application-Context=[application:6060], Date=[Fri, 06 Aug 2021 01:11:25 GMT]}>
解決方法
try
{
String seqResult = new String(stringResponseEntity.getBody().getBytes("ISO8859-1"),"utf-8");
System.out.println(seqResult);//這就是UTF-8的啦
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}加 try catch是為了解決
Unhandled exception: java.io.UnsupportedEncodingException
解決亂碼后
{"result":"[{\"field\":\"name\",\"objectName\":\"driver\",\"defaultMessage\":\"名稱不能為空!\"},{\"field\":\"address\",\"objectName\":\"driver\",\"defaultMessage\":\"系統(tǒng)id不能為空!\"},{\"field\":\"authcode\",\"objectName\":\"driver\",\"defaultMessage\":\"認(rèn)證碼不能為空!\"}]"}
responseentity下載文件名中文亂碼
SpringBoot下載文件,文件名帶了中文出現(xiàn)亂碼:

代碼如下:
return ResponseEntity.ok()
.header("Content-disposition", "attachment;filename=" + fileName)
.contentLength(file.length())
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(resource);解決辦法
給文件名進(jìn)行編碼:
fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");問題解決!

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Springboot3?ResponseEntity?完全使用案例
- SpringBoot中ResponseEntity的使用方法舉例詳解
- 一文詳解Spring中ResponseEntity包裝器的使用
- SpringBoot的ResponseEntity類返回給前端具體講解
- SpringBoot ResponseEntity標(biāo)識(shí)Http響應(yīng)方式
- springmvc @ResponseStatus和ResponseEntity的使用
- SpringMVC使用ResponseEntity實(shí)現(xiàn)文件上傳下載
- 使用spring框架ResponseEntity實(shí)現(xiàn)文件下載
- Spring ResponseEntity的使用詳解
相關(guān)文章
Spring框架JavaMailSender發(fā)送郵件工具類詳解
這篇文章主要為大家詳細(xì)介紹了Spring框架JavaMailSender發(fā)送郵件工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
MyBatis-Plus多表聯(lián)合查詢并且分頁(3表聯(lián)合)
這篇文章主要介紹了MyBatis-Plus多表聯(lián)合查詢并且分頁(3表聯(lián)合),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java基礎(chǔ)之Comparable與Comparator概述
這篇文章主要介紹了Java基礎(chǔ)之Comparable與Comparator詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
mybatis/mybatis-plus模糊查詢語句特殊字符轉(zhuǎn)義攔截器的實(shí)現(xiàn)
在開發(fā)中,我們通常會(huì)遇到這樣的情況。用戶在錄入信息是錄入了‘%’,而在查詢時(shí)無法精確匹配‘%’。究其原因,‘%’是MySQL的關(guān)鍵字,如果我們想要精確匹配‘%’,那么需要對其進(jìn)行轉(zhuǎn)義,本文就詳細(xì)的介紹一下2021-11-11

