Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Uploading file and send data to servlet or jsp page

Status
Not open for further replies.

whitesox12

Programmer
May 2, 2006
9
US
How can I upload the file content into a textarea on a HTML form. Or how can I directly upload a file and post it to a servlet. On my form I want to have a upload button where a user browse and select a file once selected wanted to post the file to a servlet. Any help is appreciated.

Thanks
 
Here is the sample code I am trying to run from but keep getting the errors:

Code:
Listing 9-4. UploadFiles.html
<HTML>
  <HEAD>
    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"/>
    <TITLE>File Upload Page</TITLE>
  </HEAD>
  <BODY>Upload Files
    <FORM name="filesForm" action="ProcessFileUpload.jsp"
    method="post" enctype="multipart/form-data">
        File 1:<input type="file" name="file1"/><br/>
        File 2:<input type="file" name="file2"/><br/>
        File 3:<input type="file" name="file3"/><br/>
        <input type="submit" name="Submit" value="Upload Files"/>
    </FORM>
  </BODY>
</HTML>



Listing 9-5. ProcessFileUpload.jsp
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="org.apache.commons.fileupload.DiskFileUpload"%>
<%@ page import="org.apache.commons.fileupload.FileItem"%>
<%@ page import="java.util.List"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="java.io.File"%>
html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Process File Upload</title>
</head>
<%
        System.out.println("Content Type ="+request.getContentType());

        DiskFileUpload fu = new DiskFileUpload();
        // If file size exceeds, a FileUploadException will be thrown
        fu.setSizeMax(1000000);

        List fileItems = fu.parseRequest(request);
        Iterator itr = fileItems.iterator();

        while(itr.hasNext()) {
          FileItem fi = (FileItem)itr.next();

          //Check if not form field so as to only handle the file inputs
          //else condition handles the submit button input
          if(!fi.isFormField()) {
            System.out.println("\nNAME: "+fi.getName());
            System.out.println("SIZE: "+fi.getSize());
            //System.out.println(fi.getOutputStream().toString());
            File fNew= new File(application.getRealPath("/"), fi.getName());

            System.out.println(fNew.getAbsolutePath());
            fi.write(fNew);
          }
          else {
            System.out.println("Field ="+fi.getFieldName());
          }
        }
%>
<body>
Upload Successful!!
</body>
</html>

but I keep getting the following error:
Code:
500 Internal Server Error
java.lang.NoClassDefFoundError: org/apache/commons/io/FileCleaner	at org.apache.commons.fileupload.disk.DiskFileItem.getTempFile(DiskFileItem.java:579)	at org.apache.commons.fileupload.disk.DiskFileItem.getOutputStream(DiskFileItem.java:519)	at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:369)	at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:268)	at _fileUpload._jspService(fileUpload.jsp:19)	[SRC:/fileUpload.jsp]	at com.orionserver[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].http.OrionHttpJspPage.service(OrionHttpJspPage.java:57)	at oracle.jsp.runtimev2.JspPageTable.service(JspPageTable.java:347)	at oracle.jsp.runtimev2.JspServlet.internalService(JspServlet.java:509)	at oracle.jsp.runtimev2.JspServlet.service(JspServlet.java:413)	at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].server.http.ResourceFilterChain.doFilter(ResourceFilterChain.java:65)	at oracle.security.jazn.oc4j.JAZNFilter.doFilter(Unknown Source)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].server.http.ServletRequestDispatcher.invoke(ServletRequestDispatcher.java:649)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].server.http.ServletRequestDispatcher.forwardInternal(ServletRequestDispatcher.java:322)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].server.http.HttpRequestHandler.processRequest(HttpRequestHandler.java:790)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].server.http.HttpRequestHandler.run(HttpRequestHandler.java:270)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].server.http.HttpRequestHandler.run(HttpRequestHandler.java:112)	at com.evermind[Oracle Application Server Containers for J2EE 10g (10.1.2.0.0)].util.ReleasableResourcePooledExecutor$MyWorker.run(ReleasableResourcePooledExecutor.java:192)	at java.lang.Thread.run(Thread.java:534)

Can someone tell what am I doing wrong. I have commons-fileupload-1.1.jar in my classpath. I even tried someother samples but keeps getting the same error
i.e. parseRequest(request); I really want to upload an xml file only and read it's content from memory in the servlet and do the processing. Any help is appreciated.

Thanks
 
Is the class org/apache/commons/io/FileCleaner in the commons-fileupload.jar ? If it is, then you need to put that jar file in the servlet container's lib directory. If it is not, you need to find what jar file it is in, and add that to the lib directory.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top