文件下載

 

                                                                               文件下載的步驟

1.創建一個URL對象

2.創建一個HttpURLConnection對象

HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();

3.獲得一個InputStream對象urlConn.getInputStream()

4.設置訪問網絡的權限

android.permission.INTERNET

DownLoad.java

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        downloadTxtButton=(Button)findViewById(R.id.downloadTxtButton);
        downloadTxtButton.setOnClickListener( new DownloadTxtListener());
        downloadMp3Button=(Button)findViewById(R.id.downloadMp3Button);
        downloadMp3Button.setOnClickListener(new DownloadMp3Listener());
    } 
    class  DownloadTxtListener implements OnClickListener{


  public void onClick(View v)
  {
  HttpDownloader httpDownloader=new HttpDownloader();
  String Irc=httpDownloader.download("
www.baidu.com"); 
  System.out.println(Irc);
  }
  }
    class DownloadMp3Listener implements OnClickListener{

  public void onClick(View v)
  {
   HttpDownloader httpDownloader=new HttpDownloader();
   int result=httpDownloader.downFile("
http://zhangmenshiting.baidu.com/service/b52cefea4f2a055dc2df909869f6ad8f.mp3", "voa/", "a1.mp3");
   System.out.println(result); 
  }
  } 
}

 

HttpDownloader

public class HttpDownloader {
  private URL url=null;
     public String download(String urlStr){
     StringBuffer sb=new StringBuffer();
     String line=null;
     BufferedReader buffer=null;
     try {
      //創建一個URL對象
    url =new URL(urlStr);
    //創建一個HttpURLConnection對象
    HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();
       //使用IO讀取數據
    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 e2) {
   e2.printStackTrace();
   }
  }
  return sb.toString();
     }
     /*
      * 該方法返回整型-1:代表文件下載錯誤 0:代表文件下載成功 1:代表文件已存在
      *
      */
     public int downFile(String urlStr,String path,String fileName)
   {    
      InputStream inputStream=null;
      try {
   FileUtils fileUtil=new FileUtils();
   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();
   return -1;
  }finally
  {
   try {
     inputStream.close(); 
   } catch (Exception e2) {
   e2.printStackTrace();
   }
  }
  return 0;
     }
     /**
   * 根據URL得到輸入流
   *
   * @param urlStr
   * @return
   * @throws MalformedURLException
   * @throws IOException
   */
  public InputStream getInputStreamFromUrl(String urlStr)
    throws MalformedURLException, IOException {
   url = new URL(urlStr);
   HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
   InputStream inputStream = urlConn.getInputStream();
   return inputStream;
  }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章