【问题标题】:Spring mvc multipartSpring mvc 多部分
【发布时间】:2013-02-10 16:44:10
【问题描述】:

我不断收到以下错误

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

向 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>();
}

我添加了控制器和实体,请检查并告诉我哪里出错了?

谢谢, 詹姆斯

【问题讨论】:

标签: java spring-mvc


【解决方案1】:

我也遇到了类似的问题,幸运的是,this 的回答帮助我找出了问题所在。正如那里提到的,问题不在于您的 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" }));

使用这段代码应该可以修复您的异常。

【讨论】:

    【解决方案2】:

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

    文件上传依赖->模型->文件上传控制器->文件上传验证->查看页面->Spring配置->Demo

    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
    }
    

    ...

    input type="file" name="file" == private MultipartFile file;

    controller -> model -> getFile ^^ YES!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 2014-02-15
      • 1970-01-01
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2012-08-12
      相关资源
      最近更新 更多