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-da ta">
<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
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=
<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
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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/we b/fileUplo ads$ ls
rick@rick-desktop:~/pvw/we b/fileUplo ads$
Thanks,
Rick
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/we
rick@rick-desktop:~/pvw/we
Thanks,
Rick
ASKER
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
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/
you can find these jars at
http://commons.apache.org/fileupload/
ASKER
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
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.fileupl oad.*,
org.apache.commons.fileupl oad.servle t.*,
org.apache.commons.fileupl oad.disk.* "%>
<%
RequestContext reqContext = new ServletRequestContext(requ est);
if(FileUploadBase.isMultip artContent (reqContex t)){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory) ;
List list;
try{
list = upload.parseRequest(reques t);
} 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()).getNa me();
File file = new File(application.getRealPa th("/fileU ploads/" + fileName));
item.write(file);
out.print(fileName + " was uploaded to the fileUploads folder.<br/>");
}
}
}
}else out.print("Wrong request.");
%>
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
You could try it the following way.
<%@ page import="java.util.*,
java.io.*,
org.apache.commons.fileupl
org.apache.commons.fileupl
org.apache.commons.fileupl
<%
RequestContext reqContext = new ServletRequestContext(requ
if(FileUploadBase.isMultip
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory)
List list;
try{
list = upload.parseRequest(reques
} 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()).getNa
File file = new File(application.getRealPa
item.write(file);
out.print(fileName + " was uploaded to the fileUploads folder.<br/>");
}
}
}
}else out.print("Wrong request.");
%>
ASKER
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
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);
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);
ASKER
rrz,
ahhhh. ok man thanks for all the help.
Rick
ahhhh. ok man thanks for all the help.
Rick
ASKER
Im actually getting errors when I try to test that... here is what Im getting...
org.apache.jasper.JasperEx
Generated servlet error:
/home/rick/.netbeans/5.5.1
import org.apache.commons.fileupl
^
Generated servlet error:
/home/rick/.netbeans/5.5.1
import org.apache.commons.fileupl
^
Generated servlet error:
/home/rick/.netbeans/5.5.1
import org.apache.commons.fileupl
^
Generated servlet error:
/home/rick/.netbeans/5.5.1
import org.apache.commons.fileupl
^
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