JSP File Upload or Read
Hi, I'm trying to find a way to upload files using jsp. I've created a form, where the user can select a file to upload. Once the submit button in clicked, it calls my jsp, but I'm not sure how to upload the file. I'd rather not put a jar file in the lib directory.
Alternatively, I know that the user will only upload a text file, so I could just read that file instead of actually making a copy of the uploaded file on the server. Is there a way to do that in JSP?
Thanks!
[497 byte] By [
Jenny24] at [2007-9-27 21:45:19]

I guess if you really wanted to you could write a parser that would through multipart/form-data submissions to get the parts out, decode them, and then decide what to do with the bytes. Take a look at RFC2388 (http://rfc.sunsite.dk/rfc/rfc2388.html). It defines the contents of a form submission to the server. You will have to write a parser that conforms with this standard but why bother when there are already some out there.
It would be a lot easier to just include the cos.jar and use the OReilly package for this or take a look at jspSmartupload.
As for your other idea, you cannot read a file on the client machine with serverside code. Think of the security problems this would cause if it were allowed. The only I know of to do this is to build an applet but even then the user will have to give explicitly give the applet permission to access the filesystem.
tus at 2007-7-7 3:44:17 >

You have to check in your form (the one with the file input), that the enctype="multipart/form-data" property is added.
You can try using a bean from http://www.jspsmart.com/ downloading a free version of the parser for uploading files. One you have it, put it in your WEB-INF/lib directory and modify your JSP file (the one from the action html tag!!!) and add this line. It brings you the possibility of using the bean.
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
Finally, add the code necessary to upload the file. It could look like this:
<%
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
try
{
mySmartUpload.save("/arp/upload");
}
catch(Exception e)
{
//handling the exception
}
%>
I hope it will help you. !!!
Andres.