<%@ page contentType="text/html; charset=gb2312" %> 
<%@ page import="java.io.*"%> 

<%! 
public String toUtf8String(String s) {
StringBuffer sb = new StringBuffer(); 
for (int i=0;i<s.length();i++) { 
char c = s.charAt(i); 
if (c >= 0 && c <= 255) { 
sb.append(c); 
} else { 
byte[] b; 
try { 
b = Character.toString(c).getBytes("utf-8"); 
} catch (Exception ex) { 
System.out.println(ex); 
b = new byte[0]; 
} 
for (int j = 0; j < b.length; j++) { 
int k = b[j]; 
if (k < 0) k += 256; 
sb.append("%" + Integer.toHexString(k). 
toUpperCase()); 
} 
} 
} 
return sb.toString(); 
} 
%> 
<% 

String download_file=request.getParameter("download_file");

String filename=download_file.substring(download_file.lastIndexOf("/")+1,download_file.length());

//out.print("filename"+filename);
String dir=  download_file.substring(0,download_file.lastIndexOf("/")); 
//out.print("dir"+dir);
String dirName=application.getRealPath(dir); 
java.io.File ff=null; 
String dd=dirName+System.getProperties().getProperty("file.separator")+filename; 
int filename_start=(filename.lastIndexOf(".")-30)<0?0:(filename.lastIndexOf(".")-20);
filename=filename.substring(filename_start,filename.length());
try{ 
ff=new java.io.File(dd); 
} 
catch(Exception e){ 
%>
<script language="javascript">
	alert('对不起,指定的文件不存在!');
	window.history.back();
</script>
<%
e.printStackTrace(); 
return ;
} 
if (ff!=null&&ff.exists()&&ff.isFile()) 
{ 
long filelength = ff.length(); 
InputStream inStream=new FileInputStream(dd); 
//设置输出的格式 
response.reset(); 
response.setContentType("application/x-msdownload"); 
response.setContentLength((int)filelength); 
response.addHeader("Content-Disposition","attachment; filename=\"" + toUtf8String(filename) + "\""); 
//循环取出流中的数据 
byte[] b = new byte[100]; 
int len; 
while((len=inStream.read(b)) >0) 
response.getOutputStream().write(b,0,len); 
inStream.close(); 

} else{
%>
<script language="javascript">
	alert('对不起,指定的文件不存在!');
	window.history.back();
</script>
<%
return ;	
}
%> 
<script language="JavaScript" type="text/JavaScript">
  
  window.close();
</script>