Android利用Http下載文件

Android利用Http下載文件javascript

1、場景java

   下載存文本文件和下載如mp3等大容量的文件android

界面網絡


2、代碼編寫app

 1.AndroidMainfest.xml中配置ide

主要是解決網絡權限和寫SDCard的權限this

 

 

Java代碼  收藏代碼url

  1. <?xml version="1.0" encoding="utf-8"?>  spa

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  .net

  3.     package="linys.download" android:versionCode="1" android:versionName="1.0">  

  4.     <uses-sdk android:minSdkVersion="8" />  

  5.   

  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  

  7.         <activity android:name=".Download" android:label="@string/app_name">  

  8.             <intent-filter>  

  9.                 <action android:name="android.intent.action.MAIN" />  

  10.                 <category android:name="android.intent.category.LAUNCHER" />  

  11.             </intent-filter>  

  12.         </activity>  

  13.     </application>  

  14.     <!-- 訪問網絡和操做SD卡 加入的兩個權限配置 -->  

  15.         <uses-permission android:name="android.permission.INTERNET" />  

  16.         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

  17. </manifest>  

 

2.Activity編寫

 利用Http協議下載文件並存儲到SDCard
    1.建立一個URL對象
    2.經過URL對象,建立一個HttpURLConnection對象
    3.獲得InputStream
    4.從InputStream當中讀取數據
    存到SDCard
    1.取得SDCard路徑
    2.利用讀取大文件的IO讀法,讀取文件

Java代碼  收藏代碼

  1. package linys.download;  

  2.   

  3. import java.io.BufferedReader;  

  4. import java.io.File;  

  5. import java.io.FileOutputStream;  

  6. import java.io.IOException;  

  7. import java.io.InputStream;  

  8. import java.io.InputStreamReader;  

  9. import java.io.OutputStream;  

  10. import java.net.HttpURLConnection;  

  11. import java.net.MalformedURLException;  

  12. import java.net.URL;  

  13.   

  14. import android.app.Activity;  

  15. import android.os.Bundle;  

  16. import android.os.Environment;  

  17. import android.view.View;  

  18. import android.view.View.OnClickListener;  

  19. import android.widget.Button;  

  20. /** 

  21.  *  

  22.  * @Project : Android_MyDownload 

  23.  * @Desciption: 利用Http協議下載文件並存儲到SDCard 

  24.     1.建立一個URL對象 

  25.     2.經過URL對象,建立一個HttpURLConnection對象 

  26.     3.獲得InputStream 

  27.     4.從InputStream當中讀取數據 

  28.     存到SDCard 

  29.     1.取得SDCard路徑 

  30.     2.利用讀取大文件的IO讀法,讀取文件 

  31.  *  

  32.  * @Author: LinYiSong 

  33.  * @Date: 2011-3-25~2011-3-25 

  34.  */  

  35. public class MyDownload extends Activity {  

  36.       

  37.     private Button downFileBtn;  

  38.     private Button downMP3Btn;  

  39.     /** Called when the activity is first created. */  

  40.     @Override  

  41.     public void onCreate(Bundle savedInstanceState) {  

  42.         super.onCreate(savedInstanceState);  

  43.         setContentView(R.layout.main);  

  44.           

  45.         downFileBtn=(Button)this.findViewById(R.id.downFile);  

  46.         downMP3Btn=(Button)this.findViewById(R.id.downMP3);  

  47.           

  48.         downFileBtn.setOnClickListener(new DownFileClickListener());  

  49.         downMP3Btn.setOnClickListener(new DownMP3ClickListener());  

  50.     }  

  51.       

  52.     /** 

  53.      *  

  54.      * @Project: Android_MyDownload 

  55.      * @Desciption: 只能讀取文本文件,讀取mp3文件會出現內存溢出現象 

  56.      * @Author: LinYiSong 

  57.      * @Date: 2011-3-25~2011-3-25 

  58.      */  

  59.     class DownFileClickListener implements OnClickListener{  

  60.         @Override  

  61.         public void onClick(View v) {  

  62.             String urlStr="http://172.17.54.91:8080/download/down.txt";  

  63.             try {  

  64.                 /* 

  65.                  * 經過URL取得HttpURLConnection 

  66.                  * 要網絡鏈接成功,需在AndroidMainfest.xml中進行權限配置 

  67.                  * <uses-permission android:name="android.permission.INTERNET" /> 

  68.                  */  

  69.                 URL url=new URL(urlStr);  

  70.                 HttpURLConnection conn=(HttpURLConnection)url.openConnection();  

  71.                 //取得inputStream,並進行讀取  

  72.                 InputStream input=conn.getInputStream();  

  73.                 BufferedReader in=new BufferedReader(new InputStreamReader(input));  

  74.                 String line=null;  

  75.                 StringBuffer sb=new StringBuffer();  

  76.                 while((line=in.readLine())!=null){  

  77.                     sb.append(line);  

  78.                 }  

  79.                 System.out.println(sb.toString());  

  80.                   

  81.             } catch (MalformedURLException e) {  

  82.                 e.printStackTrace();  

  83.             } catch (IOException e) {  

  84.                 e.printStackTrace();  

  85.             }  

  86.         }  

  87.     }  

  88.     /** 

  89.      *  

  90.      * @Project: Android_MyDownload 

  91.      * @Desciption: 讀取任意文件,並將文件保存到手機SDCard 

  92.      * @Author: LinYiSong 

  93.      * @Date: 2011-3-25~2011-3-25 

  94.      */  

  95.     class DownMP3ClickListener implements OnClickListener{  

  96.   

  97.         @Override  

  98.         public void onClick(View v) {  

  99.             String urlStr="http://172.17.54.91:8080/download/1.mp3";  

  100.             String path="file";  

  101.             String fileName="2.mp3";  

  102.             OutputStream output=null;  

  103.             try {  

  104.                 /* 

  105.                  * 經過URL取得HttpURLConnection 

  106.                  * 要網絡鏈接成功,需在AndroidMainfest.xml中進行權限配置 

  107.                  * <uses-permission android:name="android.permission.INTERNET" /> 

  108.                  */  

  109.                 URL url=new URL(urlStr);  

  110.                 HttpURLConnection conn=(HttpURLConnection)url.openConnection();  

  111.                 //取得inputStream,並將流中的信息寫入SDCard  

  112.                   

  113.                 /* 

  114.                  * 寫前準備 

  115.                  * 1.在AndroidMainfest.xml中進行權限配置 

  116.                  * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

  117.                  * 取得寫入SDCard的權限 

  118.                  * 2.取得SDCard的路徑: Environment.getExternalStorageDirectory() 

  119.                  * 3.檢查要保存的文件上是否已經存在 

  120.                  * 4.不存在,新建文件夾,新建文件 

  121.                  * 5.將input流中的信息寫入SDCard 

  122.                  * 6.關閉流 

  123.                  */  

  124.                 String SDCard=Environment.getExternalStorageDirectory()+"";  

  125.                 String pathName=SDCard+"/"+path+"/"+fileName;//文件存儲路徑  

  126.                   

  127.                 File file=new File(pathName);  

  128.                 InputStream input=conn.getInputStream();  

  129.                 if(file.exists()){  

  130.                     System.out.println("exits");  

  131.                     return;  

  132.                 }else{  

  133.                     String dir=SDCard+"/"+path;  

  134.                     new File(dir).mkdir();//新建文件夾  

  135.                     file.createNewFile();//新建文件  

  136.                     output=new FileOutputStream(file);  

  137.                     //讀取大文件  

  138.                     byte[] buffer=new byte[4*1024];  

  139.                     while(input.read(buffer)!=-1){  

  140.                         output.write(buffer);  

  141.                     }  

  142.                     output.flush();  

  143.                 }  

  144.             } catch (MalformedURLException e) {  

  145.                 e.printStackTrace();  

  146.             } catch (IOException e) {  

  147.                 e.printStackTrace();  

  148.             }finally{  

  149.                 try {  

  150.                         output.close();  

  151.                         System.out.println("success");  

  152.                     } catch (IOException e) {  

  153.                         System.out.println("fail");  

  154.                         e.printStackTrace();  

  155.                     }  

  156.             }  

  157.         }  

  158.           

  159.     }  

  160. }  

 

 

 

相關文章
相關標籤/搜索