Afterlife
asked on
JSP image upload
Need someone to point me in the direction of a image upload tutorial or something along those lines for JSP pages.
- please note that I do not need anything to do with taglibs, struts or anything of this nature, just JSP/Servlets will be fine, maybe even java beans.
- i dont want the mimeparser from oop-research.com, nor the Oreiily app for doing this.
What I want to do at the end of the day, is upload image(s) onto a server, store the imagename with its path in the database, and then be able to pull the database image paths and display, the latter being farily simplistic I think, its getting the images uploaded. Ive searched google for about an hour, and found nothing but pre made api's, taglibs, or something other I either have to pay for or get licensed. PHP and ASP.NET seem to have an abundance of help on this topic, Java has very little.
If someone could help me do this, point me in the right direction, I would be grateful.
If this is simply too much work, and someone knows an easy way using a free way (plz no struts, something rather simplistic still, like maybe a taglib but not jstl 2.0) then please post that and I will consider it.
Thankyou in advance
- please note that I do not need anything to do with taglibs, struts or anything of this nature, just JSP/Servlets will be fine, maybe even java beans.
- i dont want the mimeparser from oop-research.com, nor the Oreiily app for doing this.
What I want to do at the end of the day, is upload image(s) onto a server, store the imagename with its path in the database, and then be able to pull the database image paths and display, the latter being farily simplistic I think, its getting the images uploaded. Ive searched google for about an hour, and found nothing but pre made api's, taglibs, or something other I either have to pay for or get licensed. PHP and ASP.NET seem to have an abundance of help on this topic, Java has very little.
If someone could help me do this, point me in the right direction, I would be grateful.
If this is simply too much work, and someone knows an easy way using a free way (plz no struts, something rather simplistic still, like maybe a taglib but not jstl 2.0) then please post that and I will consider it.
Thankyou in advance
Another comment:
If you want to return the filename and path value in the fucntion (to save it in the database), you only have to add this line at the end of the function:
return Path+saveFile;
And also correct the return value from "void" to "String"
-tom
If you want to return the filename and path value in the fucntion (to save it in the database), you only have to add this line at the end of the function:
return Path+saveFile;
And also correct the return value from "void" to "String"
-tom
ASKER
Thanks Tom for the quick reply, I will give this a test when I am home tonight, if all works as hoped points will be yours. Just a few quick additions, if you have the time would appreciate an answer, tho they do not pertain to you getting the point for this.
For expanding on this, is it possible to set the maximum size of the file, restirct the type of file uploaded, and width/height properties max. I am looking for something along the lines of how some forums have the upload avatar, where it checks if file is appropriate size/dimensions/type. Seems JSP is harder to do things like this than php etc.
Is this the best way for doing an upload? If you know of easier/better ways please point me in the direction of learning about these.
Also where did you figure out how to do the above, a book? forum?
I am trying to learn j2ee technologies at a comfortable pace, but finding the resources on the net lacking compared to that of asp or php.
Do you know of any links that would be helpful, for tutorials on learning this stuff, any real good books I should take a look at, forums etc.
phpfreaks is probably the best php site out there and was hoping for a jsp equivalent.
For expanding on this, is it possible to set the maximum size of the file, restirct the type of file uploaded, and width/height properties max. I am looking for something along the lines of how some forums have the upload avatar, where it checks if file is appropriate size/dimensions/type. Seems JSP is harder to do things like this than php etc.
Is this the best way for doing an upload? If you know of easier/better ways please point me in the direction of learning about these.
Also where did you figure out how to do the above, a book? forum?
I am trying to learn j2ee technologies at a comfortable pace, but finding the resources on the net lacking compared to that of asp or php.
Do you know of any links that would be helpful, for tutorials on learning this stuff, any real good books I should take a look at, forums etc.
phpfreaks is probably the best php site out there and was hoping for a jsp equivalent.
ASKER
Would using the "The Commons FileUpload package by Jakarta" be easier than trying to do this myself?
found this page on solutions
http://jguru.com/faq/view.jsp?EID=160
found this page on solutions
http://jguru.com/faq/view.jsp?EID=160
I don't think so... but if you try, please write me and let me know.
The code that I posted is working for my project and it was tested a lot.
I don't know if my approach is the best way to do it, however I can tell you that it works for sure. =)
About the restrictions (file type/size) I'm still working on that.
If you are looking for books on J2SE or J2EE/JSP/JavaScript, the best are in: www.wrox.com
The series "Beginning Java 2" by Ivor Horton are excellent for your pace! =)
-tom
The code that I posted is working for my project and it was tested a lot.
I don't know if my approach is the best way to do it, however I can tell you that it works for sure. =)
About the restrictions (file type/size) I'm still working on that.
If you are looking for books on J2SE or J2EE/JSP/JavaScript, the best are in: www.wrox.com
The series "Beginning Java 2" by Ivor Horton are excellent for your pace! =)
-tom
ASKER
Thanks tom, I'm going to test your file upload soon. I am very intrested in further development you make on your file upload, is there anyway we can keep in contact outside of ee? thanks.
Of course,
here is my email address: tomgallo@speedy.com.ar feel free to write me.
-tom
here is my email address: tomgallo@speedy.com.ar feel free to write me.
-tom
ASKER
Ok tried using the upload thing you posted.
so this is what I have basically
<form method="post" action="eventServlet" enctype="multipart/form-da ta">
<table>
<tr>
<td>Event: <input size="50" type="text" name="event" /></td>
</tr>
<tr>
<td>Description: <textarea cols="50" rows="10" name="description"></texta rea><br /><br /></td>
</tr>
<tr>
<td>Flyer/Image<input type="file" size="40" name="filename" value="Browse" /><br /><br /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Upload File" /></td>
</tr>
</table>
<input type="hidden" name="action" value="upload" />
</form>
so then in the servlet i do this
String action = request.getParameter("acti on");
String submit = request.getParameter("subm it");
String address;
out.println("action is: " + action);
out.println("submit is: " + submit);
now it should print the values, but instead prints null...
i took out the
<tr>
<td>Flyer/Image<input type="file" size="40" name="filename" value="Browse" /><br /><br /></td>
</tr>
from the form and it works, but when i add back in it doesnt. Any ideas? I need the values as a way of checking what the servlet should do.
thanks
so this is what I have basically
<form method="post" action="eventServlet" enctype="multipart/form-da
<table>
<tr>
<td>Event: <input size="50" type="text" name="event" /></td>
</tr>
<tr>
<td>Description: <textarea cols="50" rows="10" name="description"></texta
</tr>
<tr>
<td>Flyer/Image<input type="file" size="40" name="filename" value="Browse" /><br /><br /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Upload File" /></td>
</tr>
</table>
<input type="hidden" name="action" value="upload" />
</form>
so then in the servlet i do this
String action = request.getParameter("acti
String submit = request.getParameter("subm
String address;
out.println("action is: " + action);
out.println("submit is: " + submit);
now it should print the values, but instead prints null...
i took out the
<tr>
<td>Flyer/Image<input type="file" size="40" name="filename" value="Browse" /><br /><br /></td>
</tr>
from the form and it works, but when i add back in it doesnt. Any ideas? I need the values as a way of checking what the servlet should do.
thanks
Yes!
Beacuse the form is a enctype="multipart/form-da ta".
You have to use different forms to do that... the form for the file, and the form for the hidden
Try this out!
-tom
Beacuse the form is a enctype="multipart/form-da
You have to use different forms to do that... the form for the file, and the form for the hidden
Try this out!
-tom
Another comment:
To call several submits in one click you have to do a simple Javascript function like this:
function doUpload(){
document.frmOne.submit();
document.frmTwo.submit();
}
One thing to keep in mind is that you need now two different servlets, one for the upload, the other for the additional data (you can use one, but is not the best practice in this case).
Also you can execute the javascript function in the onClick event of the submit input.
Hope it helps,
-tom
To call several submits in one click you have to do a simple Javascript function like this:
function doUpload(){
document.frmOne.submit();
document.frmTwo.submit();
}
One thing to keep in mind is that you need now two different servlets, one for the upload, the other for the additional data (you can use one, but is not the best practice in this case).
Also you can execute the javascript function in the onClick event of the submit input.
Hope it helps,
-tom
ASKER
Hey tom, for now just trying to get it to upload to a certain folder.
I did this. Servlet grabs the form data, and then instantly calls your doUpload with this
doUpload(request, "/images/"); // as images is the folder I want to store the file in.
anyways the exception im getting is this
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.io.FileNotFoundExcept ion: /images/test.txt (No such file or directory)
at java.io.FileOutputStream.o pen(Native Method)
at java.io.FileOutputStream.( FileOutput Stream.jav a:179)
at java.io.FileOutputStream.( FileOutput Stream.jav a:70)
at eventServlet.doUpload(even tServlet.j ava:132)
at eventServlet.doPost(eventS ervlet.jav a:40)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 760)
at javax.servlet.http.HttpSer vlet.servi ce(HttpSer vlet.java: 853)
at org.apache.catalina.core.A pplication FilterChai n.internal DoFilter(A pplication FilterChai n.java:247 )
at org.apache.catalina.core.A pplication FilterChai n.doFilter (Applicati onFilterCh ain.java:1 93)
at org.apache.catalina.core.S tandardWra pperValve. invoke(Sta ndardWrapp erValve.ja va:256)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.catalina.core.S tandardCon textValve. invoke(Sta ndardConte xtValve.ja va:191)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.catalina.core.S tandardCon text.invok e(Standard Context.ja va:2415)
at org.apache.catalina.core.S tandardHos tValve.inv oke(Standa rdHostValv e.java:180 )
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.valves .ErrorDisp atcherValv e.invoke(E rrorDispat cherValve. java:171)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 1)
at org.apache.catalina.valves .ErrorRepo rtValve.in voke(Error ReportValv e.java:172 )
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 1)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.catalina.core.S tandardEng ineValve.i nvoke(Stan dardEngine Valve.java :174)
at org.apache.catalina.core.S tandardPip eline$Stan dardPipeli neValveCon text.invok eNext(Stan dardPipeli ne.java:64 3)
at org.apache.catalina.core.S tandardPip eline.invo ke(Standar dPipeline. java:480)
at org.apache.catalina.core.C ontainerBa se.invoke( ContainerB ase.java:9 95)
at org.apache.coyote.tomcat4. CoyoteAdap ter.servic e(CoyoteAd apter.java :223)
at org.apache.coyote.http11.H ttp11Proce ssor.proce ss(Http11P rocessor.j ava:594)
at org.apache.coyote.http11.H ttp11Proto col$Http11 Connection Handler.pr ocessConne ction(Http 11Protocol .java:392)
at org.apache.tomcat.util.net .TcpWorker Thread.run It(PoolTcp Endpoint.j ava:565)
at org.apache.tomcat.util.thr eads.Threa dPool$Cont rolRunnabl e.run(Thre adPool.jav a:619)
at java.lang.Thread.run(Threa d.java:534 )
test.txt is the file ive uploaded, and image/ is the path I want to insert it to. Am I misuing your path setting when calling the doUpload, is path supposed to be the path the file is located at? if so how do I specify this?
I did this. Servlet grabs the form data, and then instantly calls your doUpload with this
doUpload(request, "/images/"); // as images is the folder I want to store the file in.
anyways the exception im getting is this
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.io.FileNotFoundExcept
at java.io.FileOutputStream.o
at java.io.FileOutputStream.(
at java.io.FileOutputStream.(
at eventServlet.doUpload(even
at eventServlet.doPost(eventS
at javax.servlet.http.HttpSer
at javax.servlet.http.HttpSer
at org.apache.catalina.core.A
at org.apache.catalina.core.A
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.valves
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.S
at org.apache.catalina.core.C
at org.apache.coyote.tomcat4.
at org.apache.coyote.http11.H
at org.apache.coyote.http11.H
at org.apache.tomcat.util.net
at org.apache.tomcat.util.thr
at java.lang.Thread.run(Threa
test.txt is the file ive uploaded, and image/ is the path I want to insert it to. Am I misuing your path setting when calling the doUpload, is path supposed to be the path the file is located at? if so how do I specify this?
You have to pass the full path to the function, not only the partial path.
The exception is because does not find the folder.
-tom
The exception is because does not find the folder.
-tom
the path is where do you want to save the file.
-tom
-tom
ASKER
well i want to save it on the server, cant I specify the path to be relative? like when i call a jsp i do it relativly ie "hello.jsp", the folder images exists as as sub dir to the jsp, the jsp sends the request to a servlet which then does the save, im gettin the exception because it cant find the folder, but the folder /images/ exists.
Any ideas?
Any ideas?
You do that because the Application Server knows the filesystem, but here you are accessing direct to the filesystem not through the Application Server. In fact the folder exists but the path to it is incorrect.
This process (servlet) runs in the server and knows the filesystem, you have to specify the correct path to save a file into the filesystem otherwise the AS will throw an exception.
-tom
This process (servlet) runs in the server and knows the filesystem, you have to specify the correct path to save a file into the filesystem otherwise the AS will throw an exception.
-tom
ASKER
so how do i go about specifing the absolute path?
Where do you want to save the file?
For example, in my case I call the function like this:
doUpload(request, "/Applications/eclipse/wor kspace/Hyd raNet/Resu mes/");
If your website is in, for example: /private/system/websiteroo t and you have an "images" folder in it, you have to call the function like this:
doUpload(request, "/private/system/websitero ot/images/ ");
For example, in my case I call the function like this:
doUpload(request, "/Applications/eclipse/wor
If your website is in, for example: /private/system/websiteroo
doUpload(request, "/private/system/websitero
Another thing... of course you have to have write permission in the /images folder
ASKER
well atm until i get the new username/pw for the server, im forced to use the university server as its the only way i can test jsp pages for free ;). How would i go about setting up the path and making sure that people can upload images?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks tom, got it working!
I will stay in touch to see what further developments you add to this :)
Thanks again.
I will stay in touch to see what further developments you add to this :)
Thanks again.
<FORM NAME="frmAccProfileResume"
<INPUT SIZE="40" TYPE="FILE" NAME="filename" VALUE="Browse"> <INPUT TYPE="submit" VALUE="Upload File">
</FORM>
--------------------------
And the second part is the servlet or bean that do the upload process, here I give you a function to do an upload from the request:
//Upload a file to the server
public void doUpload(HttpServletReques
//Gets the ContentType
if ((request.getContentType()
//Creates the Input Stream
DataInputStream in = new DataInputStream(request.ge
//Set the Form data length
int formDataLength = request.getContentLength()
//Create the buffer to store the file
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//Reads the InputStream
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
totalBytesRead += byteRead;
}
//Set a new string with the file content
String file = new String(dataBytes);
//Set the name of the file
String saveFile = file.substring(file.indexO
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFil
//Sets the last index and the final boundary
int lastIndex = request.getContentType().l
String boundary = request.getContentType().s
//Set the position to save the file
int pos;
pos = file.indexOf("filename=\""
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
//Sets the start and end positions
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getByte
//Creates the file physically
FileOutputStream fileOut = new FileOutputStream(Path+save
//Saves the file to the disk
//fileOut.write(dataBytes)
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
}
Hope it helps,
-tom