java 实现的下载功能,不能直接保存,请帮忙看看,路过有分

liubin041102 2010-12-07 09:11:34
调用下面的方法,返回pdf文件,弹出“打开”,“保存”按钮,点击打开按钮也是保存,不能直接打开,这个怎么解决

public void downFile(HttpServletResponse response){
response.setContentType("application/pdf");
response.setHeader("Location",downReportId+".pdf");
response.setHeader("Cache-Control", "max-age=" + 1000);//cacheTime
//filename应该是编码后的(utf-8)
response.setHeader("Content-Disposition", "attachment; filename=" + downReportId+".pdf");
//response.setContentLength(filelength);
OutputStream outputStream = null;
InputStream inputStream = null;
try{
outputStream = response.getOutputStream();
inputStream = new FileInputStream("/WEB-INF/view/reportAnalyze/requestProcess.pdf");
byte[] buffer = new byte[1024];
int i = -1;
while ((i = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, i);
}
outputStream.flush();
}catch (Exception e) {
System.out.println(e.getMessage());
}finally{
try{
outputStream.close();
inputStream.close();
}catch (Exception e) {
}
}
}
...全文
151 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
liubin041102 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 tubage408 的回复:]

InputStream in = request.getSession().getServletContext().getResourceAsStream("/WEB-INF/struts-config/struts-config.xml");
[/Quote]
高手啊,测试了下好用,谢谢
liubin041102 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 chenjl_mail 的回复:]

this.getRes().reset();
this.getRes().setContentType("application/vnd.ms-excel;charset=gb2312");
this.getRes().setHeader("Content-Disposition", "attachment; filename=\""
+ new String("生产厂商统计表.pdf".……
[/Quote]

Content-Disposition:attachment
这样设置 在IE里还是不能直接打开文件,
kinfinger 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 liubin041102 的回复:]
response.setHeader("Content-Disposition", "attachment; filename=" + downReportId+".pdf");
# Content-disposition 为属性名。
# attachment 表示以附件方式下载。如果要在页面中打开,则改为 inline。

我用attachment,在firefox中可以打开,也可……
[/Quote]
我也遇见同样的原因,很是郁闷啊
tubage408 2010-12-07
  • 打赏
  • 举报
回复
response.setHeader("Cache-Control", "max-age=" + 1000);//cacheTime
这个去掉
tubage408 2010-12-07
  • 打赏
  • 举报
回复
InputStream in = request.getSession().getServletContext().getResourceAsStream("/WEB-INF/struts-config/struts-config.xml");
liang070623 2010-12-07
  • 打赏
  • 举报
回复
\WEB-INF\view\reportAnalyze\requestProcess.pdf 改成\view\reportAnalyze\requestProcess.pdf 怎么样 看能不能找到
CoderPlusPlus 2010-12-07
  • 打赏
  • 举报
回复
response.setHeader("Content-Disposition", "attachment; filename=" + downReportId+".pdf"); 

改成
response.setHeader("Content-Disposition", "inline; filename=" + downReportId+".pdf"); 
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
response.setHeader("Content-Disposition", "attachment; filename=" + downReportId+".pdf");
# Content-disposition 为属性名。
# attachment 表示以附件方式下载。如果要在页面中打开,则改为 inline。

我用attachment,在firefox中可以打开,也可以保存,但是在IE中 只能保存,不能直接打开,这个怎么处理
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
绝对路径 是可以的
liang070623 2010-12-07
  • 打赏
  • 举报
回复
你自己尝试改变下路径啊 记得路径应该改成双斜杠吧 试下不行看看绝对路径
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
谢谢三楼,按照你说说的方式, 还是不行
java.io.FileNotFoundException: \WEB-INF\view\reportAnalyze\requestProcess.pdf (系统找不到指定的路径。)
liang070623 2010-12-07
  • 打赏
  • 举报
回复
inputStream = new FileInputStream(new File("/WEB-INF/view/reportAnalyze/requestProcess.pdf"));
改成这样吧 看看行不行
liang070623 2010-12-07
  • 打赏
  • 举报
回复
inputStream = new FileInputStream(new File()"/WEB-INF/view/reportAnalyze/requestProcess.pdf"));
改成这样吧 看看行不行
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
我知道问题应该出现在:
response.setContentType("application/pdf");
response.setHeader("Location",downReportId+".pdf");
response.setHeader("Cache-Control", "max-age=" + 1000);//cacheTime
//filename应该是编码后的(utf-8)
response.setHeader("Content-Disposition", "attachment; filename=" + downReportId+".pdf");

response 的header 设置上,应该怎么设置
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
还有一个问题,/WEB-INF/view/reportAnalyze/requestProcess.pdf 这个路径需要 写成绝对路径才能 下载成功,怎么解释
tubage408 2010-12-07
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 liubin041102 的回复:]
按照10楼修改,还是找不到文件,

11楼的做法我现在环境 实验不了,

多谢各位
[/Quote]
如何实验不了呀?还是找不到文件???
chenjl_mail 2010-12-07
  • 打赏
  • 举报
回复
this.getRes().reset();
this.getRes().setContentType("application/vnd.ms-excel;charset=gb2312");
this.getRes().setHeader("Content-Disposition", "attachment; filename=\""
+ new String("生产厂商统计表.pdf".getBytes("GBK"),"ISO-8859-1") + "\"");
OutputStream output = this.getRes().getOutputStream();
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
按照10楼修改,还是找不到文件,

11楼的做法我现在环境 实验不了,

多谢各位
liubin041102 2010-12-07
  • 打赏
  • 举报
回复
按照9楼说的那样做,只能直接打开,就不支持下再或者另存为,

请问12楼,response.setHeader("Cache-Control", "max-age=" + 1000);//cacheTime
这个是什么意思,我去掉了,和之前效果一样


81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧