Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Byte

Posted on 2006-06-23
23
Medium Priority
?
355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 3
  • +3
23 Comments
 
LVL 16

Author Comment

by:suprapto45
ID: 16967112
And can you please explain it :)?
0
 
LVL 16

Author Comment

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

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 16967118
byte[] byteArr = new byte[(int)file.getLength()]
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Expert Comment

by:sciuriware
ID: 16967140
//And:

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

;JOOP!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16967151
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
ID: 16967327
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
ID: 16967329
Additionally,

Why filesize > 1024*1024 is considered too big?

David
0
 
LVL 10

Assisted Solution

by:rajesh_bala
rajesh_bala earned 200 total points
ID: 16967339
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
ID: 16967357
>>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
ID: 16967365
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
ID: 16967377
>>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
 
LVL 16

Author Comment

by:suprapto45
ID: 16967399
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
ID: 16967409
Sorry...again, another meeting....Give me a moment

David
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16967414
>>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 600 total points
ID: 16967647
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
ID: 16967648
0
 
LVL 16

Author Comment

by:suprapto45
ID: 16967652
Ok,

I will try to grab some concepts here.

David
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1200 total points
ID: 16967825
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
ID: 16967883
> FileUploadBase.SizeLimitExceededException as i mentioned earlier.

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

Expert Comment

by:Mayank S
ID: 16968815
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
ID: 16970179
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
ID: 16970232
:-)
0
 
LVL 10

Expert Comment

by:rajesh_bala
ID: 16970852
Thank you... :)
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

610 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