文件下载的其中之一种

来源:百度文库 编辑:神马文学网 时间:2024/04/27 18:21:45
<%
try
{
String path = request.getRealPath("/"); //取ROOT的当前目录
String filename=request.getParameter("filename");//取上一个页面传过来的文件名称
String str=CONV.toStr(request.getParameter("filePath"),"");//取上一个页面传过来的路径
str="F:/WEBROOT/EMS/"+str.substring(6);//组合出要下载文件的路径
String gMimetype = "";
Hashtable Ht = new Hashtable();
String name = "";
Ht.put(".doc", "application/msword");
Ht.put(".asf", "application/vnd.ms-asf");
Ht.put(".xls", "application/vnd.ms-excel");
Ht.put(".ppt", "application/vnd.ms-powerpoint");
Ht.put(".mmp", "application/vnd.ms-project");
Ht.put(".csv", "text/comma-separated-values");
Ht.put(".zip", "application/zip");
Ht.put(".rar", "application/rar");
String suffix = filename.substring(filename.lastIndexOf("."));
gMimetype = (String)Ht.get(suffix);
if(gMimetype == null || gMimetype.equals("") || gMimetype.equals("null"))
{
gMimetype = "application/octet-stream";
}
FileInputStream fileinputstream = new FileInputStream(str);//读取模块文件的内容
int lenght = fileinputstream.available();
byte bytes[] = new byte[lenght];
fileinputstream.read(bytes);
fileinputstream.close();
String templateContent = new String(bytes);
response.setContentType(gMimetype);
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
//注意, 上面的"java.net.URLEncoder.encode(filename, "UTF-8")"代码,能使中文名称的附件名不显示乱码!
out.write(templateContent);
out.flush();
out.close();
}
catch(Exception e)
{
out.print("异常");
out.print(e.toString());
}
%>