11.struts2文件上传
文件上传1.上传单个文件2.上传多个文件
1.上传单个文件实现步骤:(1)导入一个Jar包:commons-io-1.3.2.jar。只所以要导入这个Jar包,是因为要用到一个工具类FileUtil。若不使用此工具类,就无需导入此包了。(2)把form表单的enctype设置为:“multipart/form-data”,method设置为“post”,否则此表单不能用于上传。如下:<form enctype="multipart/form-data" action="xxx.action" method="post">
<input type="file" name="uf">
</form>(3)在Action类中添加以下属性private File uf;//上传的文件
private String ufFileName;//文件名称注意:蓝色部分对应于表单中文件字段的名称。而FileName是必须的。最后是,在Action方法中实现对上传文件的操作。2.上传多个文件
与上传单个文件相比,发生了如下几个变化:
(1)提交表单中出现多个文件上传栏,这多个的name属性名必须完全相同。
(2)Action中文件不再为File类型了,而是File类型的数组或List。当然,文件名也为相应的数组或List了。
(3)Action方法需遍历这些数组来上传这些文件。

<%@ page pageEncoding="utf-8"%> <html>
<head> <title>upload page</title> </head> <body>
<form action="single.action" enctype="multipart/form-data" method="post"> 文件<input type="file" name="uf"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
Step2:编写SingleFileUploadAction.java
package actions; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionContext; public class SingleAction {
private File uf;
private String ufFileName;
public File getUf() {
return uf;
}
public void setUf(File uf) {
this.uf = uf;
}
public String getUfFileName() {
return ufFileName;
}
public void setUfFileName(String ufFileName) {
this.ufFileName = ufFileName;
} public String execute(){
String savePath="D:/";
if(uf!=null)
{
File saveFile=new File(savePath,ufFileName);
try {
//将 uf 文件的内容复制到saveFile中。
FileUtils.copyFile(uf, saveFile);
ActionContext.getContext().put("message","文件上传成功!");
} catch (IOException e) {
e.printStackTrace();
ActionContext.getContext().put("message","文件上传失败!");
} }else{
ActionContext.getContext().put("message","没有指定要上传的文件"); }
System.out.println(ufFileName);
return "success";
} }
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
修改上传文件大小限制:默认最大2M
使用方法:
在struts.xml中添加下面这段代码:
<constant name="struts.multipart.maxSize" value="5242880"/>
struts.xml如下:
<?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> <constant name="struts.multipart.maxSize" value="9999242880"></constant>
<package name="one" extends="struts-default">
<action name="single" class="actions.SingleAction">
<result>/message.jsp</result>
</action> </package> </struts>
Step3:编写message.jsp
<%@ page pageEncoding="utf-8" isELIgnored="false"%> <html>
<head> <title>message page</title> </head> <body>
提示信息:${message} </body>
</html>
http://127.0.0.1:8080/single_file_upload/




<%@ page pageEncoding="utf-8"%> <html>
<head> <title>upload page</title> </head> <body>
<form action="multiple.action" enctype="multipart/form-data" method="post"> 文件1<input type="file" name="ufs"/><br/>
文件2<input type="file" name="ufs"/><br/>
文件3<input type="file" name="ufs"/><br/>
<input type="submit" value="上传"/>
</form>
</body>
</html>
Step2:编写MultipleAction.java
package actions; import java.io.File;
import java.io.IOException; import org.apache.commons.io.FileUtils; import com.opensymphony.xwork2.ActionContext; public class MultipleAction {
private File[] ufs;
private String[] ufsFileName; public File[] getUfs() {
return ufs;
} public void setUfs(File[] ufs) {
this.ufs = ufs;
} public String[] getUfsFileName() {
return ufsFileName;
} public void setUfsFileName(String[] ufsFileName) {
this.ufsFileName = ufsFileName;
} public String execute() {
StringBuffer sb=new StringBuffer();
String savePath = "D:/";
if (ufs != null) {
for (int i = 0; i < ufs.length; i++) {
if (ufs[i] != null) {
File saveFile = new File(savePath,ufsFileName[i]);
try {
FileUtils.copyFile(ufs[i], saveFile);
sb.append("文件"+(i+1)+"上传成功!");
} catch (IOException e) {
e.printStackTrace();
sb.append("文件"+(i+1)+"上传失败!");
}
} }
}else {
sb.append("文件指定要上传的文件!"); }
ActionContext.getContext().put("message",sb.toString());
return "success";
} }
Step2:编写web.xml与struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml如下:
<?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>
<package name="one" extends="struts-default">
<action name="multiple" class="actions.MultipleAction">
<result>/message.jsp</result>
</action> </package> </struts>
Step3:编写message.jsp
<%@ page pageEncoding="utf-8" isELIgnored="false"%> <html>
<head>
<title>message page</title>
</head> <body>
提示信息:${message} </body>
</html>
部署发布,启动tomcat,输入地址:
http://127.0.0.1:8080/mutiple_fle_upload/


注意:1.txt有内容,2.txt有内容,3.txt内容为空。


11.struts2文件上传的更多相关文章
- struts2文件上传大小限制问题小结
一:首先看一下程序执行中出现的对应报错信息,如下所示: [WARN ] 2015-03-03 15:31:11 :Unable to parse request org.apache.commons. ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- struts2文件上传大小限制问题小结(引用)
最后解决办法: 页面js控制上传文件的大小,在页面进行控制.如下代码 inputs是所有文本上传input DOM //名称信息 var nameStr=''; //大小信息 var sizeStr= ...
- struts2 文件上传和下载,以及部分源代码解析
struts2 文件上传 和部分源代码解析,以及一般上传原理 (1) 单文件上传 一.简单介绍 Struts2并未提供自己的请求解析器,也就是就Struts2不会自己去处理multipart/form ...
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
一.struts2文件上传 1.上传文件的时候要求必须使得表单的enctype属性设置为multipart/form-data,把它的method属性设置为post 2.上传单个文件的时候需要在Act ...
- springMvc 使用ajax上传文件,返回获取的文件数据 附Struts2文件上传
总结一下 springMvc使用ajax文件上传 首先说明一下,以下代码所解决的问题 :前端通过input file 标签获取文件,通过ajax与后端交互,后端获取文件,读取excel文件内容,返回e ...
- Struts2文件上传下载
Struts2文件上传 Struts2提供 FileUpload拦截器,用于解析 multipart/form-data 编码格式请求,解析上传文件的内容,fileUpload拦截器 默认在defau ...
- Struts2文件上传和下载(原理)
转自:http://zhou568xiao.iteye.com/blog/220732 1. 文件上传的原理:表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值:1) ...
- Struts2 文件上传
一:表单准备 ① 要想使用HTML 表单上传一个或多个文件 –须把 HTML表单的 enctype属性设置为multipart/form-data –须把HTML 表单的method ...
随机推荐
- FIR on Hadoop using hadoop-streaming
1.Prepare Hadoop Streaming Hadoop streaming allows you to create and run Map/Reduce jobs with any ex ...
- Swift 3 and OpenGL on Linux and macOS with GLFW
https://solarianprogrammer.com/2016/11/19/swift-opengl-linux-macos-glfw/ Swift 3 and OpenGL on Linux ...
- c/c++头文件_string
string, cstring, string.h 一.string头文件 主要包含一些字符串转换的函数 // sto* NARROW CONVERSIONS// sto* WIDE CONVERSI ...
- spring xmlns 记录
spring xmlns 命名空间可从: http://www.springframework.org/schema/ 根据需求选择 如: 1.选择 : aop ...
- composer安装
1.首先到php.net下载对应版本的php,zip版本即可,注意windows需要vc11运行库支持 2.配置path路径添加对php解压目录的引用 3.将php.ini-development ...
- Logging with NLog
相比较log4net, 我更喜欢NLog, 因为NLog 更简单, 而且配置选项也更加的清楚,可能是因为log4net 是从log4j 移植过来的一个原因吧,总感觉有很多的java 成分在. 要使用N ...
- ASP.Net MVC开发基础学习笔记:三、Razor视图引擎、控制器与路由机制学习
一.天降神器“剃须刀” — Razor视图引擎 1.1 千呼万唤始出来的MVC3.0 在MVC3.0版本的时候,微软终于引入了第二种模板引擎:Razor.在这之前,我们一直在使用WebForm时代沿留 ...
- iOS开发系列--让你的应用“动”起来
--iOS核心动画 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌.在这里你可以看到iOS中如何使用图层精简非交互式绘图,如何通过核心动画创建 ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- Meteor+AngularJS:超快速Web开发
为了更好地描述Meteor和AngularJS为什么值得一谈,我先从个人角度来回顾一下这三年来WEB开发的变化: 三年前,我已经开始尝试前后端分离,后端使用php的轻量业务逻辑框架.但 ...