189 8069 5689

Android中怎么利用Http下载文件到手机

Android 中怎么利用Http下载文件到手机,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

为商城等地区用户提供了全套网页设计制作服务,及商城网站建设行业解决方案。主营业务为网站制作、成都网站设计、商城网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

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

  

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

public String gettextfilestring(String url){                 InputStream input =getinputStream(url);                 StringBuffer sb = new StringBuffer("");                 BufferedReader bfr = new BufferedReader(new InputStreamReader(input));                 String line = "";                 try {                         while((line=bfr.readLine())!=null){                                 sb.append(line);                         }                                         } catch (IOException e) {                         toasterror("流文件读写错误");                         e.printStackTrace();                 }finally{                         try {                                 bfr.close();                         } catch (IOException e) {                                 toasterror("流文件未能正常关闭");                                 e.printStackTrace();                         }                 }                 return sb.toString(); }

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

public void downFiletoDecive(String url,String filename){                     if((url!=null&&!"".equals(url))&&(filename!=null&&!"".equals(filename))){                             InputStream input = getinputStream(url);                             FileOutputStream outStream = null;                             try {                                     outStream = c.openFileOutput(filename, Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);                                     int temp = 0;                                     byte[] data = new byte[1024];                                     while((temp = input.read(data))!=-1){                                             outStream.write(data, 0, temp);                                     }                             } catch (FileNotFoundException e) {                                     toasterror("请传入正确的上下文");                                     e.printStackTrace();                             } catch (IOException e) {                                     toastemessage("读写错误");                                     e.printStackTrace();                             }finally{                                     try {                                             outStream.flush();                                             outStream.close();                                     } catch (IOException e) {                                             toasterror("流文件未能正常关闭");                                             e.printStackTrace();                                     }                                                                 }                     }                     toastemessage("下载成功");             }

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

public void downFiletoSDCard(String url,String path,String filename){                                 if((url!=null&&!"".equals(url))&&(path!=null)&&(filename!=null&&!"".equals(filename))){                                                                 InputStream input = getinputStream(url);                                 downloader(input, path, filename);                                         }else{                                 /*                                  * 对不合发的参数做处理                                  */                                 if(url==null||"".equals(url)){                                         toasterror("url不能为空或为“”");                                 }                                 if(path==null){                                         toasterror("path不能为空");                                 }                                 if(filename==null||"".equals(filename)){                                         toasterror("filename不能为空");                                 }                 }                         }

关于Android 中怎么利用Http下载文件到手机问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享名称:Android中怎么利用Http下载文件到手机
URL标题:http://cdxtjz.cn/article/gshpgc.html

其他资讯