当前位置:编程学习 > JS >>

[tip]用jsp实现直接下载文件而不是在浏览器中打开的功能

<%    // example:    // <a href="/edu/UploadPic/2008-2/2008229133031460.gif">download image</a>
String root = getServletContext().getRealPath("/"); String path = request.getParameter("path"); String name = request.getParameter("name");
response.setContentType("unknown"); response.addHeader("Content-Disposition", "filename="" + name + """);
try { java.io.OutputStream os = response.getOutputStream(); java.io.FileInputStream fis = new java.io.FileInputStream(root + path + name);
byte[] b = new byte[1024]; int i = 0;
while ( (i = fis.read(b)) > 0 ) { os.write(b, 0, i); }
fis.close(); os.flush(); os.close(); } catch ( Exception e ) { }%>
补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,