2015-09-17 5 views
0

У меня возникли некоторые проблемы загрузки файла с Spring MVC-4.2.1 ..Spring 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; 
    } 

К сожалению, я не могу изменить код для конечной точки .. Это автоматически генерируется с чванством-CodeGen .. Но я могу изменить две другие функции ..

Если я использую это, я получаю эту ошибку:

[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

Смежные вопросы