站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题用jsp实现直接下载文件的功能

用jsp实现直接下载文件的功能

摘要: 用jsp实现直接下载文件的功能

</td> </tr> <tr> <td width="552" height="35" valign="top" class="ArticleTeitle"> <%
// 有时候用户想要下载文件,但是这个文件类型在系统中与浏览器关联了,结果就变成在IE中打开。
// 常见的有word, excel, pdf等。因此将文件转成数据流让浏览器不知道其文件类型而达到下载的目的。
// 用法:
// download image

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 )
{
}
%> </td> <td width="175" valign="top" class="ArticleTeitle">
</td> </tr> <tr> <td height="25" colspan="2" valign="top" class="ArticleTeitle">


↑返回目录
前一篇: 字符串分页代码
后一篇: 简单的计数统计器