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 ..
在此先感謝
你說的mvcconfig是什麼意思? ? – Smeiliz