Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc SpringMVC:上传一个文件_Spring Mvc_Multipartform Data - Fatal编程技术网

Spring mvc SpringMVC:上传一个文件

Spring mvc SpringMVC:上传一个文件,spring-mvc,multipartform-data,Spring Mvc,Multipartform Data,我在用SpringMVC4.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 = 20

我在用SpringMVC4.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;
  }
最后是函数
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?
有人知道如何解决这个问题吗??问题是我不知道如何配置这个多部分解析器


提前感谢您(在您的mvcconfig中)这样配置
CommonsMultipartResolver


你说的mvcconfig是什么意思??
[WARNING] 
org.springframework.web.util.NestedServletException: 
Request processing failed; 
nested exception is java.lang.IllegalArgumentException: 
Expected MultipartHttpServletRequest: 
is a MultipartResolver configured?
@Bean
public CommonsMultipartResolver multipartResolver(){
    CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
    multipartResolver.setDefaultEncoding("UTF-8");
    multipartResolver.setMaxUploadSize(-1);
    return multipartResolver;
}