SpringBoot简单的多部分文件上传与高级休息客户端(Chrome)

时间:2017-03-28 05:33:11

标签: spring rest file-upload spring-boot multipartform-data

我想将图像上传到文件系统。所以我使用使用Spring boot启动多部分文件上传。我还使用Advance Rest Client(Chrome)工具来protected override CreateParams CreateParams { get { CreateParams param = base.CreateParams; param.ClassName = "tooltips_class32"; param.Style = unchecked(WindowMessages.WS_POPUP) | WindowMessages.TTS_ALWAYSTIP; param.ExStyle |= WindowMessages.WS_EX_TOPMOST; return param; } } 多部分文件。但我面临一个错误,即使我没有指定任何内容类型 org.apache.tomcat.util.http.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界。 < / strong>

  

这是我的其他控制器代码,

POST
  

屏幕截图(高级休息客户端工具)

enter image description here

  

错误

@RestController public class StringController { @RequestMapping(value="/upload", method=RequestMethod.POST) public @ResponseBody String singleSave(@RequestParam("file") MultipartFile file){ String fileName = null; if (!file.isEmpty()) { try { fileName = file.getOriginalFilename(); byte[] bytes = file.getBytes(); BufferedOutputStream buffStream = new BufferedOutputStream(new FileOutputStream(new File("F:/" + fileName))); buffStream.write(bytes); buffStream.close(); return "You have successfully uploaded " + fileName; } catch (Exception e) { return "You failed to upload " + fileName + ": " + e.getMessage(); } } else { return "Unable to upload. File is empty."; } } }

2 个答案:

答案 0 :(得分:2)

问题在于提前休息客户的请求。它在邮递员工作正常。图像上传。尝试邮递员,你会得到它。

答案 1 :(得分:0)

您在客户端请求标头值 boundary 中丢失了。 在PostMan标头“ Content-Type”中构造如下:

Content-Type : multipart/form-data;boundary="12312313132132"