pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

pastebin private pastebin - collaborative debugging tool What's a private pastebin?


Posted by eidolon on Sat 3rd May 23:14 (modification of post by view diff)
diff | download | new post

  1. package com.areteinc.servlets;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5.  
  6. import java.util.Iterator;
  7. import java.util.List;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. import org.apache.commons.fileupload.FileItem;
  15. import org.apache.commons.fileupload.FileItemFactory;
  16. import org.apache.commons.fileupload.FileUploadException;
  17. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  18. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  19. import org.apache.log4j.Level;
  20. import org.apache.log4j.Logger;
  21.  
  22. public class Filer extends HttpServlet {
  23.        
  24.         private Logger logger = Logger.getLogger(Filer.class);
  25.        
  26.         public Filer() {
  27.                 logger.setLevel(Level.DEBUG);
  28.         }
  29.        
  30.         protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  31.                 logger.debug("Serving up a GET page...");
  32.                 PrintWriter writer = resp.getWriter();
  33.                 StringBuffer response = new StringBuffer();
  34.                 response.append("<HTML><HEAD><TITLE>JENA File Uploader</TITLE></HEAD><BODY>");
  35.                 response.append("<FORM action=\"Filer\" method=\"POST\" enctype=\"multipart/form-data\">");
  36.                 response.append("Upload file: <input type=\"file\" name=\"file1\"/><br>");
  37.                 response.append("Upload file: <input type=\"file\" name=\"file2\"/><br>");
  38.                 response.append("<input type=submit value=\"Start upload\">");
  39.                 response.append("</BODY>");
  40.                 writer.println(response);
  41.         }
  42.        
  43.         protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  44.                 // First see if someone is uploading more than one file at a time...
  45.                 boolean isMultipart = ServletFileUpload.isMultipartContent(req);
  46.                
  47.                 logger.debug("Received a POST request.  Multipart is flagged as " + isMultipart);
  48.                
  49.                 // Create a factory for disk-based file items
  50.                 FileItemFactory factory = new DiskFileItemFactory();
  51.  
  52.                 // Create a new file upload handler
  53.                 ServletFileUpload upload = new ServletFileUpload(factory);
  54.  
  55.                 // Parse the request
  56.                 try {
  57.                         List<FileItem> items = upload.parseRequest(req);
  58.                         Iterator itr = items.iterator();
  59.                         logger.debug("Size of upload is " + items.size() + " items.");
  60.                         while(itr.hasNext()) {
  61.                                 FileItem item = (FileItem) itr.next();
  62.                                 logger.debug("Filename is " + item.getName());
  63.                         }
  64.                 } catch (FileUploadException e) {
  65.                         e.printStackTrace();
  66.                 }
  67.         }
  68. }
  69.  
  70.  
  71. -----------
  72. Run, with 2 files selected to upload:
  73. 13:50:15,421 DEBUG [Filer] Received a POST request.  Multipart is flagged as true
  74. 13:50:15,421 DEBUG [Filer] Size of upload is 0 items.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me