FileDocCategorySizeDatePackage
ParserServlet.javaAPI DocExample2864Fri Jun 20 09:02:48 BST 2003com.jspservletcookbook

ParserServlet

public class ParserServlet extends HttpServlet

Fields Summary
private String
fileSavePath
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)

        
        throw new ServletException(
            "GET method used with " + getClass().getName()+
                 ": POST method required.");
    
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)


    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    
    out.println("<html>");
    out.println("<head>");
    out.println("<title>File uploads</title>");  
    out.println("</head>");
    out.println("<body>");

    out.println("<h2>Here is information about any uploaded files</h2>");

    try{

        // file limit size of five megabytes
        MultipartParser parser = new MultipartParser(
           request,5 * 1024 * 1024);

        Part _part = null;

        while ((_part = parser.readNextPart()) != null) {
       
           if (_part.isFile()) {

               // get some info about the file
               FilePart fPart = (FilePart) _part;
               String name = fPart.getFileName();
     
               if (name != null) {
  
               long fileSize = fPart.writeTo(
                 new java.io.File(fileSavePath));

               out.println("The user's file path for the file: " +
                 fPart.getFilePath() + "<br>");

               out.println("The content type of the file: " +
                 fPart.getContentType()+ "<br>");

               out.println("The file size: " +fileSize+ " bytes<br><br>");

              //commence with another file, if there is one

            } else {
  
               out.println(
                 "The user did not upload a file for this part.");

            }

          }    else if (_part.isParam()) {

              // do something else if it is a non-file type parameter,
              //such as a user name
          }
  
        }// end while


        out.println("</body>");
        out.println("</html>");

        out.close();

    } catch (java.io.IOException ioe){
       
       //an error-page in the deployment descriptor is 
       //mapped to the java.io.IOException
        throw new java.io.IOException(
            "IOException occurred in: " + getClass().getName());
}
    
public voidinit()

    
      // save uploaded files to a 'data' directory in the web app

      fileSavePath =   getServletContext().getRealPath("/") + "data";