struts1实现上传下载

一、上传

1. JSP

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

<html:form action="upload.do" method="post" enctype="multipart/form-data">

<html:file property="file"></html:file>

<html:submit>上传</html:submit></html:form>

2. ActionForm类有FormFile属性

3. Action类

FileForm fileform=(FileForm)form;
FormFile file=fileform.getFile();
String filename=file.getFileName();
InputStream is=file.getInputStream();
String basePath=this.getServlet().getServletContext().getRealPath("/");
OutputStream os=new FileOutputStream(basePath+"upload/"+filename);
byte[] b=new byte[is.available()];
is.read(b);
os.write(b);
is.close();
os.flush();
os.close();

二、下载

//contentDisposition为要添加的数据。如果contentDisposition为null,则组件将自动添加"attachment;",以表明将下载的文件作为附件,结果是IE浏览器将会提示另存文件,而不是自动打开这个文件(IE浏览器一般根据下载的文件扩展名决定执行什么操作,扩展名为doc的将用word程序打开,扩展名为pdf的将用acrobat程序打开,等等)。

response.setContentType("application/x-msdownload");
String filename=request.getParameter("filename");
response.setHeader("Content-Disposition","attachment;"+"filename="+new String(filename.getBytes(), "ISO-8859-1"));
InputStream is=new FileInputStream("E:/tomcat6/webapps/smallweb/upload/"+filename);
byte[] b=new byte[is.available()];
is.read(b);
OutputStream os=response.getOutputStream();
os.write(b);
os.flush();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值