<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%><%
/*
从链接处传入path的参数如download.jsp?path=userfile/123.jpg,download.jsp为本页JSP
注意path是tomcat下的相对路径
*/
String temp=request.getParameter("path");
if(temp==null)
temp="";
String path=new String(temp.getBytes("8859_1"),"gb2312");
response.reset();
response.setContentType("application/x-download");
String filenamedownload = path;
String filenamedisplay = path.substring(path.lastIndexOf("/")+1,path.length());
filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
filenamedownload=request.getRealPath("/")+filenamedownload;
//System.out.println("filenamedownload:"+filenamedownload);
File file=new File(filenamedownload);
if (!file.exists()){
out.println("对不起,文件已删除");
return;
}
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = fis.read(b)) > 0)
{ output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
fis = null;
}
if(output != null)
{
output.close();
output = null;
}
}
%>
对于jsp中文名文件下载的解救方法!!
最新推荐文章于 2019-08-20 16:56:22 发布