上傳單一檔案
準備
#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中文網其他相關文章!