download File : Download « Network « Android






download File

  
//package org.dyndns.warenix.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Environment;
import android.util.Log;

class DownloadFileTool {

  public String downloadFile(URL url, String saveInDir, String saveAsFilename) {
    return downloadFile(url, saveInDir, saveAsFilename, null);
  }

  public String downloadFile(URL url, String saveInDir,
      String saveAsFilename,
      DownloadProgressListener downloadProgressListener) {

    String sdDrive = Environment.getExternalStorageDirectory()
        .getAbsolutePath();

    // Create one directory
    String fullLocalDirPath = String.format("%s/%s", sdDrive, saveInDir);
    Log.d("warenix", "saved in " + fullLocalDirPath);
    boolean success = (new File(fullLocalDirPath)).mkdirs();
    if (success) {
      Log
          .i("warenix", String.format("created dir[%s]",
              fullLocalDirPath));
    }

    InputStream in = null;
    BufferedOutputStream out = null;
    //
    // String filepath = Environment.getExternalStorageDirectory()
    // .getAbsolutePath();
    String full_local_file_path = String.format("%s/%s", fullLocalDirPath,
        saveAsFilename);
    Log.v("warenix", String.format("of to %s", full_local_file_path));

    try {

      FileOutputStream fos = new FileOutputStream(full_local_file_path);
      BufferedOutputStream bfs = new BufferedOutputStream(fos,
          IO_BUFFER_SIZE);

      int iFileSize = DownloadFileTool.getContentLength(url);
      Log.d("warenix", String.format("going to download file size %d",
          iFileSize));

      in = new BufferedInputStream(url.openStream(), IO_BUFFER_SIZE);

      final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
      out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
      copy(in, out, downloadProgressListener, iFileSize);
      out.flush();
      final byte[] data = dataStream.toByteArray();

      bfs.write(data, 0, data.length);
      bfs.flush();

    } catch (IOException e) {
    } finally {
      closeStream(in);
      closeStream(out);
    }

    return full_local_file_path;
  }

  // helper method
  static int IO_BUFFER_SIZE = 4096;

  private void copy(InputStream in, OutputStream out,
      DownloadProgressListener downloadProgressListener,
      final int iFileSize) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    int totalDownloaded = 0;
    while ((read = in.read(b)) != -1) {
      totalDownloaded += read;
      Log.d("warenix", String.format("downloaded: %d", totalDownloaded));
      if (downloadProgressListener != null) {
        downloadProgressListener.onReceivedProgressUpdate(
            totalDownloaded, iFileSize);
      }

      out.write(b, 0, read);
    }
  }

  DownloadProgressListener downloadProgressListener;

  private void closeStream(Closeable stream) {
    if (stream != null) {
      try {
        stream.close();
      } catch (IOException e) {
        // android.util.Log.e(LOG_TAG, "Could not close stream", e);
      }
    }
  }

  public interface DownloadProgressListener {
    public void onReceivedProgressUpdate(int totalDownloaded,
        final int fileSize);
  }

  public interface DownloadCompletedListener {
    public void onCompleted(String full_local_file_path);
  }

  public static int getContentLength(URL urlFileLocation) {
    HttpURLConnection connFile = null;
    int iFileSize = -1;
    try {
      connFile = (HttpURLConnection) urlFileLocation.openConnection();
      connFile.setDoInput(true);
      InputStream is = connFile.getInputStream();
      iFileSize = connFile.getContentLength();
      is.close();
      connFile.disconnect();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return iFileSize;
  }

}

   
    
  








Related examples in the same category

1.Android download file tools
2.Download Manager Demo
3.Using AsyncTask to download a big file
4.Using DownloadManager
5.Download Task
6.File Download Service
7.Cacheable Downloader
8.Download Manager
9.Book Downloader