Spring-MVC:上传文件

时间:2015-09-17 09:28:58

标签: spring-mvc multipartform-data

我在使用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 ..

提前致谢

1 个答案:

答案 0 :(得分:0)

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

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