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 ...
随机推荐
- hdu-5082
题意非常easy,就是给出父母的名字,然后依据父母的名字来给孩纸取名字! 能够将此题简化为: 孩纸的名字=父亲的frist name+字符串(_small_)+母亲额frist name; 然后将孩纸 ...
- servlet生成随机验证码
package com.cgyue; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import jav ...
- 从零开始Unity3D游戏开发【3烘焙】
烘焙:通过烘焙能把动态场景转化为静态场景.从而提高游戏的性能. [烘焙步骤] 1.Edit---Player---Rendering[forword] 2.Directional light(必须是这 ...
- <学习>.NET的反射基础
关键词 Assembly 使用Assembly定义和加载程序集,加载在程序集清单中列出模块,以及从此程序集中查找类型并创建该类型的实例. Module 通过它可以获取包含模块的程序集以及模块中的类等, ...
- IOS自学笔记1——学前准备
函数的声明和定义: 在标准的C编译器中,定义的函数只能调用前面已经定义的函数.若在main()函数里要调用其他方法,这时得首先在main()上面声明要调用的函数,即函数的声明. C语言中,函数的声明和 ...
- 关于Python网络爬虫实战笔记③
Python网络爬虫实战笔记③如何下载韩寒博客文章 Python网络爬虫实战笔记③如何下载韩寒博客文章 target:下载全部的文章 1. 博客列表页面规则 也就是, http://blog.sina ...
- postman下载和安装
插件下载地址:http://download.csdn.net/download/zhanghaofor/8244137 下载后解压缩,里面有安装方法 1.找到后缀为crx的文件,将后缀改成rar并解 ...
- codeforces 633G. Yash And Trees dfs序+线段树+bitset
题目链接 G. Yash And Trees time limit per test 4 seconds memory limit per test 512 megabytes input stand ...
- WireShark抓包时TCP数据包出现may be caused by ip checksum offload
最近用WireShark抓包时发现TCP数据包有报错:IP Checksum Offload,经过查阅资料终于找到了原因 总结下来就是wireshark抓到的数据包提示Checksum错误,是因为它截 ...
- delphi 7 下安装 indy 10.5.8 教程
本教程用 indy 10.5.8 替换 delphi 7 自带的 indy 版本,让大家深入了解 delphi 组件安装的方法. 第一步:下载 indy 10.5.8 组件,解压到合适的目录里.如 D ...