|
<%
try { String path = request.getRealPath("/"); //取ROOT的當前目錄 String filename=request.getParameter("filename");//取上一個頁面?zhèn)鬟^來的文件名稱 String str=CONV.toStr(request.getParameter("filePath"),"");//取上一個頁面?zhèn)鬟^來的路徑
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()); } %> |
|
|