Home >Java >javaTutorial >How to implement the function of uploading single file and multiple files in spring webflow in java development
Upload a single file
Preparation
1. If you use spring security in your project, refer to the previous article and use The second method in the previous article, and remove the MultipartFilter (if configured), otherwise you will not get the file
2. Variables in the process (such as variables defined with the var tag) need to implement the Serializable interface .
Implementation process
Add the following dependencies to the pom.xml file:
<!-- 支持文件上传 --> <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>
In spring-servlet.xml (Spring MVC configuration file) Add the file upload parser:
<!-- 文件上传解析器--> <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>
Entity class, remember to implement the Serializable interface, the attribute type is 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; } }
Process definition code, nothing special:
<?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>
Upload form code, no special configuration required:
<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>
That’s it
Upload multiple files
Upload a single file can be uploaded in front This can be achieved with minor modifications to a single file.
Implementation
First of all, the entity class needs to be modified and List is used to store multiple files:
@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; } }
The upload form also needs to be modified:
<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>
Just add a multiple="multiple"
attribute.
The above is the detailed content of How to implement the function of uploading single file and multiple files in spring webflow in java development. For more information, please follow other related articles on the PHP Chinese website!