1 传入此jsp中的参数均已URLDencoder过。
2
3 <%@page import="java.net.URLEncoder"%>
4 <%@page import="java.io.BufferedOutputStream"%>
5 <%@ page contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
6 <%
7 boolean isError = false;
8 String errorMsg = "";
9 response.reset();//可以加也可以不加
10 request.setCharacterEncoding("UTF-8");
11 String folder = "news";
12 if(request.getParameter("folder")!=null){
13 folder = "upload/"+(String)request.getParameter("folder");
14 }
15 String newname = request.getParameter("newname");
16 // 是否允许直接在浏览器内打开
17 boolean isInline = false ;
18 out.clear();
19 response.reset();
20 String filePath = request.getRealPath("/"+folder)+"/" +newname;
21 java.io.BufferedOutputStream output = null ;
22 java.io.BufferedInputStream input = null ;
23 try {
24 java.io.File f = new java.io.File(filePath);
25 if(f.exists() && f.canRead()){
26 String mimetype = null;
27 mimetype = application.getMimeType( filePath );
28 if(mimetype==null){
29 mimetype = "application/octet-stream;charset=utf-8";
30 }
31 response.setContentType(mimetype);
32 // IE 的话就只能用 IE 才认识的头才能下载 HTML 文件, 否则 IE 必定要打开此文件!
33 String ua = request.getHeader("User-Agent"); //获取终端类型
34 if(ua==null ){ua = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;)";}
35 boolean isIE = ua.toLowerCase().indexOf("msie")!=- 1; //是否为 IE
36 if (isIE && !isInline) {
37 mimetype = "application/x-msdownload";
38 }
39 response.setContentType(mimetype);
40 // 是否内联附件
41 String inlineType = isInline?"inline":"attachment";
42 response.setHeader("Content-Disposition" , inlineType + ";filename=" + filename);
43 // 设置下载内容大小
44 response.setContentLength((int)f.length());
45 // 缓冲区
46 byte[] buffer = new byte[ 4096 ];
47 output = new java.io.BufferedOutputStream(response.getOutputStream());
48 input = new java.io.BufferedInputStream(new java.io.FileInputStream(f));
49 int n = (-1);
50 while((n=input.read(buffer,0,4096))>-1){
51 output.write(buffer,0,n);
52 }
53 response.flushBuffer();
54
55 }else{
56 isError = true;
57 errorMsg = "文件不存在!";
58 }
59 } catch(Exception ex) {
60 isError = true;
61 errorMsg = "您下载的文件出现异常!";
62 } finally {
63 if(isError){
64 String path = request.getHeader("Referer");
65 errorMsg=new String(errorMsg.getBytes("GBK"), "ISO8859_1");
66 %>
67 <script type="text/javascript">
68 alert('<%=errorMsg%>');
69 window.location.href='<%=request.getHeader("Referer")%>';
70 </script>
71 <%
72 }
73 if(input!=null ){input.close();}
74 if(output!=null ){output.close();}
75 out.clear();
76 out = pageContext.pushBody();
77 }
78 %>
备注:
public void downBatchRateModelFile(String fileName,String downName, HttpServletResponse response,HttpServletRequest request) {
try {
String downPath = TemplateBuilder.getInstance().getBatchRateModelFold()+File.separator+fileName;
// path是指欲下载的文件的路径。
File file = new File(downPath);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") )
.toLowerCase();
filename = downName+ext;
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(downPath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Length", "" + file.length());
String mimetype = "application/octet-stream;charset=utf-8";
String ua = request.getHeader("User-Agent"); // 获取终端类型
if (ua == null) {
ua = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;)";
}
boolean isIE = ua.toLowerCase().indexOf("msie") != -1; // 是否为 IE
if (isIE) {
mimetype = "application/x-msdownload";
response.addHeader("Content-Disposition", "attachment;filename="
+ java.net.URLEncoder.encode(filename, "UTF-8"));
}else{
response.addHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("GBK"),"ISO-8859-1"));
}
response.setContentType(mimetype);
//response.setContentType("application/x-msdownload");
OutputStream toClient = new BufferedOutputStream(
response.getOutputStream());
//response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException e) {
}
}