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上传下载的更多相关文章

  1. struts2 上传下载文件,异常处理,数据类型转换

    一,web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=" ...

  2. Struts2 上传下载

    一. 1.文件上传是web应用经常用到的一个知识.原理是,通过为表单元素设置enctype=”multipart/form-data”属性,让表单提交的数 据以二进制编码的方式提交,在接收此请求的Se ...

  3. Struts2实现文件上传下载功能(批量上传)

    今天来发布一个使用Struts2上传下载的项目, struts2为文件上传下载提供了好的实现机制, 首先,可以先看一下我的项目截图 关于需要使用的jar包,需要用到commons-fileupload ...

  4. JAVA Web 之 struts2文件上传下载演示(二)(转)

    JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...

  5. JAVA Web 之 struts2文件上传下载演示(一)(转)

    JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...

  6. Struts2 文件上传,下载,删除

    本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用Fi ...

  7. struts2.1.6教程九、文件上传下载(了解)

    首先建立struts2UpDownLoad项目,搭建好struts2基本的开发环境. 上传实例 步骤一:upload.jsp代码如下: <s:form action="upload&q ...

  8. Struts2学习(三)上传下载

    今天记录一下利用struts2实现上传下载,借此案例说明一下struts2的开发流程. 须要注意的是struts2版本号不同非常多地方的写法是不同的.本例使用struts2.3.15 .有差别的地方文 ...

  9. Struts2配合layui多文件上传--下载

    先说上传: 前台上传文件的js代码: var demoListView = $('#demoList') ,uploadListIns = upload.render({ elem: '#testLi ...

随机推荐

  1. 【DateStructure】 Charnming usages of Map collection in Java

    When learning the usage of map collection in java, I found serveral beneficial methods that was enco ...

  2. MATLAB一句总结

    MATLAB使用过程中的一些小总结: 1.sqrt函数的输入参数应为double类型: 2.im2bw把图像转换为二值图像: 3.double类型的图片必须转换为uint8类型后才能用imshow显示 ...

  3. VSS的配置和使用

    目录:   〇. 摘要一. 开发前的准备二. 创建空的SourceSafe数据库三. 新建项目并加入版本控制四. 获取SourceSafe中的项目五. 版本控制的几个概念六. 版本控制项目的管理七. ...

  4. iOS内购的订单对应和补单

    内购的关键类: 1.SKPayment(SKMutablePayment可将自己的参数一对一与苹果产生的payment对应起来) 2.TransactionObserver:交易状态更新时执行此方法, ...

  5. BZOJ 1176: [Balkan2007]Mokia( CDQ分治 + 树状数组 )

    考虑cdq分治, 对于[l, r)递归[l, m), [m, r); 然后计算[l, m)的操作对[m, r)中询问的影响就可以了. 具体就是差分答案+排序+离散化然后树状数组维护.操作数为M的话时间 ...

  6. hdu 4735Little Wish~ lyrical step~ 重复覆盖

    题目链接 给出一棵树, 树上点的值为0或1, 可以交换树上两个点的权值, 给出一个距离m, 所有的0距离最近的1的距离不能超过m, 求最少的交换次数. 首先对于每一个点u,所有离u的距离不超过m的点v ...

  7. [LeetCode]题解(python):142-Linked List Cycle II

    题目来源: https://leetcode.com/problems/linked-list-cycle-ii/ 题意分析: 给定一个链表,如果链表有环,返回环的起始位置,否则返回NULL.要求常量 ...

  8. pandas的札记

    导入导出数据 在导入,导出DataFrame数据时,会用到各种格式,分为 to_csv ;to_excel;to_hdf;to_sql;to_json;to_msgpack ;to_html;to_g ...

  9. Mac使用技巧

    外接显示器的生活,在 系统偏好设置--显示器--排列中,点击那个白色的条,可以设置主显示器.

  10. QT显示如何减轻闪屏(双缓冲和NoErase)

    很多同志在些QT 程序后会遇见闪屏的问题, 有时速度非常快,但毕竟影响了显示效果,如何做到减轻屏幕抖动或闪屏呢?我曾试过如下的办法:1.使用双缓冲. 比如我们在一个Widget里面绘多个图的话, 先创 ...