Link to home
Start Free TrialLog in
Avatar of richardsimnett
richardsimnett

asked on

How do I do file uploads in JSP?

Hello,
I have a form where a user is supposed to be able to upload a file of any size to my webserver. Once uploaded it should just store the file in a set path on the webserver, for processing by a bash script. How do I do accomplish this in JSP?

Here is my form:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<div align=center>
<table border=0 cellpadding=0 cellspacing=0 width=80% height=600px>
    <tr>
        <td valign=top>
            <h1>New Job</h1>
           
            <div align=center>
                <form action="manager.jsp?where=savejob" method=post enctype="multipart/form-data">
                <table border=0 cellpadding=0 cellspacing=0 80%>
                    <tr>
                        <td>Job Name:</td><td><input type=text size=25 maxlength=25 name=jobname></td>
                    </tr>
                    <tr>
                        <td>List:</td><td><input type=file size=25 name=list></td>
                    </tr>
                    <tr>
                        <td colspan=2 align=right><br><br><input type=submit value="Create Job"></td>
                    </tr>
                </table>
                </form>
            </div>
        </td>
    </tr>
</table>
</div>

My manager.jsp simply detects "where" it is and has this with no output when I test it.

 <c:when test="${where == 'savejob'}">
                    ${param.list}<br><br>
                    Job Saved.
 </c:when>


A good explanation, and examples are worth 500 points.

Thanks,
Rick
ASKER CERTIFIED SOLUTION
Avatar of rrz
rrz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of richardsimnett
richardsimnett

ASKER

rrz,
 Im actually getting errors when I try to test that... here is what Im getting...

org.apache.jasper.JasperException: Unable to compile class for JSP

Generated servlet error:
/home/rick/.netbeans/5.5.1/apache-tomcat-5.5.17_base/work/Catalina/localhost/pvw/org/apache/jsp/manager_jsp.java:8: package org.apache.commons.fileupload does not exist
import org.apache.commons.fileupload.*;
^


Generated servlet error:
/home/rick/.netbeans/5.5.1/apache-tomcat-5.5.17_base/work/Catalina/localhost/pvw/org/apache/jsp/manager_jsp.java:9: package org.apache.commons.fileupload.servlet does not exist
import org.apache.commons.fileupload.servlet.*;
^


Generated servlet error:
/home/rick/.netbeans/5.5.1/apache-tomcat-5.5.17_base/work/Catalina/localhost/pvw/org/apache/jsp/manager_jsp.java:10: package org.apache.commons.fileupload.disk does not exist
import org.apache.commons.fileupload.disk.*;
^


Generated servlet error:
/home/rick/.netbeans/5.5.1/apache-tomcat-5.5.17_base/work/Catalina/localhost/pvw/org/apache/jsp/manager_jsp.java:11: package org.apache.commons.fileupload.util does not exist
import org.apache.commons.fileupload.util.*;
^


I have already included the .jar file from the commons kit in the netbeans project... I dont know why it cant find the classes.... Any ideas?

Thanks,
Rick
rrz,
Actually I take that back I have the code working... sorta. It goes through the upload process, but a file never shows up in the fileUploads directory. How do I fix this? Here is what I get when I investigate that directory for the file:

rick@rick-desktop:~/pvw/web/fileUploads$ ls
rick@rick-desktop:~/pvw/web/fileUploads$

Thanks,
Rick
rrz,
Also, the output from the file is weird... here is what it kicks out

Form field jobname with value test2 detected.
File field MLIST.TXT with file name MLIST.TXT detected.
MLIST.TXT was uploaded to the fileUploads folder.

Shouldn't the very first MLIST.TXT be the field name? in this case list?


Thanks,
Rick
The example given by RRZ use jakarta commons filr upload so you need to have these jars in you Web-Inf/lib folder.

you can find these jars at

http://commons.apache.org/fileupload/
shivaspk,
yeah I got that fixed, it was just a matter of a rebuild in netbeans. The issue now is that the upload appears to work, except no file is ever written to the fileUploads directory.

Thanks,
Rick
>Shouldn't the very first MLIST.TXT be the field name? in this case list?  
There is no guarantee for order. It is up to browser.
Did you look in logs for any error message ?    
Did you put  commons-fileupload-1.2.jar  into  Tomcat's lib(for Tomcat 6, or common/lib for Tomcat 5) folder ?  
You could try it the following way.  
<%@ page import="java.util.*,
                 java.io.*,
                 org.apache.commons.fileupload.*,
                 org.apache.commons.fileupload.servlet.*,
                 org.apache.commons.fileupload.disk.*"%>
<%
   RequestContext reqContext = new ServletRequestContext(request);
   if(FileUploadBase.isMultipartContent(reqContext)){
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        List list;
        try{
            list = upload.parseRequest(request);
           } catch(FileUploadException fue){throw new ServletException(fue);}  
        for(int i = 0; i < list.size(); i ++) {
              FileItem item = (FileItem)list.get(i);
              if(item.isFormField()){
                                     //do something with them
              }else{
                   if(!item.getName().equals("")){
                   String fileName = new File(item.getName()).getName();
                   File file = new File(application.getRealPath("/fileUploads/" + fileName));
                   item.write(file);
                   out.print(fileName + " was uploaded to the fileUploads folder.<br/>");
                   }
              }
        }
   }else out.print("Wrong request.");
%>
rrz,
actually I found the files, they were being stored in my .war file in netbeans... is it supposed to do that? How do I get it to just write it to the local disk instead of the .war?

Thanks,
Rick
>How do I get it to just write it to the local disk instead of the .war?  
You can change the file path.  For example I am on windows and I can upload to C:\aaaa\test\   by using  
File file = new File("C:\\aaaa\\test\\" + fileName);  
If you are Linux, then  use something like
File file = new File("/usr/local/test/" + fileName);
rrz,
ahhhh. ok man thanks for all the help.

Rick