Buffer size issues with file downloads in the Java web

Source: Internet
Author: User
Tags array length java web

Just do a small example of your own, is to click on a button on the Android side, and then download the file from the server side to the phone SD card. The result has encountered a problem, just started very excited, because they can download the file, but self-study to find that there is a problem, that is, the size of the download to the file is 0, can not be opened after export, do not understand, and then do not know how to engage in a moment, the size of the file itself has changed, The downloaded TXT file size is restored to normal, while the downloaded MP3 file is only 198K, such as:

My server code is as follows:

Encounter such a wonderful problem, I began to want to read the TXT that success, why this failed, read the code to know that the original way to read the same, when reading TXT code as follows;

1     protected voiddoget (httpservletrequest request,2HttpServletResponse response)throwsservletexception, IOException {3 4String Path = This. Getservletcontext (). Getrealpath ("/download/pig.jpg");5String filename = path.substring (path.lastindexof ("\ \") + 1);6 7         //If you change the contents of path.lastindexof ("\ \") to "/", the downloaded file name contains the full path. 8         //the equivalent is set directly to Response.setheader ("Content-disposition",9         //"attachment;filename=" + path);Ten  OneInputStream in =NULL; AOutputStream out =NULL; -  -Response.setheader ("Content-disposition", "Attachment;filename=" the+filename); -  -         Try { -in =NewFileInputStream (path); +  -out =Response.getoutputstream (); +  A             intLen = 0; at             byteBuffer[] =New byte[1024]; -              while(len = in.read (buffer))! = 0) { -  -Out.write (buffer, 0, Len); -             } -  in}Catch(Exception e) { -             //Todo:handle Exception to}finally { +             Try { - in.close (); the out.close (); *}Catch(Exception e) { $                 //TODO auto-generated Catch blockPanax Notoginseng e.printstacktrace (); -             } the         } +  A}

From the above you can see the difference between the two, that is, as a buffer byte array length problem, one is directly fixed 1024 size, the other is to use a kind of online people to write code habits to try to directly determine the size of the stream data, thus a one-time generation of an array of size just to hold the data , it turns out that the error is exactly here, according to read the TXT file to change the code to read the MP3 file, found that the program is working properly.

Explain:

Because the mp3 file is different from the text file, the MP3 format of the song complete small general also has 2,3m size, converted to from KB words used to generate the size of the array, it seems relatively large. There should be a memory problem here that caused this inexplicable error.

Because they are just learning Java soon, so many are just guessing, not to study the source code or read more in-depth book, so if you have any better explanation or more professional explanation, also hope pointing twos. Let me also be able to dispel doubts. Thank you.

Buffer size issues with file downloads in the Java web

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.