我正在将文件(任何格式的 doc、docx、pdf、文本等)作为多部分表单/数据从 Postman 或应用程序 UI 上传到 REST API。文本文件上传正常。所有其他非文本格式都会损坏。我打不开那些文件。
上传文件的大小急剧增加。检查以下服务器日志:
File size just before call to request.getRequestDispatcher().forward(): 27583
Controller, first line in method:39439
Run Code Online (Sandbox Code Playgroud)
上传文件大小为27.3Kb
我猜这些文件因为附加到文件中的其他数据而损坏。
控制器方法是
@RequestMapping(value="/entity/{entity}/{entityId}/type/{type}/{derive}",method = RequestMethod.POST)
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile multipartFile,@PathVariable("entity")String entity,@PathVariable("type")String type,@PathVariable("entityId")Long entityId,@PathVariable("derive") boolean derive) throws Exception
Run Code Online (Sandbox Code Playgroud)
由于文本文件正确保存并且其他文件也被正确写入,不要认为写入文件的代码不正确。
获取 inputStream 的代码
public String storeFile(MultipartFile multipartFile, String entity, Long id, String uploadType, boolean isDerive,String customName)
throws Exception
{
try
{
System.out.println(multipartFile.getSize());
String fileName = "";
String contentType = "";
if (multipartFile != null)
{
fileName = multipartFile.getOriginalFilename(); …Run Code Online (Sandbox Code Playgroud)