java安卓浏览器下载文件,JAVA实现文件下载,浏览器端得到数据没反应解决方案
JAVA實(shí)現(xiàn)文件下載,瀏覽器端得到數(shù)據(jù)沒反應(yīng)
代碼如下
public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)
throws?ServletException,?IOException?{
//得到要下載的文件名稱
String?filename=request.getParameter("filename");
//文件存放的路徑,合成絕對(duì)路徑
String?dir?=?this.getServletContext().getRealPath("/");
String?filepath=dir+"bksh"+"\\"+filename;
//得到這個(gè)文件的對(duì)象
File?f=new?File(filepath);
//response的編碼方式為.doc下載
response.setContentType("application/msword");
//寫明要下載的文件的大小
response.setContentLength((int)file.length());
//文件名
response.setHeader("Content-Disposition",?"attachment;?filename="?+?filename);
//獨(dú)處文件的IO流
FileInputStream?fis=new?FileInputStream(file);
BufferedInputStream?buff=new?BufferedInputStream(fis);
byte?[]?b=new?byte[1024];//相當(dāng)讀文件的緩存
long?k=0;//該值用于計(jì)算當(dāng)前實(shí)際下載了多少字節(jié)
//response對(duì)象得到輸出流
OutputStream?myout=response.getOutputStream();
//開始循環(huán)下載
while(k
int?j=buff.read(b,0,1024);
k+=j;
//將b中的數(shù)據(jù)寫到客戶端的內(nèi)存
myout.write(b,0,j);
}
//將寫入到客戶端的內(nèi)存的數(shù)據(jù),刷新到磁盤
myout.flush();
firbug顯示服務(wù)器已經(jīng)返回了數(shù)據(jù),但是貌似瀏覽器不認(rèn)為它需要下載這些數(shù)據(jù)。。。
求高人解答,在線等
------解決方案--------------------
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);
------解決方案--------------------
似乎少了這一句:
response.setContentType("multipart/form-data");
------解決方案--------------------
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.setCharacterEncoding("utf-8");
response.setContentType((mimetype?!=?null)???mimetype?:?"application/octet-stream");
response.setContentLength(workbook.getBytes().length);
response.setHeader("Content-Disposition",?"attachment;filename="?+?playdate?+?".xls");
唯一的明白人是這個(gè)哥們,要下載你得告訴瀏覽器,而不是發(fā)了數(shù)據(jù)就完事了。
response.setHeader("Content-type",?"application-download");
總結(jié)
以上是生活随笔為你收集整理的java安卓浏览器下载文件,JAVA实现文件下载,浏览器端得到数据没反应解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么python 为什么没有接口_py
- 下一篇: 前端处理带t的时间_适合家用的跑步机?阿