【问题标题】:SpringBoot simple multipart file upload with Advanced rest client (Chrome)Spring Boot 使用 Advanced rest 客户端(Chrome)进行简单的多部分文件上传
【发布时间】:2017-08-21 00:58:04
【问题描述】:

我想上传一张图片到文件系统。所以我使用 Spring Boot 的多部分文件上传。 而且我正在使用 Advance Rest Client(Chrome) 工具来POST 多部分文件。但即使我没有指定任何内容类型,我也面临错误org.apache.tomcat.util.http.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界。

这是我的休息控制器代码,

@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.";
    }
  }
}

截图(Advance rest客户端工具)

错误

{ "timestamp": 1490678908517, "status": 500, "error": "Internal Server Error", "exception": "org.springframework.web.multipart.MultipartException", "message": "Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found", "path": "/upload" }

【问题讨论】:

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


    【解决方案1】:

    问题出在您来自高级休息客户的请求中。它在邮递员中工作正常。图像正在上传。用邮递员试试就知道了。

    【讨论】:

    • 是的,兄弟它的工作。感谢您的友好回复。但是我可以知道为什么它不能提前休息客户端吗??
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 1970-01-01
      • 2018-05-12
      • 2014-10-31
      • 2017-12-21
      • 2014-05-01
      • 2017-09-26
      相关资源
      最近更新 更多