struts2下载文件(extjs)
2012-04-28 17:03
363 查看
前台
window.open('downFileAction?fileName='+result.fileName);
// 下载文件
@Action(value = "downFileAction")//, results = { @Result(name = "success", type = "stream") })
public void downFile() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream os = null;
InputStream is = null;
String filePath = this.application.getRealPath("root"); //得到项目的web路径
try
{
File downloadFile = new File(filePath, this.fileName);
is = new FileInputStream(downloadFile);
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
this.response.reset();
this.response.setCharacterEncoding("utf-8");
this.response.setContentType("application/octet-stream");//文件类型contenttype
this.response.setHeader("Content-Disposition","attachment; filename=" + fileName); //关键部分,打开一个下载框
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = bis.read(buffer,0,8192)) != -1)
{
bos.write(buffer, 0, bytesRead);
}
bos.flush();
is.close();
bis.close();
os.close();
bos.close();
}
catch(Exception e){
e.printStackTrace();
}
}
// 下载文件
@Action(value = "downFileAction", results = { @Result(name = "success", type = "stream",
params = {"contentType", "application/octet-stream", "inputName", "xlsInputStream",
"contentDisposition", "filename=\"a.xls\""}) })
public String downFile() {
String filePath = this.application.getRealPath("root");
File downloadFile = new File(filePath, this.fileName);
try {
this.xlsInputStream = new FileInputStream(downloadFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return this.SUCCESS;
}
window.open('downFileAction?fileName='+result.fileName);
// 下载文件
@Action(value = "downFileAction")//, results = { @Result(name = "success", type = "stream") })
public void downFile() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream os = null;
InputStream is = null;
String filePath = this.application.getRealPath("root"); //得到项目的web路径
try
{
File downloadFile = new File(filePath, this.fileName);
is = new FileInputStream(downloadFile);
bis = new BufferedInputStream(is);
os = response.getOutputStream();
bos = new BufferedOutputStream(os);
this.response.reset();
this.response.setCharacterEncoding("utf-8");
this.response.setContentType("application/octet-stream");//文件类型contenttype
this.response.setHeader("Content-Disposition","attachment; filename=" + fileName); //关键部分,打开一个下载框
int bytesRead = 0;
byte[] buffer = new byte[8192];
while((bytesRead = bis.read(buffer,0,8192)) != -1)
{
bos.write(buffer, 0, bytesRead);
}
bos.flush();
is.close();
bis.close();
os.close();
bos.close();
}
catch(Exception e){
e.printStackTrace();
}
}
// 下载文件
@Action(value = "downFileAction", results = { @Result(name = "success", type = "stream",
params = {"contentType", "application/octet-stream", "inputName", "xlsInputStream",
"contentDisposition", "filename=\"a.xls\""}) })
public String downFile() {
String filePath = this.application.getRealPath("root");
File downloadFile = new File(filePath, this.fileName);
try {
this.xlsInputStream = new FileInputStream(downloadFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return this.SUCCESS;
}
相关文章推荐
- Extjs4 实现文件的下载(后台使用struts2实现)
- extjs3.4+struts2上传文件却弹出下载框,下载的文件内容为json 返回的值的问题
- java以流方式下载文件-----struts2.x版_心灵的港湾_百度空间
- struts2实现文件的上传(仅限单文件)和下载
- struts2 实现文件的上传 和 下载
- Struts2文件上传与下载
- 七、Struts2之文件上传与下载
- struts2 文件上传与下载原理 (转载 如有侵权 请联系我 删除)
- struts2文件下载出现Can not find a java.io.InputStream with the name的错误
- struts2文件下载(文件保存以BLOB类型在数据库中)
- 使用struts2文件下载时,execute()方法执行两次
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
- Struts2学习9--中文文件下载的问题
- Struts2学习9--文件下载
- struts2 实现文件下载
- Struts2(多)文件上传和下载使用示例
- Struts2中POI在内存中生成文件并下载
- 文件下载--Struts2的中文文件下载显示为空格问题
- Struts2文件的上传和下载
- struts2文件下载