[tip]用jsp实现直接下载文件而不是在浏览器中打开的功能
程序员文章站
2022-08-10 21:30:40
<% // example: // download imag...
<% // 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 ) { }%>