[tip]用jsp实现直接下载文件而不是在浏览器中打开的功能
程序员文章站
2022-05-03 21:14:51
<% // 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 ) { }%>
上一篇: 让温度进来