struts2上传下载
struts上传下载必须引入两个jar文件:
commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import org.apache.struts2.ServletActionContext; import pojo.User; import com.opensymphony.xwork2.ActionSupport; public class FileUpAction extends ActionSupport{
private static final int BUFFER_SIZE = 40*40;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
private User user;
private static void copy(File source, File target){
InputStream is = null;
OutputStream os = null;
try{
is = new BufferedInputStream(new FileInputStream(source), BUFFER_SIZE);
os = new BufferedOutputStream(new FileOutputStream(target), BUFFER_SIZE);
int len = 0;
byte[] bs = new byte[BUFFER_SIZE];
while ((len=is.read(bs))!=-1) {
os.write(bs, 0, len);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
if(is!=null){
try{
is.close();
}catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
if(os!=null){
try{
os.close();
}catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}
}
}
} @Override
public String execute() throws Exception{
String path = ServletActionContext.getServletContext().getRealPath(this.getSavePath())+"\\"+this.getUploadFileName();
user.setPhone(this.uploadFileName);
File target = new File(path);
copy(this.upload, target);
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;
} public String getSavePath() {
return savePath;
} public void setSavePath(String savePath) {
this.savePath = savePath;
} public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}
}
struts2中的action配置
<action name="fileUp" class="actions.FileUpAction">
<param name="savePath">/upload</param>
<result>/userInfo.jsp</result>
</action>
表单
<s:form action="fileUp" namespace="/" method="post" enctype="multipart/form-data">
<s:textfield name="user.name" label="姓名" size="20"/>
<s:file name="upload" label="形象" size="20"/>
<s:textfield name="user.age" label="年龄" size="20"/>
<s:radio list="#{1:'男',2:'女' }" name="user.sex" listKey="key" listValue="value" value="1" label="性别" cssStyle="border:0px;"/>
<s:textfield name="user.icard" label="身份证号" size="20"/>
<s:textfield name="user.phone" label="联系电话" size="20"/>
<s:textfield name="user.address" label="家庭住址" size="20"/>
<s:submit value="确定录入" align="center"/>
</s:form>
如果表单中包含一个name属性为xxx的文件域,那么在Action中可以使用如下3个属性来封装文件域信息:
File xxx 封装文件域对应的文件内容
String xxxContextType 封装文件域对应文件的文件类型
String xxxFileName 封装文件域对应文件的文件名
使用图片的时候
<img src="upload/<s:property value="uploadFileName"/>"/>
下载
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private String downPath;
public InputStream getInputStream() throws Exception{
return ServletActionContext.getServletContext().getResourceAsStream(downPath);
}
public String getDownPath() {
return downPath;
}
public void setDownPath(String downPath) {
this.downPath = downPath;
}
public String getDownloadFileName(){
String downFileName = downPath.substring(7);
try{
downFileName = new String(downFileName.getBytes(), "ISO8859-1");
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
return downFileName;
}
@Override
public String execute() throws Exception{
return SUCCESS;
}
struts2.xml配置
<action name="downLoad" class="actions.DownloadAction">
<result type="stream">
<param name="contentType">
application/msword,text/plain,application/vnd.ms-powerpoint,application/vnd.ms-excel
</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="${downloadFileName}"
</param>
<param name="bufferSize">40960</param>
</result>
</action>
使用
<a href="downLoad.action?downPath=upload/123.png">下载</a>
struts2上传下载的更多相关文章
- struts2 上传下载文件,异常处理,数据类型转换
一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...
- Struts2 上传下载
一. 1.文件上传是web应用经常用到的一个知识.原理是,通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数 据以二进制编码的方式提交,在接收此请求的Se ...
- Struts2实现文件上传下载功能(批量上传)
今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
- JAVA Web 之 struts2文件上传下载演示(一)(转)
JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...
- Struts2 文件上传,下载,删除
本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...
- struts2.1.6教程九、文件上传下载(了解)
首先建立struts2UpDownLoad项目,搭建好struts2基本的开发环境. 上传实例 步骤一:upload.jsp代码如下: <s:form action="upload&q ...
- Struts2学习(三)上传下载
今天记录一下利用struts2实现上传下载,借此案例说明一下struts2的开发流程. 须要注意的是struts2版本号不同非常多地方的写法是不同的.本例使用struts2.3.15 .有差别的地方文 ...
- Struts2配合layui多文件上传--下载
先说上传: 前台上传文件的js代码: var demoListView = $('#demoList') ,uploadListIns = upload.render({ elem: '#testLi ...
随机推荐
- matlab最小二乘法数据拟合函数详解
定义: 最小二乘法(又称最小平方法)是一种数学优化技术.它通过最小化误差的平方和寻找数据的最佳函数匹配.利用最小二乘法可 以简便地求得未知的数据,并使得这些求得的数据与实际数据之间误差的平方和为最小. ...
- ORA-03113: 通信通道的文件结尾 进程 ID: 764 会话 ID: 125 序列号: 5
昨天因为导入很久数据,最后一看是因为数据文件不够,后来就关机了.现在,开启数据库,总是报“ORA-03113: 通信通道的文件结尾” SQL> conn /as sysdba; 已连接到空闲例程 ...
- Sql Server之数据库规范——1、自动化规范命名
一.废话: 随着数据库的规模越来越大,数据库的表也有成百上千,如果需要对数据库表名及字段名做操作,单个还好,直接一条语句搞定了,但如果要对整个库的所有表和字段名做操作,那就显得有点麻烦了.因此,我们需 ...
- nginx proxy_pass 后面的斜杠
# add / location /app/ { proxy_pass http://$backend/; } # location /app/ { proxy_pass http://$backen ...
- Analysis about different methods for reading and writing file in Java language
referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...
- 11-3URLTestDemo实例操作完成URL单元测试
11-3URLTestDemo 1.File -> New -> Project 在左边模板中选择Visual C#里的Web,对应到的项目类型选择ASP.NET MVC3 Web App ...
- SQL Server 数据类型陷阱
1. bit 类型:bit(1) 不要以为它只占一个位,事实上它要占一个字节!也就是说当n < 8 时都是这样的! 2. varchar(n) 这里的n不能大于8000,如果想要比8000大你 ...
- Linux ---> 监控JVM工具
Linux ---> 监控JVM工具shkingshking 发布时间: 2013/10/10 01:27 阅读: 2642 收藏: 26 点赞: 1 评论: 0 JDK内置工具使用 jps(J ...
- 如何取消一个本地svn目录与svn的联系(即恢复原有图标等)
在使用svn 的时候容易手抖错选update地址,使其目录所有同级文件夹上出现蓝色“?”图样,非常烦人,下面记录一下解决方案. 首先在该目录下打开同级文件件,工具→文件夹选项→查看→隐藏文件和文件夹→ ...
- ios post空文件流导致400错误