日志文章

2008年05月29日 17:28:35

download.jsp   下载指向这个页面,并把要保存的文件名和文件路径参数给出

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<[url=mailto:%@page]%@page import="java.net.URLEncoder"%>
<%
out.clear();
response.reset();
String savename = request.getParameter("savename").replaceAll(" ","");
savename=URLEncoder.encode(savename,"UTF-8");
String filepath = request.getParameter("filepath");
response.setHeader("Content-Type", "application/force-download;");
response.setHeader("Content-disposition", "attachment;   filename="
  + savename + "");
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
if (filepath.indexOf("HotDataUploadFile") >= 0) {
try {
  bis = new java.io.BufferedInputStream(
  new java.io.FileInputStream(filepath));
  bos = new java.io.BufferedOutputStream(response
  .getOutputStream());
  byte[] buff = new byte[10 * 1024];
  int bytesRead;
  while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
  }
} catch (final java.io.IOException e) {
  System.out.println("文件下载出错! path: " + filepath);
} finally {
  if (bis != null)
bis.close();
  if (bos != null)
bos.close();
}
} else {
System.out.println("非法下载,路径中没有发现HotDataUploadFile!   "
+ filepath);
out.print("非法下载,路径中没有发现HotDataUploadFile");
}
%>

Tags: jsp 下载 附件  

类别: 无分类 |  评论(0) |  浏览(2885) |  收藏
发表评论
看不清楚,换一张