<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.*"%>
<title>文檔下載保存中轉頁面</title>
</head>
<!--
*文件名:down_center.jsp
*實現功能:服務器上保存的文件名爲按上傳時間命名的
下載時自動替換爲中文文件名
*遺留問題:無格式判斷功能,有待開發
*編寫時間:2006-9-5
*代碼編寫:鄭兆童
*最終修改時間:2006-9-5
*最終修改內容:無
-->
<body>
<%
String root = getServletContext().getRealPath("/"); //得到網站絕對路徑
//String filepath = "upload_file\\lwfile\\"; //設置文件存放的相對路徑(windows)
String filepath = "upload_file/lwfile/"; //設置文件存放的相對路徑(linux)
String fileName = request.getParameter("filename"); //得到文件名
String myName = "中文文檔下載.doc"; //文件改名
out.print(root + filepath + fileName);
// 設置響應頭和下載保存的文件名
response.reset();
//response.setContentType("application/octet-stream"); //windows
//response.addHeader("Content-Disposition", "filename=\"" + myName + "\""); //windows
response.setContentType("application/octet-stream; charset=GBK"); //linux
response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(myName.getBytes("gb2312"),"iso8859-1") + "\""); //linux
//新建文件輸入輸出流
OutputStream output = null;
FileInputStream fis = null;
try{
//新建File對象
File f = new File(root + filepath + fileName);
//新建文件輸入輸出流對象
output = response.getOutputStream();
fis = new FileInputStream(f);
//設置每次寫入緩存大小
byte[] b = new byte[(int)f.length()];
//out.print(f.length());
//把輸出流寫入客戶端
int i = 0;
while((i = fis.read(b)) > 0){
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(fis != null){
fis.close();
fis = null;
}
if(output != null){
output.close();
output = null;
}
}
%>
</body>
</html>