技术开发 频道

Web框架

下面的例子说明了如何使用CommonsMultipartResolver:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maximumFileSize"> <value>100000</value> </property> </bean> 这个例子使用CosMultipartResolver: <bean id="multipartResolver" class="org.springframework.web.multipart.cos.CosMultipartResolver"> <!-- one of the properties available; the maximum file size in bytes --> <property name="maximumFileSize"> <value>100000</value> </property> </bean>

当然你需要在你的classpath中为multipart解析器提供正确的jar文件。如果是CommonsMultipartResolver,你需要使用commons-fileupload.jar,如果是CosMultipartResolver,使用cos.jar。

你已经看到如何设置Spring处理multipart请求,接下来我们看看如何使用它。当Spring的DispatchServlet发现multipart请求时,它会激活定义在上下文中的解析器并处理请求。它通常做的就是将当前的HttpServletRequest封装到支持multipart的MultipartHttpServletRequest。使用MultipartHttpServletRequest,你可以获取请求所包含的multipart信息,在控制器中获取具体的multipart内容。

在一个表单中处理multipart
在MultipartResolver完成multipart解析后,multipart请求就会和其它请求一样被处理。使用multipart,你需要创建一个带文件上传域的表单,让Spring将文件绑定到你的表单上。就象其它不会自动转换成String或基本类型的属性一样,为了将二进制数据放到你的bean中,你必须用ServletRequestDatabinder注册一个自定义的编辑器。Spring有许多编辑器可以用来处理文件,以及在bean中设置结果。StringMultipartEditor能将文件转换成String(使用用户定义的字符集),ByteArrayMultipartEditor能将文件转换成字节数组。它们就象CustomDateEditor一样工作。

所以,为了在网站中使用表单上传文件,需要声明解析器,将URL映射到控制器,以及处理bean的控制器本身。

<beans> ... <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/upload.form">fileUploadController</prop> </props> </property> </bean> <bean id="fileUploadController" class="examples.FileUploadController"> <property name="commandClass"><value>examples.FileUploadBean</value></property> <property name="formView"><value>fileuploadform</value></property> <property name="successView"><value>confirmation</value></property> </bean> </beans>

 然后,创建控制器和含有文件属性的bean

// snippet from FileUploadController public class FileUploadController extends SimpleFormController { protected ModelAndView onSubmit( HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws ServletException, IOException { // cast the bean FileUploadBean bean = (FileUploadBean)command; // let's see if there's content there byte[] file = bean.getFile(); if (file == null) { // hmm, that's strange, the user did not upload anything } // well, let's do nothing with the bean for now and return: return super.onSubmit(request, response, command, errors); } protected void initBinder( HttpServletRequest request, ServletRequestDataBinder binder) throws ServletException { // to actually be able to convert Multipart instance to byte[] // we have to register a custom editor (in this case the // ByteArrayMultipartEditor binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor()); // now Spring knows how to handle multipart object and convert them } } // snippet from FileUploadBean public class FileUploadBean { private byte[] file; public void setFile(byte[] file) { this.file = file; } public byte[] getFile() { return file; } }


你会看到,FileUploadBean有一个byte[]类型的属性来存放文件。控制器注册一个自定义的编辑器以便让Spring知道如何将解析器发现的multipart对象转换成bean指定的属性。在这些例子中,没有对bean的byte[]类型的属性做任何处理,但是在实际中可以做任何你想做的(将文件存储在数据库中,通过电子邮件发送给某人,等等)。

但是我们还没有结束。为了让用户能真正上传些东西,我们必须创建表单:

<html>
<head>
<title>Upload a file please</title>
</head>
<body>
<h1>Please upload a file</h1>
<form method="post" action="upload.form" enctype="multipart/form-data">
<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
你可以看到,我们在bean的byte[]类型的属性后面创建了一个域。我们还添加了编码属性以便让浏览器知道如何编码multipart的域(千万不要忘记!)现在就可以工作了。

0
相关文章