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> 增加一个 更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》 希望本文所述对大家java程序设计有所帮助。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- java – 为什么我坚持不会发生什么?
- SpringBoot项目打包发布到外部tomcat(出现各种异
- 基于html5+java实现大文件上传实例代码
- mysql中数据库database、实例instance、会话sess
- java 交换两个数据的方法实例详解
- java – 具有任意轮廓的OpenCV变换形状为矩形
- 如何使用枚举或任何其他方式在java中构建类别的层
- Monitor and diagnose performance in Java SE 6
- java.io.filenotfoundexception:/home/ljh/.and
- java – OnItemClickListener()不使用List Adapt