Content-length different from byte array length! cl=252481044, array=0

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去掉,就不會轉發了,也就上傳成功了

發佈了238 篇原創文章 · 獲贊 30 · 訪問量 22萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章