Spring MVC文件上传

6

我一直收到以下错误:

org.springframework.web.multipart.support.MissingServletRequestPartException:未找到请求部分“model”。

当向Spring MVC控制器发布多部分请求时。

这是请求内容:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:4394941
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryK4y8t7mg2SNoYxC4
Cookie:SID=091f182f-5534-47c4-b0c1-8ca9c17e1f09
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/controller/home/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
X-Requested-With:XMLHttpRequest
Request Payload
------WebKitFormBoundaryK4y8t7mg2SNoYxC4
Content-Disposition: form-data; name="model"

{"name":"kjkjk","description":"kkjkjk"}
------WebKitFormBoundaryK4y8t7mg2SNoYxC4
Content-Disposition: form-data; name="photo"; filename="IMG_1281.JPG"
Content-Type: image/jpeg

控制器

@RequestMapping(value = "/t")
    public ResponseEntity<ResponseMessage> t(@CookieValue(value = "SID", required = true) String sessionId, 
            @RequestPart("model") CategoryModel model,
            @RequestPart("photo") MultipartFile file)
    {
    return new ResponseEntity<ResponseMessage>(new ResponseMessage(200, "success"), HttpStatus.OK);
    }

模型

package bla.bla.bla;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;

public class CategoryModel {

    public CategoryModel(String id, String name, String description, CategoryModel parent) {
        super();
        this.name = name;
        this.description = description;
        this.id = id;
        this.parent = parent;
    }

    public CategoryModel(String id, String name, String description, CategoryModel parent, List<CategoryModel> childrens) {
        super();
        this.name = name;
        this.description = description;
        this.id = id;
        this.parent = parent;
        this.childrens = childrens;
    }

    public CategoryModel()
    {

    }
    public String id;
    public String name;
    public String description;
    public String imageUrl; 
    public CategoryModel parent;
    public List<CategoryModel> childrens = new ArrayList<CategoryModel>();
}

我已经添加了控制器和实体,请检查并让我知道我哪里做错了?

谢谢, 詹姆斯


1
你能否澄清一下你是如何设置你的multipartResolver bean的?http://static.springsource.org/spring/docs/3.1.0.RC1/spring-framework-reference/html/mvc.html#mvc-multipart - purgatory101
1
这是配置文件: <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> - James Zalame
1
你的JSP表单长什么样? - kabal
2个回答

5

我也遇到了类似的问题,幸运的是,这个答案帮助我找出了问题所在。就像那里提到的一样,问题不在于你的Java部分。您需要更改在客户端构建CategoryModel的JavaScript逻辑。根据该答案,您的逻辑应该像下面显示的代码一样:

var file = ... // your file
var model = {
    id: 'TestId'
    name: 'TestName',
    description: 'TestDesciption',
    .... // other fields are ommited
};

var fd = new FormData();
fd.append('photo', file);
fd.append('model', new Blob([JSON.stringify(model)], { type: "application/json" }));

使用这段代码,您的异常问题应该已经解决了。

0

http://www.mkyong.com/spring-mvc/spring-mvc-file-upload-example/

文件上传依赖 -> 模型 -> 文件上传控制器 -> 文件上传验证 -> 视图页面 -> Spring配置 -> 演示
html
<form:form method="POST" commandName="fileUploadForm"
    enctype="multipart/form-data">

    <form:errors path="*" cssClass="errorblock" element="div" />

    Please select a file to upload : <input type="file" name="file" />
    <input type="submit" value="upload" />
    <span><form:errors path="file" cssClass="error" />
    </span>

</form:form>

pom.xml

        <!-- Apache Commons Upload --> 
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.2</version>
</dependency>

<!-- Apache Commons Upload --> 
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

控制器

public class FileUploadController extends SimpleFormController{

public FileUploadController(){
    setCommandClass(FileUpload.class);
    setCommandName("fileUploadForm");
}

@Override
protected ModelAndView onSubmit(HttpServletRequest request,
    HttpServletResponse response, Object command, BindException errors)
    throws Exception {

    FileUpload file = (FileUpload)command;

    MultipartFile multipartFile = file.getFile();

    String fileName="";

    if(multipartFile!=null){
        fileName = multipartFile.getOriginalFilename();
        //do whatever you want
    }

    return new ModelAndView("FileUploadSuccess","fileName",fileName);
}

模型

public class FileUpload{

MultipartFile file;
//getter and setter methods
}

...

输入类型="文件" 名称="文件" == 私有 MultipartFile 文件;

控制器 -> 模型 -> 获取文件 ^^ 是的!!!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接