温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-06-29 14:10:38 来源:亿速云 阅读:564 作者:Leah 栏目:移动开发

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

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

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

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下载文件到手机问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI