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 ...
随机推荐
- D - Common Subsequence
D - Common Subsequence Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I ...
- Linux学习之挂载
linux的系统组织方式是——整个系统从根开始,按树形目录依次向下逐渐扩大,分类存放不同用途的文件,/读作“斜线”,英文slash:当其写作一个路径时,第一个/表示根,即root,其他的/表示路径分割 ...
- hadoop搭建杂记:Linux下hostname的更改办法
VirtualBox搭建hadoop伪分布式模式:更改hostname VirtualBox搭建hadoop伪分布式模式:更改hostname master: ip:192.168.56.120 机器 ...
- R与数据分析旧笔记(⑨)广义线性回归模型
广义线性回归模型 广义线性回归模型 例题1 R.Norell实验 为研究高压电线对牲畜的影响,R.Norell研究小的电流对农场动物的影响.他在实验中,选择了7头,6种电击强度, 0,1,2,3,4, ...
- 如何创建一个简单的struts2程序
如何创建一个简单的Struts2程序 “计应134(实验班) 凌豪” 1.创建一个新的Web项目test(File->new->Web Project) 2.Struts2框架的核心配置文 ...
- (转)深入浅出Java三大框架SSH与MVC的设计模式
原址:http://www.educity.cn/java/1382738.html 现在许许多多的初学者和程序员,都在趋之若鹜地学习Web开发的宝典级框架:Struts2, Spring,Hiber ...
- PHP开发APP接口
第1章 APP接口简介 - 课程简介 (:) - APP接口介绍 (:) - 客户端APP通信 (:) 最近学习 - 客户端APP通信格式区别 (:) - APP接口做的哪些事儿 (:) 第2章 封装 ...
- BOOL、sizeof
BOOL使用前需要声明 #include <stdbool.h>(这个头文件定义了bool,true,false等宏) int a[5]; sizeof(a[5]),sizeof是关键字, ...
- Visual Studio 常用快捷键总结
删除或剪切一行: Ctrl + X 或者 Shift+Delete格式化整个文档: Ctrl + K + D 或者 Ctrl+E+D智能感知: Ctrl + J 或者 Alt+→折叠所有方法: C ...
- 如何使用Palette提取Bitmap的颜色
5.X提出了color palette 的概念,能够让主题动态的去适应当前的页面色调,让整个app色调看起来比较和谐统一 那么如何使用Palette呢,必不可少,我们需要在Android studio ...