Android Http下载文件到手机内存与SDCard

移动开发 Android
Android 访问Http下载文件到本地一共分两步,使用gettextfilestring(String url)获取文本文件内容和分别下载到内存和SDCard。

访问Internet和保存文件到SDCard上,首先要在mainifest.xml文件中加上下面的权限。

  1. <uses-permission android:name="android.permission.INTERNET"/> 
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
  3. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 

gettextfilestring(String url)获取文本文件内:

  1. public String gettextfilestring(String url){ 
  2.                 InputStream input =getinputStream(url); 
  3.                 StringBuffer sb = new StringBuffer(""); 
  4.                 BufferedReader bfr = new BufferedReader(new InputStreamReader(input)); 
  5.                 String line = ""
  6.                 try { 
  7.                         while((line=bfr.readLine())!=null){ 
  8.                                 sb.append(line); 
  9.                         } 
  10.                         
  11.                 } catch (IOException e) { 
  12.                         toasterror("流文件读写错误"); 
  13.                         e.printStackTrace(); 
  14.                 }finally
  15.                         try { 
  16.                                 bfr.close(); 
  17.                         } catch (IOException e) { 
  18.                                 toasterror("流文件未能正常关闭"); 
  19.                                 e.printStackTrace(); 
  20.                         } 
  21.                 } 
  22.                 return sb.toString(); 

downFiletoDecive(String url,String filename)方法下载文件到设备内存,下载的文件在应用的路径file下:

  1. public void downFiletoDecive(String url,String filename){ 
  2.                     if((url!=null&&!"".equals(url))&&(filename!=null&&!"".equals(filename))){ 
  3.                             InputStream input = getinputStream(url); 
  4.                             FileOutputStream outStream = null
  5.                             try { 
  6.                                     outStream = c.openFileOutput(filename, Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE); 
  7.                                     int temp = 0
  8.                                     byte[] data = new byte[1024]; 
  9.                                     while((temp = input.read(data))!=-1){ 
  10.                                             outStream.write(data, 0, temp); 
  11.                                     } 
  12.                             } catch (FileNotFoundException e) { 
  13.                                     toasterror("请传入正确的上下文"); 
  14.                                     e.printStackTrace(); 
  15.                             } catch (IOException e) { 
  16.                                     toastemessage("读写错误"); 
  17.                                     e.printStackTrace(); 
  18.                             }finally
  19.                                     try { 
  20.                                             outStream.flush(); 
  21.                                             outStream.close(); 
  22.                                     } catch (IOException e) { 
  23.                                             toasterror("流文件未能正常关闭"); 
  24.                                             e.printStackTrace(); 
  25.                                     } 
  26.                                     
  27.                             } 
  28.                     } 
  29.                     toastemessage("下载成功"); 
  30.             } 

downFiletoSDCard(String url,String path,String filename)下载文件到SDCard中,自定义保存路:

  1. public void downFiletoSDCard(String url,String path,String filename){ 
  2.                 
  3.                 if((url!=null&&!"".equals(url))&&(path!=null)&&(filename!=null&&!"".equals(filename))){ 
  4.                                 
  5.                                 InputStream input = getinputStream(url); 
  6.                                 downloader(input, path, filename); 
  7.                         
  8.                 }else
  9.                                 /* 
  10.                                  * 对不合发的参数做处理 
  11.                                  */ 
  12.                                 if(url==null||"".equals(url)){ 
  13.                                         toasterror("url不能为空或为“”"); 
  14.                                 } 
  15.                                 if(path==null){ 
  16.                                         toasterror("path不能为空"); 
  17.                                 } 
  18.                                 if(filename==null||"".equals(filename)){ 
  19.                                         toasterror("filename不能为空"); 
  20.                                 } 
  21.                 } 
  22.                 
  23.         } 
责任编辑:徐川 来源: OSChina
相关推荐

2021-09-30 22:40:03

手机内存技术

2013-02-19 11:12:53

IE10

2022-01-17 21:19:25

手机内存运存

2021-06-10 10:24:37

WindowsiPhone苹果手机

2009-07-06 16:18:51

Servlet下载文件

2010-01-28 14:28:03

Android创建sd

2015-09-29 09:25:20

HTTP网络协议

2015-10-08 09:38:24

HTTP网络协议文件传输

2015-09-28 13:39:13

Http网络协议HTTP

2015-10-10 16:46:14

HTTP网络协议文件传输

2015-10-09 09:41:24

HTTP网络协议文件传输

2021-11-15 23:47:19

手机内存技术

2021-03-19 18:13:21

手机内存软件

2017-03-09 14:42:21

LinuxsFTP上传下载

2021-07-22 23:19:07

手机内存存储

2009-10-09 09:42:07

虚拟机内存

2010-03-04 14:32:24

Python自动下载文

2011-05-26 15:41:25

java虚拟机

2009-03-11 09:28:18

文件名乱码下载浏览器PHP
点赞
收藏

51CTO技术栈公众号