eamolawler
asked on
using a file in jsp that was sent with a <INPUT TYPE="FILE"...
Hi,
I want to retrieve a file that is sent with a html request in a jsp page
I've got a html page with this form :
<FORM ACTION="file.jsp" METHOD="POST" ENCTYPE="multipart/form-da ta">
Send this file to the server
<INPUT TYPE="FILE" NAME="file">
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
In my file.jsp how do I retrieve the file that is contained in the request object "file"? i.e. if I want to store that file in a directory on the server, how do I copy the file?
Thanks.
I want to retrieve a file that is sent with a html request in a jsp page
I've got a html page with this form :
<FORM ACTION="file.jsp" METHOD="POST" ENCTYPE="multipart/form-da
Send this file to the server
<INPUT TYPE="FILE" NAME="file">
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
In my file.jsp how do I retrieve the file that is contained in the request object "file"? i.e. if I want to store that file in a directory on the server, how do I copy the file?
Thanks.
http://jakarta.apache.org/commons/fileupload/
ASKER
ok.. looks like I can use these classes in my jsp code to retrieve the files from the request object and do some work with them. Sounds good so far.
- Are these the "industry standard" means of retrieving files on web servers?
- Which would you recommend? I need this for a website that will allow users to upload a number of large files (~1mb each)
Thanks Idbkutty
- Are these the "industry standard" means of retrieving files on web servers?
- Which would you recommend? I need this for a website that will allow users to upload a number of large files (~1mb each)
Thanks Idbkutty
I don't think any of them are "industry standard"... But if you already use part of the Jakarta project, like Tomcat, it's as close as you are going to get
It's pretty simple to roll your own with StreamMultipart, FileOutputStream, and BufferedOutputStream... It also found it allows me to have the code a bit more custom for different file types, validation, and error handling but that's just me.
It's pretty simple to roll your own with StreamMultipart, FileOutputStream, and BufferedOutputStream... It also found it allows me to have the code a bit more custom for different file types, validation, and error handling but that's just me.
ldbkutty is correct with jakarta commons fileupload. It is the industry standard for web based J2EE projects. Simple to use too:
In your servlet you merely parse the request like this:
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Set upload parameters
upload.setSizeThreshold(yo urMaxMemor ySize);
upload.setSizeMax(yourMaxR equestSize );
upload.setRepositoryPath(y ourTempDir ectory);
// Parse the request
List items = upload.parseRequest(reques t);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//As an input stream
InputStream uploadedStream = item.getInputStream();
//or as a byte array
//byte[] data = item.get();
}
In your servlet you merely parse the request like this:
// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();
// Set upload parameters
upload.setSizeThreshold(yo
upload.setSizeMax(yourMaxR
upload.setRepositoryPath(y
// Parse the request
List items = upload.parseRequest(reques
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//As an input stream
InputStream uploadedStream = item.getInputStream();
//or as a byte array
//byte[] data = item.get();
}
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 for the help W0lver, bloodredsun and Idbkutty