1 22 23 package org.meshcms.core; 24 25 import java.io.*; 26 import javax.servlet.*; 27 import javax.servlet.http.*; 28 import org.meshcms.util.*; 29 30 33 public final class DownloadServlet extends HttpServlet { 34 40 protected void doGet(HttpServletRequest request, HttpServletResponse response) 41 throws ServletException, IOException { 42 processRequest(request, response); 43 } 44 45 51 protected void doPost(HttpServletRequest request, HttpServletResponse response) 52 throws ServletException, IOException { 53 processRequest(request, response); 54 } 55 56 62 protected void processRequest(HttpServletRequest request, HttpServletResponse response) 63 throws IOException { 64 WebSite webSite = (WebSite) request.getAttribute("webSite"); 65 Path path = new Path(request.getPathInfo()); 66 67 if (webSite.isSystem(path)) { 68 69 UserInfo userInfo = (UserInfo) 70 request.getSession(true).getAttribute("userInfo"); 71 72 if (userInfo == null || userInfo.isGuest()) { 73 response.sendError(HttpServletResponse.SC_NOT_FOUND); 74 return; 75 } 76 } 77 78 File file = webSite.getFile(path); 79 80 if (!file.exists()) { 81 response.sendError(HttpServletResponse.SC_NOT_FOUND, 82 "File not found on server"); 83 return; 84 } 85 86 String fileName = request.getParameter("filename"); 87 88 if (Utils.isNullOrEmpty(fileName)) { 89 fileName = path.getLastElement(); 90 } 91 92 try { 93 InputStream is = new FileInputStream(file); 94 response.setContentType("application/x-download"); 95 response.setHeader("Content-Disposition", "attachment; filename=\"" + 96 fileName + "\""); 97 response.setHeader("Content-Length", Long.toString(file.length())); 98 Utils.copyStream(is, response.getOutputStream(), false); 99 } catch (IOException ex) { 100 if (!response.isCommitted()) { 101 response.resetBuffer(); 102 response.sendError(HttpServletResponse.SC_NOT_FOUND, 103 "File not found on server"); 104 } 105 } 106 } 107 } 108 | Popular Tags |