How to upload a large file from HTTP?

Question:

I am writing my first application, for this I ask you not to scold too much …

Is it possible to download a file from the internet without using BufferedInputStream ?

The problem is that when downloading a file up to 50MB in size, everything is fine, but if the file is larger, I get an exception with a heap overflow error.

12-18 18: 20: 26.846: I / dalvikvm-heap (12319): Forcing collection of SoftReferences for 58369314-byte allocation__ __12-18 18: 20: 26.916: E / dalvikvm-heap (12319): Out of memory on a 58369314-byte allocation.

In addition, the file limit varies depending on the device.

The code:

  @Override
        protected Boolean doInBackground(String...  params) {
            //В этом методе происходит загрузка файла через
            //стандартный класс URLConnection
            int count;

            try {
                int i=0;
                dataFiles = getResources().getStringArray(R.array.dataFiles);  
                File path = new File(Environment.getExternalStorageDirectory()+"/testing");
                if(!path.exists()){
                    path.mkdirs();
                }
                for (String url_string : params) {
                    URL url = new URL(url_string);
                    URLConnection conection = url.openConnection();
                    conection.connect();
                    int lenghtOfFile = conection.getContentLength();
                    InputStream input = new BufferedInputStream(url.openStream(), lenghtOfFile);
                    OutputStream output = new FileOutputStream(path+"/"+dataFiles[i]);
                    byte data[] = new byte[256];     
                    long total = 0;
                    while ((count = input.read(data)) != -1) {                   
                        //Проверяем, актуальна ли еще задача
                        if (isCancelled()){
                             output.flush(); 
                            output.close();
                            input.close();
                            return null;
                        }
                        total += count;  
                        output.write(data, 0, count);

                        //Информирование о закачке.
                        //Передаем число, отражающее процент загрузки файла
                        //После вызова этого метода автоматически будет вызван
                        //onProgressUpdate в главном потоке
                        publishProgress((int)((total*100)/lenghtOfFile));
                    }
                    output.flush(); 
                    output.close();
                    input.close();
                    ++i;
                }
            } catch (Exception e) {
                System.out.println("Ошибка: "+e.getMessage()); 
            }


            return true;
        }

Can you please tell me how it is still possible to download large files without eating the whole bunch?

Answer:

It is not clear why you are using BufferedInputStream when downloading data of a fixed size from the network and saving it directly to a file, which does not make any sense, and at the same time you do not wrap the FileOutputStream in a BufferedOutputStream , which should significantly reduce the brakes, in the case of a slow FS, and fast internet connections. Try the opposite.

Scroll to Top