File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Accepting File Uploads to Server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accepting File Uploads to Server" Watch "Accepting File Uploads to Server" New topic
Author

Accepting File Uploads to Server

Billy Hause
Greenhorn

Joined: Jan 16, 2002
Posts: 25
I'm using ServletFileUpload and parseRequest(req) to accept file uploads to the server.

It seems to be the case that once you call parseRequest to get a List of file items, you can not call it again later.

I would like to parse the request once to figure out what is being uploaded and then pass the request off to a handler to process that particular kind of upload.

The problem is that the handler can't parse the request because it's already been parsed earlier to determin what handler to send the request to.

Is there some way to preserve the request so that the handler can parse it or can requests only be parsed once by ServletFileUpload.parseRequest()?

Thanks for any help. It seems lame to me that parseRequest() would modify the request in such a way that it can't be parsed again later.

Thanks

Billy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

ServletFileUpload is not a standard package so you'll need to check the documentation or code source of that package.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Billy Hause
Greenhorn

Joined: Jan 16, 2002
Posts: 25
Thanks for the reply Bear,

ServletFileUpload is a class in the org.apache.commons.fileupload.servlet package.

I believe most file uploads are done using this class so I thought someone in this forum may have used it before and know the answer to my question.

I spent time going throught the documentation for this class before posting my question but was not able to find the answer.

I'm still needing a solution to the problem so if anyone who reads this knows the answer I would appriciate hearing it. Otherwise I will find some sort of a work-around.

Thanks

Billy
Billy Hause
Greenhorn

Joined: Jan 16, 2002
Posts: 25
I should have posted this code to begin with but better late than never.

Here is my code. If I make a call to upload.parseRequest(req); after the code shown below, then the List that is returned is empty. I would like a way to reset the request so that I can pass it on to a handler function and parse the request again in the handler.

Thanks again for any help,

Billy

// Check that we have a file upload request
if (ServletFileUpload.isMultipartContent(req))
{
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ fileItems = upload.parseRequest(req);

// Process the uploaded items
Iterator iter = fileItems.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();

if (item.isFormField())
{
// Process a regular form field
sMsg += "FILE FORM FIELD - Name, Value: " + item.getFieldName() + ", " + item.getString() + "\n";
}
else
{
sMsg += "FILE UPLOAD - File Name: " + item.getName() + "\n";
sMsg += "FILE UPLOAD - Content Type: " + item.getContentType() + "\n";
sMsg += "FILE UPLOAD - IsInMemory: " + item.isInMemory() + "\n";
sMsg += "FILE UPLOAD - Size In Bytes: " + item.getSize() + "\n";
}
}
Billy Hause
Greenhorn

Joined: Jan 16, 2002
Posts: 25
Solution Found!

The request canot be parsed more than once but the resulting List object can have new iterators created so the solution is to pass the List to the handler function instead of the request.

Thanks,

Billy
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

Thank you for posting back with the solution. It will help any future readers who might have the same issue.
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 115

Guys,
Is there any dependent jars that i need to use other than commons-fileupload-1.2.2 for using the parseRequest method . i am not able to proceed further this line

This line returns me back to error page. I'm not able to see anything in my logs .

My code is as follows


"Learning is a Culture where your Eagerness & Curiosity plays a major Role".
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

if there's a missing jar you should see a ClassNotFOund exception.
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 115

Guys,
I got the solution. I needed commons-io-1.1.jar . I got the below error

java.lang.NoClassDefFoundError: org/apache/commons/io/output/DeferredFileOutputStream
karthik Suryanarayanan
Ranch Hand

Joined: Oct 07, 2009
Posts: 115

Thanks Bear . I just couldn't think out of box working day and night and finally found out the exception in System error logs. What a funny world to work in !!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61415
    
  67

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accepting File Uploads to Server