1. 创建HttpURLConnection对象
HttpDownloader downloader = new HttpDownloader();
2.获得一个InputStream对象
3.访问网络,在Manifest.xml声明:androi.permission.INTERNET
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="mars.download"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Download"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"/> //允许网络访问
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> //允许写入SDK卡
</manifest>
Environment.getExternalStorageDirectory()// 获得SDK的路径
package page.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpDownloader{
private URL url=null;
/**
* 根据URL下载文本文件
*/
public String download(String urlStr){
StringBuffer sb = new StringBuffer();
String line = null;
BufferedReader buffer = null;
try{
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((line = buffer.readLine())!=null)
{
sb.append(line);
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
buffer.close();
}catch(Exception e){
e.printStackTrace();
}
}
return sb.toString();
}
/**
* 下载文件并写SD卡
* @param urlStr
* @param path
* @param fileName
* @return 0-success,-1-fail,1-existed
*/
public int downFile(String urlStr,String path,String fileName){
InputStream inputStream= null;
try{
FileUtil fileUtil = new FileUtil();
if(fileUtil.isFileExist(path+fileName))
return 1;
else{
inputStream = getInputStreamFromUrl(urlStr);
File resultFile = fileUtil.write2SDFromInput(path, fileName, inputStream);
if(resultFile == null)
return -1;
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
return 0;
}
public InputStream getInputStreamFromUrl(String urlStr) throws MalformedURLException,IOException{
url = new URL(urlStr);
HttpURLConnection urlCon =(HttpURLConnection)url.openConnection();
InputStream inputStream = urlCon.getInputStream();
return inputStream;
}
}
package page.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.os.Environment;
public class FileUtil{
private String SDPATH;
public String getSDPATH(){
return SDPATH;
}
public FileUtil(){
SDPATH= Environment.getExternalStorageDirectory()+"/";
}
public File createSDFile(String fileName) throws IOException{
File file = new File(SDPATH+fileName);
file.createNewFile();
return file;
}
public File createSDDir(String dirName) {
File dir = new File(SDPATH+dirName);
dir.mkdir();
return dir;
}
public boolean isFileExist(String fileName){
File file = new File(SDPATH+fileName);
return file.exists();
}
public File write2SDFromInput(String path,String fileName,InputStream input){
File file = null;
OutputStream output = null;
try{
createSDDir(path);
file = createSDFile(path+fileName);
output = new FileOutputStream(file);
byte buffer[] = new byte[4*1024];
while((input.read(buffer))!=-1){
output.write(buffer);
}
output.flush();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
output.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return file;
}
}