JAVA实现文件下载,浏览器端得到数据没反应

Uncloseable 2013-12-13 11:32:44
代码如下

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

//得到要下载的文件名称
String filename=request.getParameter("filename");
//文件存放的路径,合成绝对路径
String dir = this.getServletContext().getRealPath("/");
String filepath=dir+"bksh"+"\\"+filename;
//得到这个文件的对象
File f=new File(filepath);
//response的编码方式为.doc下载
response.setContentType("application/msword");
//写明要下载的文件的大小
response.setContentLength((int)file.length());
//文件名
response.setHeader("Content-Disposition", "attachment; filename=" + filename);

//独处文件的IO流
FileInputStream fis=new FileInputStream(file);
BufferedInputStream buff=new BufferedInputStream(fis);

byte [] b=new byte[1024];//相当读文件的缓存
long k=0;//该值用于计算当前实际下载了多少字节

//response对象得到输出流
OutputStream myout=response.getOutputStream();

//开始循环下载

while(k<file.length()){

int j=buff.read(b,0,1024);
k+=j;

//将b中的数据写到客户端的内存
myout.write(b,0,j);

}

//将写入到客户端的内存的数据,刷新到磁盘
myout.flush();

firbug显示服务器已经返回了数据,但是貌似浏览器不认为它需要下载这些数据。。。
求高人解答,在线等
...全文
2215 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
skystore 2016-08-17
  • 打赏
  • 举报
回复
解决了吗?求解!!!
baidu_32133009 2015-10-19
  • 打赏
  • 举报
回复
如何解决的啊
ruchiruzui2012 2015-07-07
  • 打赏
  • 举报
回复
引用 9 楼 u013136376 的回复:
[quote=引用 8 楼 jia20003 的回复:] [quote=引用 7 楼 u012463264 的回复:] response.setCharacterEncoding("utf-8"); response.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); response.setContentLength(workbook.getBytes().length); response.setHeader("Content-Disposition", "attachment;filename=" + playdate + ".xls");
唯一的明白人是这个哥们,要下载你得告诉浏览器,而不是发了数据就完事了。
response.setHeader("Content-type", "application-download");
[/quote] 加了这句,浏览器还是没有反应,response.setHeader()有什么顺序要求么? "application-download"是说这个servlet能够提供所有格式的文件下载么? 现在我想实现jpg格式的图片下载和DOC格式的文件下载,response要怎么设置?[/quote] 我也加了 也不好使,浏览器输出也有数据,就是没有下载提示,求解
windhawk777 2014-07-24
  • 打赏
  • 举报
回复
求解,如何解决的?
lt_shui3280 2014-06-11
  • 打赏
  • 举报
回复
请教如何解决的,我也遇到这个问题了
Rlay_2 2013-12-13
  • 打赏
  • 举报
回复
似乎少了这一句: response.setContentType("multipart/form-data");
Rlay_2 2013-12-13
  • 打赏
  • 举报
回复

response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
String downloadFileName = "";
if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
	downloadFileName = URLEncoder.encode(fileRealName, "UTF-8");
} else {
	downloadFileName = new String(fileRealName.getBytes("UTF-8"), "ISO8859-1");
}

response.setHeader("Content-disposition", "attachment; filename=" + downloadFileName);
Uncloseable 2013-12-13
  • 打赏
  • 举报
回复
好吧,没人来帮个忙么=。=
Uncloseable 2013-12-13
  • 打赏
  • 举报
回复
引用 8 楼 jia20003 的回复:
[quote=引用 7 楼 u012463264 的回复:] response.setCharacterEncoding("utf-8"); response.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); response.setContentLength(workbook.getBytes().length); response.setHeader("Content-Disposition", "attachment;filename=" + playdate + ".xls");
唯一的明白人是这个哥们,要下载你得告诉浏览器,而不是发了数据就完事了。
response.setHeader("Content-type", "application-download");
[/quote] 加了这句,浏览器还是没有反应,response.setHeader()有什么顺序要求么? "application-download"是说这个servlet能够提供所有格式的文件下载么? 现在我想实现jpg格式的图片下载和DOC格式的文件下载,response要怎么设置?
gloomyfish 2013-12-13
  • 打赏
  • 举报
回复
引用 7 楼 u012463264 的回复:
response.setCharacterEncoding("utf-8"); response.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); response.setContentLength(workbook.getBytes().length); response.setHeader("Content-Disposition", "attachment;filename=" + playdate + ".xls");
唯一的明白人是这个哥们,要下载你得告诉浏览器,而不是发了数据就完事了。
response.setHeader("Content-type", "application-download");
别闹腰不好 2013-12-13
  • 打赏
  • 举报
回复
response.setCharacterEncoding("utf-8"); response.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); response.setContentLength(workbook.getBytes().length); response.setHeader("Content-Disposition", "attachment;filename=" + playdate + ".xls");
Uncloseable 2013-12-13
  • 打赏
  • 举报
回复
愁死了,一个问题弄一天了,求解决TAT
Uncloseable 2013-12-13
  • 打赏
  • 举报
回复
引用 3 楼 Rlay_2 的回复:
似乎少了这一句: response.setContentType("multipart/form-data");
response.setContentType()不是确定输出流的格式么,还是输出什么文件都能用"multipart/form-data"
Uncloseable 2013-12-13
  • 打赏
  • 举报
回复


浏览器得到的数据。。。我觉得是编码问题?

81,094

社区成员

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

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