TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
<%
// 有时候用户想要下载文件,但是这个文件类型在系统中与浏览器关联了,结果就变成在IE中打开。
// 常见的有word, excel, pdf等。因此将文件转成数据流让浏览器不知道其文件类型而达到下载的目的。
// 用法:
// <a href="download.jsp?path=img/&name=test.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 )
{
}
%>
源码下载:http://file.javaxxz.com/2014/10/1/005213750.zip |
|