ChronoXIII ChronoXIII - 8 months ago 53
Java Question

Determine the size of an InputStream

My current situation is: I have to read a file and put the contents into

. Afterwards I need to place the contents of the
into a byte array which requires (as far as I know) the size of the
. Any ideas?

As requested, I will show the input stream that I am creating from an uploaded file

InputStream uploadedStream = null;
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
java.util.List items = upload.parseRequest(request);
java.util.Iterator iter = items.iterator();

while (iter.hasNext()) {
FileItem item = (FileItem);
if (!item.isFormField()) {
uploadedStream = item.getInputStream();
//CHANGE uploadedStreambyte = item.get()

The request is a
object, which is like the
is from the Apache Commons FileUpload package.

Answer Source

I just wanted to add, Apache Commons IO has stream support utilities to perform the copy. (Btw, what do you mean by placing the file into an inputstream? Can you show us your code?)


Okay, what do you want to do with the contents of the item? There is an item.get() which returns the entire thing in a byte array.


item.getSize() will return the uploaded file size.