13.8.2.使用MultipartResolver
下面的例子说明了如何使用CommonsMultipartResolver:
<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="100000"/>
</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="maxUploadSize" value="100000"/>
</bean>
当然你需要在classpath中为分段文件解析器提供正确的jar文件。如果是CommonsMultipartResolver,你需要使用commons-fileupload.jar,如果是CosMultipartResolver,则使用cos.jar。
你已经看到如何设置Spring处理文件上传请求,接下来我们看看如何使用它。当Spring的DispatcherServlet发现文件上传请求时,它会激活定义在上下文中的解析器来处理请求。这个解析器随后是将当前的HttpServletRequest封装成MultipartHttpServletRequest,后者支持分段文件上传。使用MultipartHttpServletRequest,你可以获取请求所包含的上传信息,甚至可以在控制器中获取分段文件的内容。
在MultipartResolver完成分段文件解析后,这个请求就会和其它请求一样被处理。为了使用文件上传,你需要创建一个带文件上传域(upload field)的(HTML)表单,让Spring将文件绑定到你的表单上(如下所示):
<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>
在上面这个表单里有一个input元素,这个元素的名字(“file”)和服务器端处理这个表单的bean(在下面将会提到)中类型为byte[]的属性名相同。 在这个表单里我们也声明了编码参数(enctype="multipart/form-data")以便让浏览器知道如何对这个文件上传表单进行编码(千万不要忘记这么做!)。
和其它不能自动转为字符串类型或者基本类型(primitive type)的属性一样,为了将上传的二进制数据存成bean的属性,你必须通过ServletRequestDatabinder注册一个属性编辑器。Spring中内置了几个这样的编辑器,它们可以处理文件,然后将结果存成bean的属性。比如,StringMultipartEditor可以将文件转换成一个字符串(使用用户声明的字符集)。ByteArrayMultipartEditor可以以将文件转换为byte数组。他们的功能和CustomDateEditor相似。
总而言之,为了使用表单上传文件,你需要声明一个解析器,一个控制器,再将文件上传的URL映射到控制器来处理这个请求。下面是这几个bean的声明。
<beans>
<!-- lets use the Commons-based implementation of the MultipartResolver interface -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/upload.form=fileUploadController
</value>
</property>
</bean>
<bean id="fileUploadController" class="examples.FileUploadController">
<property name="commandClass" value="examples.FileUploadBean"/>
<property name="formView" value="fileuploadform"/>
<property name="successView" value="confirmation"/>
</bean>
</beans>
下面的代码定义了控制器和用来存放文件的那个bean。
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
binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}
}
public class FileUploadBean {
private byte[] file;
public void setFile(byte[] file) {
this.file = file;
}
public byte[] getFile() {
return file;
}
}
FileUploadBean用一个byte[]类型的属性来存放文件。前面已经提到过,通常控制器注册一个自定义的编辑器以便让Spring知道如何将解析器找到的multipart对象转换成bean指定的属性,但在上面的例子中,我们除了将byte数组记录下来以外,没有对这个文件进行任何操作,在实际的应用程序中你可以做任何你想做的事情(比如将文件存储在数据库中,通过电子邮件发送给某人等等)。
在下面这个例子里,上传的文件被绑定为(表单支持的)对象(form backing object)的String属性:
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
String 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 a String
// we have to register a custom editor
binder.registerCustomEditor(String.class, new StringMultipartFileEditor());
// now Spring knows how to handle multipart object and convert them
}
}
public class FileUploadBean {
private String file;
public void setFile(String file) {
this.file = file;
}
public String getFile() {
return file;
}
}
如果仅仅是处理一个文本文件的上传,上面这个例子的做法还是合理的。但如果上传的是一张图片, 那段代码就会出问题。
最后的解决方法就是将表单支持对象(form backing object) 的相关属性设成MultipartFile类型。 这样的话,没有类型转换的需要,我们也就不需要声明任何属性编辑器(PropertyEditor)。
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
MultipartFile 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);
}
}
public class FileUploadBean {
private MultipartFile file;
public void setFile(MultipartFile file) {
this.file = file;
}
public MultipartFile getFile() {
return file;
}
}
相关推荐
java spring 文件上传实例,包含了完整的 spring 文件上传配置
struts+spring文件上传大小限制 所以不设资源分,可以自由下载
例如MSDN 的资源上传,yahoo邮箱的附件上传,论坛的附件上传等,文件上传的方式有很多种,例如,使用Struts框架实现文件上传,在JSP中使用jspSmartUpload 组件实现文件上传和使用Spring框架实现文件上传等
spring文件上传,文件上传 传统方式 借助第三方组件实现文件上传: UserController.java编写:
封装springmvc上传单文件、多文件保存,保存文件时(文件夹路径不存在则创建),会改变文件名,使用起来非常方便。
使用了Apache的commons-fileupload完成的具体的上传操作,可以在Spring的各个版本使用,具体下载的代码可以参看我的对应的博客
spring文件上传测试项目
AXIS2 + SPRING 文件上传 用MYECLIPSE直接导入文件运行即可.
spring mvc上传文件jar包,下载解压导入即用!!!!!!
这里整理了齐全的jar包 ,我是在开发的过程中积累的,很多是跟别人要的,希望能帮到系伙伴们.不够的请联系我
使用springcloud的负载均衡 以及网关 实现文件的上传处理参看链接 https://blog.csdn.net/zhaoyu_nb/article/details/80579918
通用excel导入功能 文件上传下载功能 以及相关的依赖包 自己用的,下载你自己给分
NULL 博文链接:https://fanshuyao.iteye.com/blog/2431007
spring mvc文件上传,通过监听器实现进度条
基于SpringBoot的文件上传系统,前后端分离,单文件上传,多文件上传,大文件上传,断点续传,文件秒传,图片上传 项目经过严格测试,确保可以运行! 采用前后端分离的方式进行开发,实现了几种常用的文件上传功能...
SpringMVC单文件上传、多文件上传、文件列表显示、文件下载,具体效果和过程看博文http://blog.csdn.net/evankaka/article/details/45826697
用springmvc的框架,实现了文件的上传,真实进度条的显示,多种上传方式选择一种,总有一种适合。jar包也都在工程里,是maven项目。访问http://localhost:8110/SpringMVCFileUpload/up3.jsp,或者同目录下的其他jsp...
Spring MVC文件上传下载,是http://blog.csdn.net/geloin/article/details/7537425#comments的配套资源。
基于Spring的Web项目,涉及的要点、servlet的注入和使用,spring上下文对象的获得和使用,以及配置属性文件properties中属性值的获得,最后通过前台首页显示,实现文件的上传和下载