D:\JavaFrameworks\InsuranceFramework\src\java\servlet\TestUploadMultipleDocuments.java
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 
  6 package servlet;
  7 
  8 /**
  9  *
 10  * @author sameldin
 11  */
 12 import java.io.*;
 13 import java.io.IOException;
 14 import java.io.FileOutputStream;
 15 import java.io.PrintWriter;
 16 
 17 import javax.servlet.ServletException;
 18 import javax.servlet.http.HttpServlet;
 19 import javax.servlet.http.HttpServletRequest;
 20 import javax.servlet.http.HttpServletResponse;
 21 
 22 import utils.*;
 23 import constants.*;
 24 
 25 public class TestUploadMultipleDocuments extends HttpServlet
 26 {
 27         public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 28         {
 29                 doPost(request, response);
 30         }
 31 
 32         public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
 33         {
 34                 response.setContentType("text/html");
 35             PrintWriter out = response.getWriter();
 36             out.println("<h2>Uploading Document Servlet</h2>");
 37             out.println("<BR><BR>");
 38 
 39 
 40             String path = "";
 41                 String uploadFolderIDField = (String)request.getParameter("Constants.UPLOAD_2_FOLDER_ID_FIELD");
 42                 if(null == uploadFolderIDField)
 43                         out.println("uploadFolderIDField = null");
 44                 else
 45                         out.println("uploadFolderIDField = " + uploadFolderIDField);
 46             out.close();
 47 
 48 //              try
 49 //              {
 50 //
 51 //                      boolean isMultipart = ServletFileUpload.isMultipartContent(request);
 52 //                      System.out.println("isMultipart :"+isMultipart);
 53 //
 54 //                      // Create a new file upload handler
 55 //                      ServletFileUpload upload = new ServletFileUpload();
 56 //
 57 //                      byte[] b = new byte[1024];
 58 //                      int x=0;
 59 //                      FileOutputStream buffer = null;
 60 //                      String fileName = "";
 61 //
 62 //                      ServletContext sc = this.getServletContext();
 63 //                      String FilePath = sc.getRealPath("/");
 64 //                      String destDirPath = FilePath;
 65 //
 66 //                      //Parse the request
 67 //                      FileItemIterator iter = upload.getItemIterator(request);
 68 //
 69 //                      InputStream stream = null;
 70 //                      while (iter.hasNext())
 71 //                      {
 72 //                              FileItemStream item = iter.next();
 73 //                              String name = item.getFieldName();
 74 //                              stream = item.openStream();
 75 //                              if (item.isFormField())
 76 //                              {
 77 //                                      System.out.println("Form field " + item.getName()
 78 //                                                      + " with value Streams.asString(stream) detected.");
 79 //                              }
 80 //                              else
 81 //                              {
 82 //                                      System.out.println("File field " + item.getName()
 83 //                                                      + " with file name item.getName() detected.");
 84 //                                      // Process the input stream
 85 //                                      fileName = item.getName();
 86 //                                      fileName = fileName.substring(fileName.lastIndexOf("\\") +1,fileName.length());
 87 //                                      System.out.println("fileName :"+ fileName);
 88 //                                      buffer = new java.io.FileOutputStream(destDirPath+fileName);
 89 //                                      while((x=stream.read(b,0,1024))>-1)
 90 //                                      {
 91 //                                              buffer.write(b,0,x);
 92 //                                      }
 93 //                                      buffer.close();
 94 //                              }
 95 //                              stream.close();
 96 //                      }
 97 //              }
 98 //              catch(Exception e)
 99 //              {
100 //                      e.printStackTrace();
101 //              }
102         }
103 
104 }
105 
106