Java tutorial
/* * This source is part of the CommonClasses repository. * * Copyright 2014 Kevin Liu (airk908@gmail.com) * * CommonClasses is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * CommonClasses is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with CommonClasses. If not, see <http://www.gnu.org/licenses/>. */ package com.github.commonclasses.network; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.GZIPInputStream; import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; /** * Network download tool * <p/> * Permission Needed * <p/> * <uses-permission android:name="android.permission.INTERNET" /> */ public class DownloadUtils { private static final int CONNECT_TIMEOUT = 10000; private static final int DATA_TIMEOUT = 40000; private final static int DATA_BUFFER = 8192; public interface DownloadListener { public void downloading(int progress); public void downloaded(File dest); public void exception(Exception e); } public static long download(String urlStr, File dest, boolean append, DownloadListener downloadListener) throws Exception { int downloadProgress = 0; long remoteSize = 0; int currentSize = 0; long totalSize = -1; if (!append && dest.exists() && dest.isFile()) { dest.delete(); } if (append && dest.exists() && dest.exists()) { FileInputStream fis = null; try { fis = new FileInputStream(dest); currentSize = fis.available(); } catch (IOException e) { throw e; } finally { if (fis != null) { fis.close(); } } } HttpGet request = new HttpGet(urlStr); request.setHeader("Content-Type", "application/x-www-form-urlencoded"); if (currentSize > 0) { request.addHeader("RANGE", "bytes=" + currentSize + "-"); } HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, CONNECT_TIMEOUT); HttpConnectionParams.setSoTimeout(params, DATA_TIMEOUT); HttpClient httpClient = new DefaultHttpClient(params); InputStream is = null; FileOutputStream os = null; try { HttpResponse response = httpClient.execute(request); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { is = response.getEntity().getContent(); remoteSize = response.getEntity().getContentLength(); Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { is = new GZIPInputStream(is); } os = new FileOutputStream(dest, append); byte buffer[] = new byte[DATA_BUFFER]; int readSize = 0; while ((readSize = is.read(buffer)) > 0) { os.write(buffer, 0, readSize); os.flush(); totalSize += readSize; if (downloadListener != null) { downloadProgress = (int) (totalSize * 100 / remoteSize); downloadListener.downloading(downloadProgress); } } if (totalSize < 0) { totalSize = 0; } } } catch (Exception e) { if (downloadListener != null) { downloadListener.exception(e); } e.printStackTrace(); } finally { if (os != null) { os.close(); } if (is != null) { is.close(); } } if (totalSize < 0) { throw new Exception("Download file fail: " + urlStr); } if (downloadListener != null) { downloadListener.downloaded(dest); } return totalSize; } }