62,616
社区成员
发帖
与我相关
我的任务
分享
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<tr bgcolor="#FFFFFF" align="center">
<td align="left" colspan="2" width="" height="20px" nowrap="nowrap">
<img src="images/mail_affix.gif" alt="下载" style="cursor: hand">
<font color=navy>out.xml<a href="download.jsp?filePath=out.xls">下载</a>
</td>
</tr>
</body>
</html>
<%@ page contentType="text/html;charset=GBK" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.text.*"%>
<%! public static boolean checkFor304(HttpServletRequest req,File file){
if("no-cache".equalsIgnoreCase(req.getHeader("Pragma"))
|| "no-cache".equalsIgnoreCase(req.getHeader("cache-control"))){
}else{
String thisTag = Long.toString(file.lastModified());
String eTag = req.getHeader("If-None-Match");
if(eTag != null){
if(eTag.equals(thisTag)){
return true;
}
}
DateFormat rfcDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
Date lastModified = new Date(file.lastModified());
try{
long ifModifiedSince = req.getDateHeader("If-Modified-Since");
if(ifModifiedSince != -1){
long lastModifiedTime = lastModified.getTime();
if(lastModifiedTime <= ifModifiedSince){
return true;
}
}else{
try{
String s = req.getHeader("If-Modified-Since");
if(s != null){
Date ifModifiedSinceDate = rfcDateFormat.parse(s);
if(lastModified.before(ifModifiedSinceDate)){
return true;
}
}
}catch(ParseException e){
e.printStackTrace();
}
}
}catch(IllegalArgumentException e){
e.printStackTrace();
}
}
return false;
}%>
<%
//从配置文件中读取path
String realPath = "d:/";
//文件下载地址
String filePath = realPath + request.getParameter("filePath");
if(filePath != null){
filePath = new String(filePath.getBytes("iso-8859-1"),"gb2312");
}
boolean isInline = false;
out.clear();
response.reset();
try{
java.io.File f = new java.io.File(filePath);
if(f.exists() && f.canRead()){
if(checkFor304(request,f)){
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
String mimetype = null;
mimetype = application.getMimeType(filePath);
if(mimetype == null){
mimetype = "application/x-download;charset=ISO8859-1";
}
response.setContentType(mimetype);
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; //
if(isIE && !isInline){
mimetype = "application/x-msdownload";
}
//System.out.println("mimetype====================="+mimetype+"\n");
String downFileName = new String(f.getName().getBytes(),"ISO8859-1");
String inlineType = isInline ? "inline" : "attachment"; //
response.setHeader("Content-Disposition",inlineType + ";filename=\"" + downFileName + "\"");
response.setContentLength((int) f.length());
byte[] buffer = new byte[4096];
BufferedOutputStream output = null;
BufferedInputStream input = null;
try{
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(f));
int n = (-1);
while((n = input.read(buffer,0,4096)) > -1){
output.write(buffer,0,n);
}
response.flushBuffer();
}catch(Exception e){
e.printStackTrace();
}finally{
if(input != null) input.close();
if(output != null) output.close();
}
}else{
%>
<script>
alert("file not found or file not exists !");
</script>
<%
}
return;
}catch(Exception ex){
ex.printStackTrace();
}
response.sendError(404);
%>