Spring分段上传

我试图编写一个控制器和一个窗体,可以处理多部分文件上传和其他数据传递。 首先我做了这样的基本形式:

<form:form method="POST" commandName="myForm">

那么一切都很好,但当然没有多部分处理。 然后我添加像这样的enctype部分:

<form:form method="POST" commandName="myForm" enctype="multipart/form-data">

然后我的整个表格都搞砸了,所有的属性都给了NullPointers。 甚至没有一个简单的String名称属性工作。 另外我补充说:

<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

所以我真的不知道这个问题是什么。 任何评论都会有很大帮助。 提前Thnaks。


我们在我们的项目中使用CommonsMultipartResolver。 它是这样的。 在你的applicationContext.xml中:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="1048576000"/>
    <property name="defaultEncoding" value="UTF-8" />
</bean>

然后将您的请求投射到MultipartHttpServletRequest:

public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    if (!(req instanceof MultipartHttpServletRequest)) {
        error(resp, "Invalid request (multipart request expected)");
        return null;
    }
Map<String, MultipartFile> files = ((MultipartHttpServletRequest)req).getFileMap();
... do thomething with the files
链接地址: http://www.djcxy.com/p/48709.html

上一篇: Spring multipart upload

下一篇: Swagger configured in Spring Boot shows only methods with POST and GET mapping