java开发之spring webflow实现上传单个文件及多个文件功能实例
本文实例讲述了java开发之spring webflow实现上传单个文件及多个文件功能。分享给大家供大家参考,具体如下:
上传单个文件
准备
1. 如果你项目中使用了spring security的话,参考上一篇文章,使用上篇的第二种方法,并去掉MultipartFilter(如果有配置的话),否则得不到文件
2. 流程中的变量(如用var标签定义的变量),都需要实现Serializable接口。
实现过程
在pom.xml文件中加入下列依赖:
<!-- 支持文件上传 -->
<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的配置文件)中加入文件上传解析器:
<!-- 文件上传解析器-->
<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:
@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;
}
}
流程定义代码,没什么特别的:
<?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>
上传表单代码,无需特别配置:
<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来存储多个文件:
@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;
}
}
上传表单也要修改:
<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相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
栏 目:JAVA代码
本文标题:java开发之spring webflow实现上传单个文件及多个文件功能实例
本文地址:http://www.codeinn.net/misctech/175627.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




