struts2的文件上传特别简单,把我做的小例子给大家看一下
upload.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
< %@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
< %@ taglib prefix="s" uri="/struts-tags" %>
< %String path = request.getContextPath();pageContext.setAttribute("context",path);
%>< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"></meta><meta http-equiv="cache-control" content="no-cache"></meta><meta http-equiv="expires" content="0"></meta><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"></meta><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript">function more() {var td = document.getElementById("more");var br = document.createElement("br");var input = document.createElement("input");input.type="file";input.name="uf.upload";var button = document.createElement("input");button.type="button";button.value="remove";td.appendChild(br);td.appendChild(input);td.appendChild(button);button.onclick = function() {td.removeChild(br);td.removeChild(input);td.removeChild(button);}}</script></meta></head><body><h1>struts2上传不定数的文件,限制文件上传类型(struts.xml配置拦截器)</h1><hr /><s :fielderror></s><table align="center" border="1" width="50%"><s :form action="upload" enctype="multipart/form-data" method="post" theme="simple"><tr> <td>用户名:</td><td><s :textfield name="uf.username"></s></td></tr><tr> <td>上传:</td><td id="more"><s :file name="uf.upload"></s><font color="red">*只能上传.jpg文件,小于400kb</font><input type="button" value="添加.." onclick="more()"/></td></tr><tr> <td align="center"><s :submit value="提交"></s></td><td><s :reset value="重置"/></td></tr></s></table></body>
</html>

我把jsp上传的内容封装成了一个类UploadFile.java,下面代码显示有点问题,泛型里的好像都出错了应该是 ,,还有最下边的一行根本就没有,没时间找显示错误的问题,说明一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package pojo;import java.io.File;
import java.util.List;public class UploadFile {private String username;private List<file> upload;private List<string> uploadFileName;private List</string><string> uploadContentType;public List<file> getUpload() {return upload;}public void setUpload(List</file><file> upload) {this.upload = upload;}public List<string> getUploadContentType() {return uploadContentType;}public void setUploadContentType(List</string><string> uploadContentType) {this.uploadContentType = uploadContentType;}public List</string><string> getUploadFileName() {return uploadFileName;}public void setUploadFileName(List</string><string> uploadFileName) {this.uploadFileName = uploadFileName;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}}</string></file></string></file>

接着是struts.xml的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
< ?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!-- 更改字符集编码 在org.apache.struts2.default.properties 定义-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.multipart.maxSize" value="40960000"></constant>
<!--  显示错误信息时,src下有message.properties,要在这里配置,才显示自己写的错误信息--><constant name="struts.custom.i18n.resources" value="message"></constant><action name="upload" class="action.UploadAction"><result name="success">/uploads/result.jsp</result><result name="input">/uploads/upload.jsp</result><interceptor -ref name="fileUpload"><!-- 在org.apache.struts2.interceptor.FileUploadInterceptor找限制大小和类型的参数限制每个每个上传文件的大小,允许上传的类型,类型在tomcat-->conf-->web.xml中寻找 --><param name="maximumSize">40960000</param><param name="allowedTypes">image/jpeg,image/jpeg</param></interceptor><!-- 默认拦截器defaultStack要显示的调用,当你调用其他的拦截器后,默认拦截器不会再自动调用 --><interceptor -ref name="defaultStack"></interceptor></action></struts>

接着看UploadAction.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package action;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;import org.apache.struts2.ServletActionContext;import sjzpc.pojo.UploadFile;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {private UploadFile uf;public UploadFile getUf() {return uf;}public void setUf(UploadFile uf) {this.uf = uf;}@Overridepublic String execute() throws Exception {for(int i = 0; i < uf.getUpload().size(); i++) {//多文件上传InputStream is = new FileInputStream(uf.getUpload().get(i));//    String root = ServletActionContext.getRequest.getRealPath("/uploads"); 这个request.getRealPath()方法已过时,不建议使用String root = ServletActionContext.getServletContext().getRealPath("/uploads");OutputStream os = new FileOutputStream(new File(root,uf.getUploadFileName().get(i)));int len = 0;byte[] buffer = new byte[1024];//缓冲,提高效率while((len =  is.read(buffer)) > 0) {os.write(buffer, 0, len);}// 输入输出流记着一定要关闭os.close();is.close();}return "success";}
}

struts2多文件限制上传源码下载

转载于:https://www.cnblogs.com/JemBai/archive/2009/12/11/1621857.html

struts2 实现多文件限制上传相关推荐

  1. struts2中实现文件的上传

    struts2中实现文件的上传 文件上传的action,同时过滤上传的文件格式只对满足要求的格式支持上传 package com.inspur.action; import java.io.File; ...

  2. java struts2下载文件_Struts2下多文件的上传与下载

    Struts2下多文件的上传与下载 目录 多文件 上传 下载 随意文件java Struts2单例 配置 动态读取 李顺利 在网络上,对于Java处理文件上传和下载的技术比较多,而Struts作为一款 ...

  3. java action 上传文件_JavaWeb框架_Struts2_(七)-----文件的上传和下载

    1.  前言 这个章节是Struts2框架应用最广泛的三个版块(上传下载.国际化.校验输入)之一,所以这一版块的学习还蛮重要的. 2.  具体内容 2.1Struts2文件上传 2.1.1单文件上传 ...

  4. applet实现大文件ftp上传(一)

    由于要用APPLET实现大文件FTP上传下载,从网上搜索了几下,找到很多资料,最后决定采用基于 org.apache.commons.net.ftp包实现FTP上传下载,Net包中的类既提供对协议的底 ...

  5. 使用struts2完成ckeditor和图片上传

    代码地址如下: http://www.demodashi.com/demo/12427.html 使用struts2完成ckeditor和ckeditor图片上传 ckeditor版本ckeditor ...

  6. 【j2ee spring】38、巴巴运动网的产品文件的上传

    巴巴运动网的产品文件的上传 1.项目图解 2.我们开始做我们的相应的功能模块 页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8 ...

  7. java图片上传下载_java实现文件的上传和下载

    1. servlet 如何实现文件的上传和下载? 1.1上传文件 参考自:http://blog.csdn.net/hzc543806053/article/details/7524491 通过前台选 ...

  8. 如何限制上传服务器的文件容量,如何通过配置php文件限制上传文件的大小

    在网站开发的过程中,为了确保能够充分利用服务器的空间,在开发上传功能时,必须对上传文件的大小进行控制.那么我们如何进行对上传文件的大小进行控制呢? 控制文件的大小可以从两个方面入手: 第一个是在PHP ...

  9. SpringMVC+SwfUpload进行多文件同时上传

    由于最近项目需要做一个多文件同时上传的功能,所以好好的看了一下各种上传工具,感觉uploadify和SwfUpload的功能都比较强大,并且使用起来也很方便.SWFUpload是一个flash和js相 ...

最新文章

  1. Google Gson-反序列化列表 class 宾语? (通用类型)
  2. 越过 __chkesp 检测的缓冲区溢出
  3. 迷宫搜索问题最短路_【算法常用模板】总结(更新中)
  4. alpine登陆mysql_如何构建一个php7-alpine的docker镜像
  5. 设计模式视频教程——【从设计到模式】
  6. 【perl】LWP module
  7. iOS开发之通过代理逆向传值
  8. CEF3研究(四)之javascript集成
  9. 安卓WindowManager注入事件如何跳出进程间安全限制
  10. Box(-and-Whisker) Plot SPSS and Excel 箱图学习
  11. 斐讯k2路由器刷华硕固件做桥接中继
  12. 批量打印html文档,vue项目中使用Lodop实现批量打印html页面和pdf文件
  13. 计算机应用物联网应用技术论文,计算机应用技术论文:物联网垃圾回收站设计与实现...
  14. 2.3 pandas:汇总和计算描述统计(统计上的方法)
  15. resulful规范_ResultFul API
  16. 谁能最后享受到胜利成果?
  17. win xp和 win7 的集成版 和 安装方法
  18. USACO Training Section 1.3 Calf Flac 解题报告AC代码
  19. 服务器上的文件怎么共享给学生机,云服务器学生机
  20. 高频故障-文件扩展名消失(windows)

热门文章

  1. 【五线谱】Sibelius 7.5.1 打谱软件安装 ( 软件下载 | 软件安装 )
  2. 【Android 逆向】Dalvik 函数抽取加壳 ( 类加载流程分析 | DexPathList#findClass 函数分析 | DexFile#loadClassBinaryName 函数 )
  3. 【C 语言】文件操作 ( 文件加密解密 | 使用第三方 DES 加密解密库 | 头文件导入 | 兼容 C++ 语言 | 加密解密函数说明 )
  4. 【错误记录】集成 Tinker 热修复报错 ( No such property: variantConfiguration for class: .ApplicationVariantData )
  5. 【错误记录】Flutter / Android 报错 ( AAPT: error: attribute android:requestLegacyExternalStorage not found )
  6. 【计算理论】正则语言 ( 推广型的非确定性有限自动机 GNFA | 删除状态 | 确定性有限自动机 转为 正则表达式 )
  7. mybatis整合spring下的的各种配置文件
  8. 微信验证以及登录流程
  9. 链接SQL Server服务器
  10. 整理记录个人面试问题