1. instance a HttpURLConnection , by Url.openConnection(); url=new URL(String url);
2. get an inputstream from HttpURLConnection.getInputStream();
3. get a String or write the input stream to a File .
4. get SD card file directory in android: Environment.getExternalStorageDirectory() +"/" + "file_path/ file.format"
5. remember to close all streams when done;
6. set the android permission in android manifest , out of the application element;
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.httpDownloadTest" android:versionCode="1" android:versionName="1.0"> <application android:label="@string/app_name" > <activity android:name="HttpDownloadTest" 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"/> </manifest>
package com.httpDownloadTest; import com.httpDownloadTest.utils.HttpDownloader; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class HttpDownloadTest extends Activity { private Button txtButt; private Button mp3Butt; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtButt=(Button)this.findViewById(R.id.downloadTxt); txtButt.setOnClickListener(txtLtn); mp3Butt=(Button)this.findViewById(R.id.downloadMp3); mp3Butt.setOnClickListener(mp3Ltn); } OnClickListener txtLtn=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); HttpDownloader httpDownloader=new HttpDownloader(); String lrc=httpDownloader.download( "http://www.9ilrc.com/lrc.php?id=35174&uid=2650"); Log.i("http_test", "lrc:"+lrc); } }; OnClickListener mp3Ltn=new OnClickListener(){ public void onClick(View arg0) { // throw new UnsupportedOperationException("Not supported yet."); HttpDownloader httpDownloader=new HttpDownloader(); int result=httpDownloader.download( "http://61.187.150.39:9206/5D606B71829EBCFF6BF81C5D00B23FFCA5CB434B4D56D81B1EAB23E213078E1C26E857860586267A3EE62F7F0/zhangmenshiting.baidu.com/data2/music/19235087/19234007255600.mp3?xcode=d1c2f7d1629355f6da42e1f1a4fa697b", "voa/", "a1.mp3"); Log.i("http_test",result+""); } }; }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.httpDownloadTest.utils; import android.util.Log; import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Administrator */ public class HttpDownloader { private URL url; public String download(String urlStr){ StringBuffer sb=new StringBuffer(); String line; 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){ Log.i("http_test","every line:"+line); sb.append(line); } } catch (IOException ex) { Log.i("http_test", "txt down load:"+ex.getMessage()); } try { buffer.close(); } catch (IOException ex) { Log.i("http_test", ex.getMessage()); } return sb.toString(); } public int download(String url,String path,String fileName){ InputStream inputStream=null; try{ FileUtil fileUtil=new FileUtil(); if(fileUtil.isFileExist(path+fileName)){ return 1; } else{ inputStream=getInputStreamFromUrl(url); File resultFile=fileUtil.write2SDFormat(path, fileName, inputStream); if(resultFile==null){ return -1; } } } catch(Exception e){ Log.i("http_test", "mp3 down load:"+e.getMessage()); return -1; } try { inputStream.close(); } catch (IOException ex) { Log.i("http_test", ex.getMessage()); } return 0; } private InputStream getInputStreamFromUrl(String urlStr) throws IOException { HttpURLConnection urlConn=null; try { URL url=new URL(urlStr); urlConn=(HttpURLConnection)url.openConnection(); } catch (IOException ex) { Log.i("http_test", ex.getMessage()); } return urlConn.getInputStream(); }; }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.httpDownloadTest.utils; import android.os.Environment; import android.util.Log; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Administrator */ public class FileUtil { private String SD_PATH; public String getSDPATH(){ return SD_PATH; } public FileUtil(){ SD_PATH=Environment.getExternalStorageDirectory()+"/"; Log.i("http_test", "sd_path:"+SD_PATH); } public File createSDFile(String fileName){ File file=null; try { file=new File(SD_PATH+fileName); file.createNewFile(); } catch (IOException ex) { Log.i("http_test", "file create error:"+ex.getMessage()); } return file; } public File createSDDir(String dirName){ File dir=new File(SD_PATH+dirName); dir.mkdir(); return dir; } public boolean isFileExist(String fileName){ File file=new File(fileName); return file.exists(); } public File write2SDFormat(String path,String fileName,InputStream input){ File file=null; FileOutputStream 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 (IOException ex) { Log.i("http_test", "file write error:"+ex.getMessage()); } try { output.close(); } catch (IOException ex) { Log.i("http_test", ex.getMessage()); } return file; } }