Spring-MVC:上传文件

Spring-MVC: Upload a file

我在使用 Spring-MVC 4.2.1 上传文件时遇到一些问题..

这是端点的代码:

  @CrossOrigin
  @ApiOperation(value = "Add an attachment to a video", notes = "Add an attachment to the video with a specific id.", response = String.class)
  @ApiResponses(value = { 
    @ApiResponse(code = 200, message = "Succesfully attached."),
    @ApiResponse(code = 403, message = "Request could not be authorized."),
    @ApiResponse(code = 500, message = "Internal Server Error."),
    @ApiResponse(code = 400, message = "Malformed request.") })
  @RequestMapping(value = "/attachment/add",
    produces = { "application/json" }, 
    consumes = { "multipart/form-data" },
    method = RequestMethod.POST)
  public ResponseEntity<String> addAttachmentVideo(@ApiParam(value = "ID of the document", required = true) @RequestParam(value = "docId", required = true) String docId,
 @ApiParam(value = "file detail") @RequestPart("file") MultipartFile file,
    @ApiParam(value = "Name of the attachment", required = true) @RequestParam(value = "attachmentName", required = true) String attachmentName)
      throws NotFoundException {

        VideoControllerMock dummyClass = new VideoControllerMock();

        ResponseEntity<String> dummyResponse = dummyClass.addAttachmentVideo(docId, file, attachmentName);

        return dummyResponse;
  }

我也给你函数addAttachmentVideo:

public ResponseEntity<String> addAttachmentVideo(String docId, MultipartFile file, String attachmentName) {

    String responseString = "{\"response\": \"File with name " + attachmentName + " has been attached to document " + docId + ".\", \"file\": {\"size\": " + file.getSize() + "}}";

    // Prepare the headers
    HttpHeaders headers = headersClass.getMultiPartHeaders();

    // Response to send back
    ResponseEntity<String> response = new ResponseEntity<String>(responseString, headers, HttpStatus.ACCEPTED);

    return response;
  }

最后是函数 getMultiPartHeaders:

  public HttpHeaders getMultiPartHeaders() {
    HttpHeaders headers = new HttpHeaders();
    //headers.add(CREDENTIALS_NAME, "true");
    //headers.add(ORIGIN_NAME, "http://localhost:8080");
    headers.add(METHODS_NAME, "GET, OPTIONS, POST, HEAD");
    headers.add(HEADERS_NAME, "Origin, X-Requested-With, Content-Type, Accept");
    headers.add(CONTENT_TYPE, "multipart/form-data");
    headers.add(MAX_AGE_NAME, "3600");

    return headers;
  }

遗憾的是,我无法更改端点的代码。它是使用 swagger-codegen 自动生成的。但我可以更改其他两个函数。

如果我使用它,我会得到这个错误:

[WARNING] 
org.springframework.web.util.NestedServletException: 
Request processing failed; 
nested exception is java.lang.IllegalArgumentException: 
Expected MultipartHttpServletRequest: 
is a MultipartResolver configured?

有谁知道如何解决这个问题??问题是我不知道如何配置这个 MultipartResolver..

提前致谢

像这样配置 CommonsMultipartResolver(在您的 mvcconfig 中):

@Bean
public CommonsMultipartResolver multipartResolver(){
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setDefaultEncoding("UTF-8");
    multipartResolver.setMaxUploadSize(-1);
    return multipartResolver;
}