Results 1 to 3 of 3
  1. #1
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Simple upload servlet

    Hi

    I haven't been writing servlets for very long. I tried using the "org.apache.commons.fileupload" package but couldn't really get it to work. It would bomb at run-time. But at the same time, I think it is overkill for a simple file upload.

    Is there a simpler way to upload a file to spot on my web server? I am trying to upload PDFs. Just a simple one file upload. Or should I just go back and figure out why "fileupload" is not working?

    If curious the error is:
    java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileUploadException

    Thanks!

  2. #2
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Hi

    Apparently, **AGAIN**, it does not work in Eclipse. But had to add commons-io.jar to Tomcat which they should have included in the download.

    Now my problem is that I took the code from Apache and it is uploading the files but the printout shows that it is looping only once in the iteration:
    Java Code:
    			while(itr.hasNext()) {
    				
    				out.println( "Pass into loop"    );
    
    				FileItem item = (FileItem) itr.next();
    				/*
    				 * Handle Form Fields.
    				 */
    				if(item.isFormField()) {
    					out.println("Form field" );
    					out.println("File Name = "+item.getFieldName()+", Value = "+item.getString());
    				} else {
    					//Handle Uploaded files.
    					out.println("upload the files" );
    
    					out.println("Field Name   = "+item.getFieldName()   );
    					out.println("File Name    = "+item.getName()   );
    					out.println("Content type = "+item.getContentType()   );
    					out.println("File Size    = "+item.getSize()   );
    					out.println(     );
    					
    					/*
    					 * Write file to the ultimate location.
    					 */
    					File file = new File(destinationDir,item.getName());
    					item.write(file);
    				} //end if else
    				out.close();
    			} //end while
    And my output looks like this:
    Java Code:
    <h1>Servlet File Upload Example using Commons File Upload</h1>
    
    Pass into loop
    upload the files
    Field Name   = fileggg1
    File Name    = g.jpg
    Content type = image/jpeg
    File Size    = 2548

    So, it uploads 3 files but will only show the first filename. How can I get it to show all the uploaded files?

    Thanks!

  3. #3
    Gary20 is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default

    Figured it out. The Apache code is a little off:
    out.close();
    should be outside the while loop.

Similar Threads

  1. How to upload image in jsp/servlet
    By ibhaviik in forum Java Servlet
    Replies: 11
    Last Post: 11-16-2010, 11:32 AM
  2. Ajax / servlet File upload
    By n3mesi83 in forum Java Servlet
    Replies: 0
    Last Post: 06-08-2010, 02:48 PM
  3. upload and download file using servlet
    By amit_m04 in forum Java Servlet
    Replies: 1
    Last Post: 09-27-2009, 03:06 AM
  4. File Upload Servlet problem
    By jeniramires in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 07:34 PM
  5. how to upload a file using servlet
    By simon in forum Java Servlet
    Replies: 1
    Last Post: 08-05-2007, 03:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •