一、实现单个文件上传

1、创建如下web项目结构

2、在src下的com.action包下创建UploadAction.java

 package com.action;
import java.io.File; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
* 单个文件上传
* @author Dell
*
*/
public class UploadAction extends ActionSupport {
//封装上传文件属性==form表单中file文件域的name属性值保持一致
private File upload; //封装上传文件的类型,固定语法=file文件域name属性值+ContextType;
private String uploadContentType; //封装文件上传名,固定语法=file文件域name属性值+FileName
private String uploadFileName; @Override
public String execute() throws Exception {
//获取上下文对象
ServletContext appliaction=ServletActionContext.getServletContext();
//获取要保存文件的位置
String path=appliaction.getRealPath("/upload");
//创建一个与上传同名的文件
File file=new File(path,uploadFileName);
//将临时文件内容拷贝到目标文件夹下的那个同名的文件
FileUtils.copyFile(upload, file);
//删除临时文件
upload.delete();
return SUCCESS;
} public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
} }

UploadAction.java

3、在src下创建struts.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "struts-2.1.dtd" >
<struts>
<!-- 中文乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/> <constant name="struts.devMode" value="true"/>
<!-- 设置上传文件的总大小 -->
<constant name="struts.multipart.maxSize" value="200000000"/> <package name="default" namespace="/" extends="struts-default">
<!--文件上传 -->
<action name="*" class="com.action.{1}Action" method="execute">
<result>/success.jsp</result>
<result name="input">/error.jsp</result>
<interceptor-ref name="defaultStack">
<!-- 配置文件上传的大小,这里配置的是上传文件的单个大小 -->
<param name="fileUpload.maximumSize">20971520</param> <!-- 配置文件上传允许的类型 -->
<param name="fileUpload.allowedTypes">text/plain,application/msword</param> <!-- 配置文件的扩展名 -->
<param name="fileUpload.allowedExtensions">.txt,.doc</param>
</interceptor-ref>
</action> </package>
</struts>

struts.xml

4、在WebRoot下创建upload文件夹

5、编辑WebRoot下的WEB-INF下的web.xml文件

 <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.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>upload.jsp</welcome-file>
</welcome-file-list> </web-app>

web.xml

6、在WebRoot下创建upload.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri= "/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>单个文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<s:form action="Upload.action" enctype="multipart/form-data" method="post">
<s:textfield name="title" label="标题"/><br/>
<s:file name="upload" label="选择文件"/><br/>
<s:submit name="submit" value="上传文件"/>
</s:form>
</body>
</html>

upload.jsp

7、在WebRoot下创建success.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
上传成功!
您所上传的文件是:<s:property value="uploadFileName"/><br/>
文件类型:<s:property value="uploadContentType"/><br/>
</body>
</html>

success.jsp

8、在WebRoot下创建error.jsp文件

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
操作失败!!
</body>
</html>

error.jsp

9、运行

二、上传多个文件

(接着上面的项目进行)

1、在src的com.action包下创建ManyUploadAction.java

 package com.action;
import java.io.File; import javax.servlet.ServletContext; import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
* 多个文件上传
* @author Dell
*
*/
public class ManyUploadAction extends ActionSupport {
//封装上传文件属性==form表单中file文件域的name属性值保持一致
private File[] upload; //封装上传文件的类型,固定语法=file文件域name属性值+ContextType;
private String[] uploadContentType; //封装文件上传名,固定语法=file文件域name属性值+FileName
private String[] uploadFileName; @Override
public String execute() throws Exception {
//获取上下文对象
ServletContext appliaction=ServletActionContext.getServletContext();
//获取要保存文件的位置
String path=appliaction.getRealPath("/upload");
for (int i = 0; i < upload.length; i++) { //创建一个与上传同名的文件
File file=new File(path,uploadFileName[i]);
//将临时文件内容拷贝到目标文件夹下的那个同名的文件
FileUtils.copyFile(upload[i], file);
//删除临时文件
upload[i].delete();
}
return SUCCESS;
} public File[] getUpload() {
return upload;
} public void setUpload(File[] upload) {
this.upload = upload;
} public String[] getUploadContentType() {
return uploadContentType;
} public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
} public String[] getUploadFileName() {
return uploadFileName;
} public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
} }

ManyUploadAction.java

2、在WebRoot下创建manyUpload.jsp

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri= "/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>多个文件上传</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<s:form action="ManyUpload.action" enctype="multipart/form-data" method="post">
<s:file name="upload" label="选择文件1"/><br/>
<s:file name="upload" label="选择文件2"/><br/>
<s:file name="upload" label="选择文件3"/><br/>
<s:file name="upload" label="选择文件4"/><br/>
<s:file name="upload" label="选择文件5"/><br/>
<s:submit name="submit" value="上传文件"/><br/>
</s:form>
</body>
</html>

manyUpload.jsp

3、运行

三、文件下载

(接着上面的项目进行)

1、在src下的com.action包下创建FileDownAction.java

 package com.action;

 import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport; /**
* 文件下载
* @author Dell
*
*/
public class FileDownAction extends ActionSupport{
//读取下载文件的目录
private String inputPath;
//下载文件的文件名
private String fileName;
//读取下载文件的输入流
private InputStream inputStream;
//下载文件的类型
private String contentType; @Override
public String execute() throws Exception {
return SUCCESS;
} public String getInputPath() {
return inputPath;
}
public void setInputPath(String inputPath) {
this.inputPath = inputPath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
//创建InputStream输入流
public InputStream getInputStream() throws FileNotFoundException {
//得到下载文件的实际路径
String path=ServletActionContext.getServletContext().getRealPath(inputPath);
//创建输入流实现文件的下载读取
return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
} }

FileDownAction.java

2、编辑src下的struts.xml文件(添加文件下载的action的内容)

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "struts-2.1.7.dtd" >
<struts>
<!-- 中文乱码处理 -->
<constant name="struts.i18n.encoding" value="UTF-8"/> <!-- 设置开发模式 -->
<constant name="struts.devMode" value="true"/> <!-- 设置上传文件的大小 -->
<constant name="struts.multipart.maxSize" value="200000000"/> <package name="default" namespace="/" extends="struts-default">
<!-- 文件上传 -->
<action name="*" class="com.action.{1}Action" method="execute">
<result>/success.jsp</result>
<result name="input">/error.jsp</result>
<!-- 引用默认拦截器栈 -->
<interceptor-ref name="defaultStack">
<!-- 配置上传的大小,这里配置的是上传文件的单个大小 -->
<param name="fileUpload.maximumSize">20971520</param>
<!-- 配置文件上传允许的类型 -->
<!-- 图片:image/pjpeg,image/jpeg,image/gif,image/png,image/bmp -->
<!-- 文本文件:text/plain -->
<!-- HTML网页:text/html -->
<!-- 可执行文件:application/octet-stream -->
<!-- PPT:Application/vnd.ms-powerpoint -->
<!-- Excel:Application/vnd.ms-excel -->
<!-- Word:application/msword -->
<param name="fileUpload.allowedTypes">text/plain,application/msword</param>
<!-- 配置文件的扩展名 -->
<param name="fileUpload.allowedExtensions">.txt,.doc</param> </interceptor-ref>
</action> <!-- 文件下载 -->
<action name="download" class="com.action.FileDownAction" method="execute">
<!-- 配置action中使用的参数-->
<param name="inputPath">/upload</param> <result name="success" type="stream">
<!--设置浏览器的文件类型 -->
<param name="contentType">application/octet-stream</param>
<!-- 输入流,读取 -->
<param name="inputStream">inputStream</param>
<!--设置http响应的头 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<!-- 设置缓冲区大小 -->
<param name="bufferSize">4096</param>
</result>
</action>
</package>
</struts>

struts.xml

3、在WebRoot下创建down.jsp页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri= "/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>文件下载</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<a href="download.action?fileName=20140914.txt">点击此处下载20140914.txt文档</a> </body>
</html>

down.jsp

4、运行

最新文章

  1. html / css打印样式
  2. android:installLocation = &quot;auto&quot; 的用法
  3. BZOJ4573 : [Zjoi2016]大森林
  4. jquery导航栏html页面跳转导航字体变色
  5. 快速开发~Rafy框架的初步认识
  6. debian attempt to kill init!
  7. IOS异步和多线程操作&amp;&amp;在sqlite3中的应用
  8. mvc 生成输出url
  9. NOIP2015-D2T3运输计划
  10. vue better-scroll用法
  11. PAT A1127 ZigZagging on a Tree (30 分)——二叉树,建树,层序遍历
  12. 【转】Python之列表生成式、生成器、可迭代对象与迭代器
  13. 一次TIME_WAIT和CLOSE_WAIT故障和解决办法
  14. 合理利用配置不同的机器资源做redis cluster的server
  15. TOJ5398: 签到大富翁(简单模拟) and TOJ 5395: 大于中值的边界元素(数组的应用)
  16. F - A计划
  17. 还没被玩坏的robobrowser(8)——robobrowser的实现原理
  18. OpenCV学习:播放avi视频文件
  19. FrameWork数据权限浅析3之基于角色的配置表实现行级数据安全
  20. 【设计模式最终总结】桥接模式 VS 外观模式

热门文章

  1. linux 学习-用户&amp;群组&amp;权限
  2. 摘抄来自论坛的一些DDD讨论
  3. 系统不识别某些Android设备:adb devices不显示问题解决
  4. 深入了解css3新特性
  5. [Q]pdfFactory打印机内存不能为read的问题
  6. iOS 最新App提交上架流程及部分问题的解决方案2016.12.21,感谢原博主!!!
  7. Anaconda 安装概要
  8. ueditor1.4.3 在IE8下的 BUG
  9. linux + shell 命令等
  10. vim menu乱码