欢迎来到天天文库
浏览记录
ID:40558877
大小:35.50 KB
页数:4页
时间:2019-08-04
《Jsp页面下载的两种方式》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1、采用RequestDispatcher的方式进行 jsp页面中添加如下代码:复制代码1.<%2. response.setContentType("application/x-download");//设置为下载application/x-download3. Stringfiledownload="/要下载的文件名";//即将下载的文件的相对路径4. Stringfiledisplay="最终要显示给用户的保存文件名";//下载文件时显示的文件保存名称5. filenamedisp
2、lay=URLEncoder.encode(filedisplay,"UTF-8");6. response.addHeader("Content-Disposition","attachment;filename="+filedisplay);7. 8. try9. ...{10. RequestDispatcherdis=application.getRequestDispatcher(filedownload);11. if(dis!=null)12.
3、...{13. dis.forward(request,response);14. }15. response.flushBuffer();16. }17. catch(Exceptione)18. ...{19. e.printStackTrace();20. }1. finally2. ...{3. 4. }5.%>2、采用文件流输出的方式下载复制代码1.<%@pagelanguage="j
4、ava"contentType="application/x-msdownload"pageEncoding="gb2312"%>2.<%3. //关于文件下载时采用文件流输出的方式处理:4. //加上response.reset(),并且所有的%>后面不要换行,包括最后一个;5.6. response.reset();//可以加也可以不加7. response.setContentType("application/x-download");8. Stringfiledownload="想办法找到
5、要提供下载的文件的物理路径+文件名";9. Stringfiledisplay="给用户提供的下载文件名";10. filedisplay=URLEncoder.encode(filedisplay,"UTF-8");11. response.addHeader("Content-Disposition","attachment;filename="+filedisplay);12.13. OutputStreamoutp=null;14. FileInputStreamin=null;15.
6、 try16. ...{17. outp=response.getOutputStream();1. in=newFileInputStream(filenamedownload);2.3. byte[]b=newbyte[1024];4. inti=0;5.6. while((i=in.read(b))>0)7. ...{8. outp.write(b,0,i);9. }10.
7、 outp.flush();11. }12. catch(Exceptione)13. ...{14. System.out.println("Error!");15. e.printStackTrace();16. }17. finally18. ...{19. if(in!=null)20. ...{21. in.close();22. in=null
8、;23. }24. if(outp!=null)25. ...{26
此文档下载收益归作者所有