功能是實現下載文件,圖片或MP3等,爲了簡單起見使用單線程,此代碼爲MarsAndroid教程的複製品,放在此處,留着參考。java
首先是一個獲得字節流隨後保存到內存卡上的工具類:android
package com.example.utils; 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 FileUtils { private String SDpath; public String getSDpath(){ return SDpath; } public FileUtils(){ //獲得當前外部存儲設備的目錄,即/SDCARD,後邊加"/"爲了以後方便 SDpath=Environment.getExternalStorageDirectory()+"/"; } /** * 在SD卡上建立文件 * @param fileName * @return File * @throws IOException */ public File creatSDFile(String fileName) throws IOException{ File file=new File(SDpath+fileName); file.createNewFile(); return file; } /** * 在SDCARD建立目錄 * @param dirName * @return */ public File creatSDDir(String dirName){ File dir=new File(SDpath+dirName); dir.mkdir(); return dir; } public boolean isFileExist(String fileName){ File file=new File(fileName); return file.exists(); } /** * 將一個InputStream裏的數據寫入SD卡 */ public File write2SDFromInput(String path,String fileName,InputStream is){ File file=null; OutputStream os=null; try{ creatSDDir(path); file=creatSDFile(path+fileName); os=new FileOutputStream(file); byte buffer[]=new byte[4*1024];//4kb while((is.read(buffer))!=-1){ os.write(buffer); } os.flush(); System.out.println("write to "+path+"sucess!"); }catch(Exception e){ e.printStackTrace(); }finally{ try{ os.close(); }catch(Exception e){ e.printStackTrace(); } } return file; } }
隨後是文件下載類:app
package com.example.utils; 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; //此方法返回文本,但並未保存到SD卡 public String downloadText(String urlstr){ StringBuffer sb=new StringBuffer(); String line=null; BufferedReader br=null; try{ url=new URL(urlstr); HttpURLConnection con= (HttpURLConnection)url.openConnection(); br=new BufferedReader(new InputStreamReader(con.getInputStream())); while((line=br.readLine())!=null){ sb.append(line); } }catch(Exception e){ e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return sb.toString(); } /** * * @param urlstr * @param path * @param fileName * @return -1-error,0-success,1-file exist */ public int downloadMP3(String urlstr,String path,String fileName){ InputStream is=null; try{ FileUtils fileUtils=new FileUtils(); if(fileUtils.isFileExist(path+fileName)){ return 1; }else{ is=getInputStreamFromUrl(urlstr); File resultfile=fileUtils.write2SDFromInput(path, fileName, is); if(resultfile==null){ return -1; } } }catch(Exception e){ e.printStackTrace(); return -1; }finally{ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return 0; } private InputStream getInputStreamFromUrl(String urlstr) throws MalformedURLException,IOException{ URL url=new URL(urlstr); HttpURLConnection con=(HttpURLConnection)url.openConnection(); InputStream is=con.getInputStream(); return is; } }