zuul上傳文件,提示Content-length different from byte array length! cl=252481044, array=0
首先, 來說說什麼是Content-Length
,在http
的協議中Content-Length
首部告訴瀏覽器報文中實體主體的大小。這個大小是包含了內容編碼的,比如對文件進行了gzip
壓縮,Content-Length
就是壓縮後的大小(這點對我們編寫服務器非常重要)。除非使用了分塊編碼,否則Content-Length
首部就是帶有實體主體的報文必須使用的。使用Content-Length
首部是爲了能夠檢測出服務器崩潰而導致的報文截尾,並對共享持久連接的多個報文進行正確分段.
用postman請求zuul upload接口
我們發現,http 請求狀態是200,說明請求成功,但是返回頁面是空白頁,我代碼裏上傳成功返回的是文件地址。
文件上傳,經過zuul路由轉發之後,丟失了contentLength,後來發現是zuul對文件上傳路徑進行轉發導致的
zuul: routes: localhost-zuul: path: /** url: forward:/zuul
package com.etc.zuul.controller; import org.springframework.stereotype.Controller; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; @Controller public class FileUploadController { /** * 文件上傳類 * @param file * @return * @throws IOException */ @RequestMapping(value = "/upload",method = RequestMethod.POST) public @ResponseBody String handleFileUpload(@RequestParam(value = "file",required = true)MultipartFile file) throws IOException { //獲取文件字節流 byte[] bytes = file.getBytes(); File saveFile = new File(file.getOriginalFilename()); FileCopyUtils.copy(bytes,saveFile); return saveFile.getAbsolutePath(); } @GetMapping("/test") public String index(){ return "test zuul"; } }
把路由配置 localhost-zuul去掉,就不會轉發了,也就上傳成功了