Struts2单文件上传
第一步:首先写个上传文件的页面(简单的一个form表单)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<h1>文件上传</h1>
<br><br>
<form action="/Struts2-day02pm/upload/one_upload.action" method="post" enctype="multipart/form-data">
文件上传1:<br><br>
<input type="file" name="oneFile"><br><br>
<input type="submit" value="开始上传">
</form> </body>
第二步:创建一个基类BaseAction.java,继承ActionSupport,并实现ServletRequestAware,ServletResponseAware,ServletContextAware三个接口,重写三个接口的set方法
public class BaseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{
/**
* 编写一个基类,继承ActionSupport并实现相应的接口
* 以后的Action直接继承该类,就可以简单获取到Servlet API
* 这是一个典型的适配设计模式
* @author Owen
*/
private static final long serialVersionUID = 7267018575222346353L;
@Override
public void setServletContext(ServletContext servletContext) {
}
@Override
public void setServletResponse(HttpServletResponse response) {
}
@Override
public void setServletRequest(HttpServletRequest request) {
}
}
第三步:创建OneUploadAction请求处理类,继承BaseAction
public class OneUploadAction extends BaseAction {
private static final long serialVersionUID = -4445894434193884175L;
// 该属性名必须和<input type="file" name="oneFile">中name值一致
private File oneFile;
// 真实名称
private String oneFileFileName;
// 文件类型
private String oneFileContentType;
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
this.request = request;
}
@Override
public String execute() throws Exception {
// 获取保存上传文件在服务器的真是路径
String uploadPath = request.getServletContext().getRealPath("upload");
System.out.println(uploadPath);//目录真是路径
System.out.println("oneFile--"+oneFile.getName());//文件临时名称
System.out.println("oneFileFileName--"+oneFileFileName);//文件原始名称
System.out.println("oneFileContentType--"+oneFileContentType);//文件类型
// 第一种:该步骤适合上传小文件
// 将临时文件复制到硬盘上的真是路径
/*
File file = new File(uploadPath, oneFileFileName);//拼接文件的存放路径和存放文件的真实名称
FileUtils.copyFile(oneFile, file);//将临时文件复制到上面这个路径
*/
// 第二种:适合大文件上传操作
/*InputStream is = null;
OutputStream os = null;
is = new FileInputStream(oneFile);
os = new FileOutputStream(new File(uploadPath,oneFileFileName));
byte[] buffer = new byte[500];
int length = 0;
while((length=is.read(buffer,0,buffer.length)) != -1){
os.write(buffer, 0,length);
}
os.close();
is.close();*/
copyFile(uploadPath);
return SUCCESS;
}
public void copyFile(String uploadPath){
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(oneFile);
os = new FileOutputStream(new File(uploadPath,oneFileFileName));
byte[] buffer = new byte[500];
int len = 0;
while((len=is.read(buffer,0,buffer.length)) != -1){
os.write(buffer, 0,len);
}
os.close();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public File getOneFile() {
return oneFile;
}
public void setOneFile(File oneFile) {
this.oneFile = oneFile;
}
public String getOneFileFileName() {
return oneFileFileName;
}
public void setOneFileFileName(String oneFileFileName) {
this.oneFileFileName = oneFileFileName;
}
public String getOneFileContentType() {
return oneFileContentType;
}
public void setOneFileContentType(String oneFileContentType) {
this.oneFileContentType = oneFileContentType;
}
}
第三步:配置struts.xml文件
<struts>
<package name="upload-default" namespace="/upload" extends="struts-default">
<action name="one_upload" class="com.struts2.day02pm.action.OneUploadAction">
<result>/WEB-INF/jsp/one_upload_ok.jsp</result>
</action>
</package>
</struts>
第四步:浏览器测试
Struts2单文件上传的更多相关文章
- Struts2单文件上传原理及示例
一.文件上传的原理 表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值: 1.application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里 ...
- Struts2 单文件上传
Struts2 提供了更为简便的文件上传机制,将文件上传的复杂操作都封装到commons-fileupload.jar .commons-io.jar两个jar包中,然后再程序中使用简单的几句代码就能 ...
- struts2的文件上传
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- struts2实现文件上传和下载
在做B/S系统时,通常会涉及到上传文件和下载文件,在没接struts2框架之前,我们都是使用apache下面的commons子项目的FileUpload组件来进行文件的上传,但是那样做的话,代码看起来 ...
- Struts2实现单文件上传
首先配置一下web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi ...
- 【Struts2学习笔记(9)】单文件上传和多文件上传
(1)单文件上传 第一步:在WEB-INF/lib下增加commons-fileupload-1.2.1.jar.commons-io-1.3.2.jar. 这两个文件能够从http://common ...
- Struts2之文件上传(单文件/多文件)
<一>简述: Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,<interceptor name="fileUp ...
- struts2之单文件上传(7)
前台页面jsp <!-- 拦截的时候用这个 <s:form action="uploadAction" enctype="multipart/form-dat ...
- struts2实现文件上传(多文件上传)及下载
一.要实现文件上传,需在项目中添加两个jar文件 二.上传准备的页面 注:必须植入enctype="multipart/form-data"属性,以及提交方式要设置成post &l ...
随机推荐
- asp.net Post Get提交数据转Model实例
转自:http://blog.csdn.net/daodaowolf/article/details/8990694 此功能是将客户端HTTP协议POST GET方式提交的数据转换为某个Model实例 ...
- ASP.NET生命周期详解 [转]
最近一直在学习ASP.NET MVC的生命周期,发现ASP.NET MVC是建立在ASP.NET Framework基础之上的,所以原来对于ASP.NET WebForm中的很多处理流程,如管道事件等 ...
- [LeetCode] 234. Palindrome Linked List 解题思路
Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...
- 从Spark-Shell到SparkContext的函数调用路径过程分析(源码)
不急,循序渐进,先打好基础 Spark shell的原理 首先,我们清晰定位找到这几个. 1.spark-shell 2. spark-submit 3.spark-class 4.SparkSu ...
- 【python自动化第五篇:python入门进阶】
今天内容: 模块的定义 导入方法 import的本质 导入优化 模块分类 模块介绍 一.模块定义: 用来在逻辑上组织python代码(变量,函数,逻辑,类):本质就是为了实现一个功能(就是以.py结尾 ...
- 微信开发第4章 通过accesstoken获取用户标签管理
通过access_token获取用户标签管理: 1.获取标签列表 调用接口为: http请求方式:GET(请使用https协议) https://api.weixin.qq.com/cgi-bin/t ...
- Oracle学习.Windows 命令行 启动ORACLE服务与实例
使用数据库前要先打开数据库的实例和监听器! --总结启动命令如下: lsnrctl [start|stop|status] --启动监听器,停止监听器,查看监听器的 ...
- 导出Unity场景为配置文件
在处理很多人参与的项目时,很多时候在操作场景时,可能会牵扯到场景修改的冲突问题,这种时候,我们可以将场景以配置文件的形式存储下来(cocos的场景.android的view保存思想),可以采用json ...
- 23讲 URL
这是看完23讲后的小笔记,关于URL规则.伪静态. 一.URL规则 2.此处的区分大小写,也只是对第一个字母区分,并非对整个模块名. 3.模块名复杂时,且区分大小写,此时在地址栏访问时要用" ...
- hdu 4634 Swipe Bo 搜索
典型的bfs模拟 (广度优先搜索) ,不过有好多细节要注意,比如图中如果是 R# 走到这个R的话就无限往右走了,这样就挂了~肯定到不了出口.还有一种容易造成死循环的,比如 #E## DLLL D. ...