Android文件下载

package com.zzy.infobackup.utils.httpUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import com.zzy.infobackup.model.Constant;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

/**
 * class name:HttpDownload<BR>
 * class description:Http下载类<BR>
 * PS: <BR>
 * Date:2013-4-18<BR>
 * 
 * @version 1.0
 * @author wyr
 */
public class HttpDownload {
	private static final int connecttime = 6000;

	/**
	 * 下载文件
	 * 
	 * @param urlStr
	 *            下载地址
	 * @return
	 * */
	public static String downLoad(String urlStr) {
		HttpURLConnection huc;
		StringBuffer sb = new StringBuffer();
		BufferedReader br = null;
		String line = new String();
		URL url;
		try {
			url = new URL(urlStr);
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}
		try {
			huc = (HttpURLConnection) url.openConnection();
			huc.setConnectTimeout(connecttime);
			huc.connect();
			if (huc.getResponseCode() != 200) {
				System.out.println("失败");
				return null;
			}

			br = new BufferedReader(new InputStreamReader(huc.getInputStream()));

			while ((line = br.readLine()) != null) {
				sb.append(line);
			}

		} catch (HttpHostConnectException ste) {
			ste.printStackTrace();
			return null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("IOException");

			e.printStackTrace();
			return null;
		}
		return sb.toString();
	}
}

你可能感兴趣的:(Android文件下载)