Spring分段上传

Sor*_*rex 1 java forms spring file-upload multipart

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

<form:form method="POST" commandName="myForm">
Run Code Online (Sandbox Code Playgroud)

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

<form:form method="POST" commandName="myForm" enctype="multipart/form-data">
Run Code Online (Sandbox Code Playgroud)

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

<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
Run Code Online (Sandbox Code Playgroud)

所以我真的不知道这个问题是什么.任何评论都会有所帮助.事先提醒.

Dmi*_*oda 5

我们在项目中使用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>
Run Code Online (Sandbox Code Playgroud)

然后将您的请求转换为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
Run Code Online (Sandbox Code Playgroud)