2015-09-17 94 views
0

我有一些麻烦上传与Spring的MVC 4.2.1文件..弹簧MVC:上传文件

这里是端点代码:

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

可悲的是,我不能更改代码为终点。它与招摇,代码生成自动生成的。但我可以改变其他两个功能..

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

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

有谁知道如何摆脱这个问题?问题是我不知道如何配置此MultipartResolver ..

在此先感谢

回答

0

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

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

你说的mvcconfig是什么意思? ? – Smeiliz