首页 > 编程知识 正文

arraylist tostring,byte数组转multipartfile

时间:2023-05-03 06:46:48 阅读:250810 作者:2692

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;@Controllerpublic 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去掉,就不会转发了,也就上传成功了

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。