File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Open Source Projects and the fly likes commons file upload help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "commons file upload help needed" Watch "commons file upload help needed" New topic

commons file upload help needed

Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Hello All,

Im trying to use the streaming API for file upload.

I have taken the code sample from the site's "Stream API" section and already have the basic steps.

I need the file to be uploaded available as a byte[].

I need code sample for
1. converting an InputStream into byte[] if anyone has already done that.
2. before using this streaming API I used to get the fileSize by a metod called item.size()
Which is the corresponding method in the streaming API?


Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

For #1: if you have code that reads from an InputStream and copies the data to an OutputStream, then use that code to copy to a ByteArrayOutputStream. After you close the OutputStream you then have an object you can get a byte[] from.

For #2: I looked in the documentation a bit, then I decided it wasn't really up to me to find out what you meant by that. It would be easier if you just posted the name of the class you were asking about.
Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
For #1: Thank you. Will try that out. Buferring and ByteArrayOutputStream together may solve my problem.

For #2: My mistake. I tried putting this on the commons mailing list and there everyone refers according to the sample code provided on the site.

In the API docs at here there is a class called DiskFileItem in org.apache.commons.fileupload.disk package. I used use the getSize() method in it when I used the code for 1.0 version [non-streaming]

Now Im trying out streaming as it is faster and I need to support larger files.

By basic purpose is - I need to get the file size before the upload starts and restrict the upload if the file size is too big.

Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
ServletFileUpload has a couple of max size settings. They arent exactly what you are asking for, but have you tried using them?
Leena Diwan
Ranch Hand

Joined: Jun 18, 2001
Posts: 351
Thank you for your reply.
Yes, Im using the method for some other size validations.

I did some testing and research in past 3/4 days and may be I have arrived at what is actually happening.

If Im not mistaken, for large files the issue is because of the behaviours of HTML and Java: Once the user hits on upload button, the file is included in the request body as the enctype is multipart/form-data. If the connection is slower [this application can be used over internet from user being anywhere in the world] the time taken for a file >10 MBs is considerable.

By the time the whole request is prepared and sent to the server, a lot of time elapses. Then the method you have specified can be called on the fileItem.

If the user is trying to upload a file way above the limit, like of 50MB or 100MB, then the time taken to reach till the point error gets thrown increases.

As of now we are experiencing delays in giving the size limit error message to user like the following- 10MB file ~ 10minutes. 33 MB file ~ 30 minutes and so on.

What can we do for improving this? Or is there any way to get the file size before even the upload begins?

[ April 09, 2007: Message edited by: Leena Diwan ]
I agree. Here's the link:
subject: commons file upload help needed
It's not a secret anymore!