问题描述
StringfilePath=request.getSession().getServletContext().getRealPath("/upload/loanExcel/维保订单.xls");StringfileName=filePath.substring(filePath.lastIndexOf("\")+1);//得到文件名Filefile=newFile(filePath);if(file.exists()){ServletOutputStreamout=response.getOutputStream();response.reset();response.setContentType("application/x-download");//设置为下载application/x-download//response.addHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));response.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));BufferedInputStreambis=null;BufferedOutputStreambos=null;try{bis=newBufferedInputStream(newFileInputStream(filePath));bos=newBufferedOutputStream(out);byte[]buff=newbyte[8192];intbytesRead;while(-1!=(bytesRead=bis.read(buff,0,buff.length))){bos.write(buff,0,bytesRead);}bos.flush();}catch(IOExceptione){throwe;}finally{if(bis!=null)bis.close();if(bos!=null)bos.close();}}
解决方案
解决方案二:
需要用"window.location.href"这种get请求方式请求下载方法
解决方案三:
打断点跟踪一下代码,看代码走到哪一步了走完bos.flush();这一步,客户端应该出现弹出框代码走完,服务器断开与客户端的tcp连接时,客户端开始解析下载文件如果断点没走到后台,那就检查下请求下载的方式,看是否请求方式有问题
解决方案四:
你确认进到后台了?换个浏览器试试?
解决方案五:
一楼说的对,不能用ajax等方式去请求,这样是不会弹出框的~~~
解决方案六:
其实你都设置下载流了,直接用response.getOutputStream得到outputStream然后在这里面写入就可以了1.你到tomcat下面看看文件生成没有2.如果没有生成文件就是你的写入有问题3.如果你生成了下载的文件那就是你没有弹出框建议使用OutputStreamout=response.getOutputStream();response.reset();response.setHeader("content-disposition","attachment;filename=demo.xls");response.setContentType("application/msexcel");WritableWorkbookwwb=WritableWorkSheet.createWorkBook(out);WritableSheetsheet=wwb.createSheet("123",0);//写入数据out.flush();out.close();
我以前也遇到过,我是用的是window.location.hrefajax的方法结果不可以ajax没试过不知道可以不
解决方案七:
//将文件存到指定位置(服务器)Stringpath=ConstanData.LOAD_EXCEL;try{Filefile=newFile(path);if(!file.getParentFile().exists()){file.mkdirs();}path=path+"维保订单.xls";Filefile2=newFile(path);FileOutputStreamfout=newFileOutputStream(file2);wb.write(fout);fout.close();DownloadTooldownloadtool=newDownloadTool();response=ServletActionContext.getResponse();request=ServletActionContext.getRequest();//downloadtool.resetResponse(request,response,path);downloadtool.download(request,response,path);//downloadtool.download2(file2,response);}catch(Exceptione){e.printStackTrace();}//从服务器下载文件到本地机器publicvoiddownload(HttpServletRequestrequest,HttpServletResponseresponse,Stringlocation){try{StringfileName=location.substring(location.lastIndexOf("\")+1);//得到文件名Filefile=newFile(location);if(file.exists()){ServletOutputStreamout=response.getOutputStream();response.reset();response.setContentType("application/x-download");//设置为下载application/x-downloadfileName=newString(fileName.getBytes("GBK"),"ISO-8859-1");//response.addHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"UTF-8"));response.setHeader("Content-Disposition","attachment;filename="+fileName.substring(fileName.lastIndexOf("/")+1,fileName.length()));BufferedInputStreambis=null;BufferedOutputStreambos=null;try{bis=newBufferedInputStream(newFileInputStream(location));bos=newBufferedOutputStream(out);byte[]buff=newbyte[10240];intbytesRead;while(-1!=(bytesRead=bis.read(buff))){bos.write(buff);}bos.flush();}catch(IOExceptione){throwe;}finally{if(bis!=null)bis.close();if(bos!=null)bos.close();}}}catch(FileNotFoundExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}
解决方案八:
http://download.csdn.net/detail/u013112641/7375585看这个吧