本文实例讲述了java开发之spring webflow实现上传单个文件及多个文件功能。分享给大家供大家参考,具体如下:
上传单个文件
准备
1. 如果你项目中使用了spring security的话,参考上一篇文章,使用上篇的第二种方法,并去掉MultipartFilter(如果有配置的话),否则得不到文件
2. 流程中的变量(如用var标签定义的变量),都需要实现Serializable接口。
实现过程
在pom.xml文件中加入下列依赖:
1
2
3
4
5
6
7
8
9
10
11
|
<!-- 支持文件上传 --> < dependency > < groupId >commons-fileupload</ groupId > < artifactId >commons-fileupload</ artifactId > < version >1.2.1</ version > </ dependency > < dependency > < groupId >commons-io</ groupId > < artifactId >commons-io</ artifactId > < version >2.4</ version > </ dependency > |
在spring-servlet.xml(Spring MVC的配置文件)中加入文件上传解析器:
1
2
3
4
5
|
<!-- 文件上传解析器--> < 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 = "10485760" /> </ bean > |
实体类,记住要实现Serializable接口,属性类型是MultipartFile:
1
2
3
4
5
6
7
8
9
10
11
|
@Component public class GoodsEntity implements Serializable{ private static final long serialVersionUID = 1L; private MultipartFile images; public MultipartFile getImages() { return images; } public void setImages(MultipartFile images) { this .images = images; } } |
流程定义代码,没什么特别的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "UTF-8" ?> < flow xmlns = "http://www.springframework.org/schema/webflow" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/webflow http://www.springframework.org/schema/webflow/spring-webflow-2.0.xsd"> < var name = "goods" class = "com.huanle.model.entity.GoodsEntity" /> < view-state id = "viewfirst" view = "/views/user/releasegoods/release_first.jsp" model = "goods" > < transition on = "submit" to = "viewsecond" ></ transition > </ view-state > < view-state id = "viewsecond" view = "/views/user/releasegoods/second.jsp" model = "goods" > < transition on = "submit" to = "performReleaseGoodsAction" ></ transition > </ view-state > < action-state id = "performReleaseGoodsAction" > < evaluate expression = "goodsService.save(goods)" ></ evaluate > < transition to = "returntouserindex" ></ transition > </ action-state > < end-state id = "returntouserindex" view = "/views/user/seller/index.jsp" ></ end-state > < global-transitions > < transition on = "cancel" to = "returntouserindex" ></ transition > </ global-transitions > </ flow > |
上传表单代码,无需特别配置:
1
2
3
4
5
|
< form:form action = "${flowExecutionUrl}&_eventId=submit&${_csrf.parameterName}=${_csrf.token}" method = "post" commandName = "goods" enctype = "multipart/form-data" > < input type = "hidden" name = "_flowExecutionKey" value = "${flowExecutionKey}" /> 商品图片:< form:input id = "images" path = "images" type = "file" multiple = "multiple" /> < input type = "submit" > </ form:form > |
就这样就可以了
上传多个文件
上传单个文件可在前面上传单个文件基础上稍作修改就可以实现了。
实现
首先,实体类要修改,使用List来存储多个文件:
1
2
3
4
5
6
7
8
9
10
11
|
@Component public class GoodsEntity implements Serializable{ private static final long serialVersionUID = 1L; private List<MultipartFile> images; public List<MultipartFile> getImages() { return images; } public void setImages(List<MultipartFile> images) { this .images = images; } } |
上传表单也要修改:
1
2
3
4
5
|
<form:form action= "${flowExecutionUrl}&_eventId=submit&${_csrf.parameterName}=${_csrf.token}" method= "post" commandName= "goods" enctype= "multipart/form-data" > <input type= "hidden" name= "_flowExecutionKey" value= "${flowExecutionKey}" /> 商品图片:<form:input path= "images" type= "file" multiple= "multiple" /> <input type= "submit" value= "提交" > </form:form> |
增加一个multiple="multiple"
属性即可。
希望本文所述对大家java程序设计有所帮助。
原文链接:http://blog.csdn.net/ruangong1203/article/details/51170385