<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%@include file="/WEB-INF/jspf/web/config.jspf"%> <%@page import="com.jspsmart.upload.SmartUpload"%> <%@page import="java.io.*,java.net.*"%> <% /*try{ // Usage: download.jsp?path=&file= // Initiate object. SmartUpload su = new SmartUpload(); // Download file name. // String fileName = StringTool.validString(request.getParameter("file")); String path = StringTool.validString(request.getParameter("path")); String file = StringTool.validString(request.getParameter("file")); String downloadfile = app_uploadpath+"/"+ (path.equals("")?"":path+"/") + file; // Whole path of download file. // String downloadfile = app_uploadpath+"/"+fileName; // Initialize. su.initialize(pageContext); // Download file. su.setContentDisposition(null); su.downloadFile(downloadfile); }catch(Exception e){ System.out.println("Error info:["+e+"]File name edit.jsp for [download]Time:["+DateTimeTool.dateTimeString()+"]"); out.println(""); }*/ String path = StringTool.validString(request.getParameter("path")); String fileName = StringTool.validString(request.getParameter("file")); String downloadfile = app_uploadpath+"/"+ (path.equals("")?"":path+"/") + fileName; //下載檔案. //String downloadfile = request.getRealPath("/uploads") + "/"+data_code+"/"+data_lang+"/"+fileName; java.io.File file = new java.io.File(downloadfile); if(file.exists() && file.isFile()){//檢驗檔案是否存在 /*-- synchronized(this){ // 更新下載次數 cp.setValue("cp_download",cp.getInt("cp_download")+1); cp.setUpdate("WebUser"); app_sm.update(cp); } --*/ try{ String mimetype = getServletConfig().getServletContext().getMimeType(downloadfile); response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" ); response.setHeader("Content-Disposition","attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20") + "\""); OutputStream output = response.getOutputStream(); InputStream in = new FileInputStream(file); byte[] b = new byte[2048]; int len; while((len = in.read(b))>0){ output.write(b,0,len); } in.close(); output.flush(); output.close(); //關閉串流 // 解決Connection rest by peer response.setStatus(response.SC_OK); response.flushBuffer(); // 解決 getOutputStream() has already been called for this response out.clear(); out = pageContext.pushBody(); /*------------------------------------------------------------ // smartupload下載功能 SmartUpload su = new SmartUpload(); // 不支援中文檔名 su.initialize(pageContext); su.setContentDisposition(null); //解決 getOutputStream() has already been called for this response out.clear(); out = pageContext.pushBody(); su.downloadFile(downloadfile); ------------------------------------------------------------*/ }catch(Exception ex){ response.setContentType("text/html; charset=UTF-8"); out.println(""); return; } }else{ out.println(""); return; } %>