Solved

Byte

Posted on 2006-06-23
23
342 Views
Last Modified: 2010-03-31
Ok,

I am really not good in this java.io thing.

        // Make a byte Array big enough to fit the binary file.
        byte[] byteArr = new byte[filesize];
        //Create an input stream to read the uploaded file.
        ByteArrayInputStream bytein = new ByteArrayInputStream(form.contentUp.getFileData());
        // Load the input stream into the byte Array.
        bytein.read(byteArr);
        // Close the input stream.
        bytein.close();

Okay, what should be the value in the filesize? >>"byte[] byteArr = new byte[filesize];"
I want the user can only upload 1MB.

Thanks
David
0
Comment
Question by:suprapto45
  • 9
  • 7
  • 3
  • +3
23 Comments
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
And can you please explain it :)?
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
I'll be coming back in the next 30 mins
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 300 total points
Comment Utility
byte[] byteArr = new byte[(int)file.getLength()]
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
//And:

    if(filesize > 1024*1024)
    {
//     refuse: too big.
    }

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Correction: i just noticed this

>>I want the user can only upload 1MB.

You need to catch an exception caused by exceeding max size in the upload component
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Thanks guys,

Sorry for the delay. I just get back to my machine now.

So does it mean that
byte[] byteArr = new byte[(int)file.getLength()]
is actually loading the content of the files into byteArr?

Just finding problem in understanding most of the java.io concepts.

David
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Additionally,

Why filesize > 1024*1024 is considered too big?

David
0
 
LVL 10

Assisted Solution

by:rajesh_bala
rajesh_bala earned 50 total points
Comment Utility
I am not sure if you are trying to copy from file. If so, the following might be an option as well..

    public static void copyCompletely(InputStream input, OutputStream output)
        throws IOException
    {
        //if both are file streams, use channel IO
        if ((output instanceof FileOutputStream) && (input instanceof FileInputStream))
        {
            try
            {
                FileChannel target = ((FileOutputStream) output).getChannel();
                FileChannel source = ((FileInputStream) input).getChannel();
               
                source.transferTo(0, Integer.MAX_VALUE, target); //MOST EFFECIENT WAY OF COPYING. ITS NATIVE COPY.
               
                source.close();
                target.close();
               
                return;
            }
            catch (Exception e)
            { /* failover to byte stream version */ }
        }
       
        byte[] buf = new byte[8192]; //increase it since you might throw an exception, if the file size if more than 1 MB.
        while (true)
        {
            int length = input.read(buf);
            if (length < 0)
                break;
            output.write(buf, 0, length);
        }
       
        try { input.close(); } catch (IOException ignore) {}
        try { output.close(); } catch (IOException ignore) {}
    }
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Why filesize > 1024*1024 is considered too big?

The only person who said it was is you ;-) That is entirely up to you. Just to clarify, are you reading a file uploaded by a user and do you want the upload to be limited to 1Meg?
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
ah ha,

It is not me :). It is sciuriware - look at the previous message.

sciuriware  >> can you elaborate more :)?

CEHJ >> Thanks, so how do we determine the file size?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>CEHJ >> Thanks, so how do we determine the file size?

Well you didn't answer my first question, but i'll assume the answer is yes. What file upload component are you using?
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Author Comment

by:suprapto45
Comment Utility
I used simple input type="file" component. Well, the file is uploaded successfully.

My confusion is how to determine the filesize of the codes above. Try to assume that we do not have the limitation of 1 MB. What should be the value there? This is where my weaknesses are i.e. java.io

David
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Sorry...again, another meeting....Give me a moment

David
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I used simple input type="file" component. Well, the file is uploaded successfully.

I mean on the other end. How is the file being received?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
Comment Utility
you actually weant to be writing the recieved data to a ByteArrayOuputStream (or some other output stream). you don't actually mneed to know the size as the buffer will reallocate size as needed.
If you use something file jakarta fileupload a lot of this is looked after for you.
to limit the amount read you can use something like:

while (nread<maxsize && -1!=(n = in.read(buf)) {
   if (nread + n>maxsize) n = maxsize - nread;
   out.write(buf, 0, n);
   nread += n;
}
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Ok,

I will try to grab some concepts here.

David
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
Comment Utility
One concept you need to get hold of is that you should not try to limit the amount you read server-side, as has been suggested. Even if this is successful, you will couple the max size unnecessarily in your code. Get rid of the coupling by specifying the maximum size declaratively and  catching FileUploadBase.SizeLimitExceededException as i mentioned earlier.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> FileUploadBase.SizeLimitExceededException as i mentioned earlier.

thats a feature of jakarta upload, which I actually mentioned :)
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
Yes, if the file is too big, try to stream it by reading it in chunks, writing then flushing, and then reading again.
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
I am going to split the questions here.

I would be more specific in the next questions...I will post more questions soon (mostly about Java IO).

David
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 
LVL 10

Expert Comment

by:rajesh_bala
Comment Utility
Thank you... :)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now