java开发之spring webflow实现上传单个文件及多个文件功能实例

2025-05-29 0 43

本文实例讲述了java开发之spring webflow实现上传单个文件及多个文件功能。分享给大家供大家参考,具体如下:

上传单个文件

准备

1. 如果你项目中使用了spring security的话,参考上一篇文章,使用上篇的第二种方法,并去掉MultipartFilter(如果有配置的话),否则得不到文件

2. 流程中的变量(如用var标签定义的变量),都需要实现Serializable接口。

实现过程

在pom.xml文件中加入下列依赖:

?

1

2

3

4

5

6

7

8

9

10

11
<!-- 支持文件上传 -->

<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的配置文件)中加入文件上传解析器:

?

1

2

3

4

5
<!-- 文件上传解析器-->

<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:

?

1

2

3

4

5

6

7

8

9

10

11
@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;

}

}

流程定义代码,没什么特别的:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
<?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>

上传表单代码,无需特别配置:

?

1

2

3

4

5
<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来存储多个文件

?

1

2

3

4

5

6

7

8

9

10

11
@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;

}

}

上传表单也要修改:

?

1

2

3

4

5
<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程序设计有所帮助。

原文链接:http://blog.csdn.net/ruangong1203/article/details/51170385

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java开发之spring webflow实现上传单个文件及多个文件功能实例 https://www.kuaiidc.com/114015.html

相关文章

发表评论
暂无评论