• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accepting File Uploads to Server

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ServletFileUpload is not a standard package so you'll need to check the documentation or code source of that package.
 
Billy Hause
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for posting back with the solution. It will help any future readers who might have the same issue.
 
Ranch Hand
Posts: 129
1
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if there's a missing jar you should see a ClassNotFOund exception.
 
karthik Suryanarayanan
Ranch Hand
Posts: 129
1
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 129
1
Oracle Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
F is for finger. Can you stick your finger in your nose? Doesn't that feel nice? Now try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic