上传单个文件
准备
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 webflow怎么实现上传单个文件及多个文件功能的详细内容。更多信息请关注PHP中文网其他相关文章!