最近在使用SpringCloud搭建微服務的過程當中,發現上傳文件通過Zuul網關轉發的時候,迴應爲上傳文件名中文亂碼致使文件的寫操做失敗,從而致使文件上傳失敗,可是不通過Zuul轉發的時候,文件上傳正常,所以猜想是Zuul對上傳的請求的編碼進行了處理。最終在網上找到了兩種解決方案:git
方案1、在上傳文件的請求路徑以前添加字符串「zuul」聲明此請求的編碼不作處理。github
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* 各類poi導入導出的請求控制模塊;
*
* @author:JunZhou
* @Company:LongZheng
* @Email:1769676159@qq.com
* @2018年1月16日@下午4:28:49
*/
@RestController
@RequestMapping("/poi")
@ClazzNote(desc = "excel文件", resource = "input", modName = "poi")
public class POIController {
@RequestMapping("/uploadExcel")
@ServiceNote(desc = "上傳考勤明細的excel", auth = ServiceNote.AUTH.CHECK)
public ResObject<ResponseMessage> singleFileUpload(MultipartHttpServletRequest request,
RedirectAttributes redirectAttributes, HttpServletResponse response) {
Integer stateCode = poiService.receiveUploadExcel(request, redirectAttributes, response);
//根據返回值肯定響應信息實體;
ResponseMessage responseMessage = UploadStatusEnum.desicideUploadStatusBystateCode(stateCode, response);
//設置響應狀態爲200;
response.setStatus(UploadStatusEnum.UPLOAD_OK.getStateCode());
return new ResObject<ResponseMessage>(null,responseMessage);
}
|
例如在當前代碼下,請求路徑是:spring
localhost:9090/oss/poi/uploadExcel後端
那麼採用方案一解決中文亂碼的請求路徑就是:app
localhost:9090/zuul/oss/poi/uploadExcelide
通過測試,中文亂碼問題成功解決。微服務
方案二:
在方案一的基礎上,不只要改動後端代碼,前端代碼也要變動,較爲麻煩,
所以更簡單的方案是在zuul的配置文件中添加一個以下的屬性:測試
1
|
zuul.servlet-path=/
|
注意實在網關的配置文件中添加的,通過本人側ishi,問題也成功解決了。編碼
參考文章:
一、SpringCloud 上傳文件,通過Zuul,中文文件名亂碼解決辦法
二、zuul proxy file upload, file name is Chinese garbled #1385