用struts1.x实现文件上传、下载(文件存在文件系统中)
2009-08-06 18:36
776 查看
一、上传
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();
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();
相关文章推荐
- Struts2.3.5+Hibernate3+Spring3.1基于注解实现的多文件上传,下载
- 云计算学习笔记004---hadoop的简介,以及安装,用命令实现对hdfs系统进行文件的上传下载
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- struts 实现文件上传与下载
- Linux系统安装rz/sz实现本地和服务器文件上传下载
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- hadoop的FileSystem 文件系统实现上传下载文件
- struts 实现文件上传与下载
- Linux系统安装rz/sz实现本地和服务器文件上传下载
- Struts学习之 实现文件上传、下载操作
- Struts 2实现文件的上传下载(解除文件大小限制)
- struts1文件上传和下载 连接 使用Struts1实现文件上传(二)
- struts实现简单的文件上传与下载
- struts 实现文件上传与下载
- struts实现文件上传下载
- struts 实现文件上传与下载
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Ubuntu下通过sftp远程登录linux系统,并实现上传、下载文件