Guest User

kaffeinecoma

a guest
Aug 4th, 2009
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package com.armhold.content;
  2.  
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.DataInputStream;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Date;
  15. import java.util.TimeZone;
  16.  
  17. /**
  18.  */
  19. public class ContentServlet extends HttpServlet {
  20.  
  21.     public static final int BUFSIZE = 8192;
  22.  
  23.     @Override
  24.     protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  25.         doDownload(res, mapRequestToFile(req));
  26.     }
  27.  
  28.  
  29.     @Override
  30.     protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  31.         doGet(req, res);
  32.     }
  33.  
  34.  
  35.     protected File mapRequestToFile(HttpServletRequest req) throws ServletException {
  36.  
  37.         String path = req.getParameter("resource");
  38.         if (path == null || path.equals("")) {
  39.             throw new ServletException("no resource parameter specified");
  40.         }
  41.  
  42.         if (path.contains("..")) throw new ServletException("illegal path");
  43.  
  44.         File contentDir = new File("content");
  45.         return new File(contentDir, path);
  46.     }
  47.  
  48.     protected void doDownload(HttpServletResponse res, File file) throws IOException {
  49.  
  50.         int length;
  51.         ServletOutputStream op = res.getOutputStream();
  52.         ServletContext context = getServletConfig().getServletContext();
  53.         String mimetype = context.getMimeType(file.getAbsolutePath());
  54.  
  55.         SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
  56.  
  57.         // force it to GMT because NSDateFormatter on iPhone does not totally
  58.         // support the standard
  59.         TimeZone gmtTime = TimeZone.getTimeZone("GMT");
  60.         df.setTimeZone(gmtTime);
  61.  
  62.         Date lastModified = new Date(file.lastModified());
  63.  
  64.         res.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
  65.         res.setContentLength((int) file.length());
  66.         res.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
  67.         res.setHeader("Last-Modified", df.format(lastModified));
  68.         res.setHeader("Last-Modified-in-millis", "" + file.lastModified());
  69.  
  70.         byte[] buf = new byte[BUFSIZE];
  71.         DataInputStream in = new DataInputStream(new FileInputStream(file));
  72.  
  73.         while ((length = in.read(buf)) != -1) {
  74.             op.write(buf, 0, length);
  75.         }
  76.  
  77.         in.close();
  78.         op.flush();
  79.         op.close();
  80.     }
  81.  
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment