分享

Android利用Http下载文件

 安若婧年 2016-01-26

Android利用Http下载文件

一、场景

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

界面


二、代码编写

 1.AndroidMainfest.xml中配置

主要是解决网络权限和写SDCard的权限

 

 

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas./apk/res/android"  
  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. }  
 

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多