android 通过httpclient下载文件并保存
2015-06-23 19:31
633 查看
代码:(主要针对图片、gif下载无问题)
/** * 下载网络文件 * @param url 请求的文件链接 * @param IsMD5Name 是否MD5加密URL来命名文件名 * @param cachePath 保存的路径 * @return 返回文件的位置path */ public static String getDownloadFile2Cache(String url,boolean IsMD5Name,String cachePath) { String filePath = null; try { HttpGet httpRequest = new HttpGet(url); HttpClient httpclient = new DefaultHttpClient(); HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); HttpEntity entity = response.getEntity(); BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity); InputStream is = bufferedHttpEntity.getContent(); String fileName = null; if(IsMD5Name) //是否以MD5命名下载的文件 { fileName = MD5Util.To32MD5(url)+url.substring(url.lastIndexOf('.')); } else { fileName = url.substring(url.lastIndexOf('/')+1); } FileOutputStream fos = new FileOutputStream(cachePath+"/"+fileName); byte buf[] = new byte[1024]; int numread; while ((numread = is.read(buf)) != -1) { fos.write(buf, 0, numread); } fos.close(); is.close(); filePath = cachePath+"/"+fileName; } catch (IOException e) { e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } return filePath; }
相关文章推荐
- 博客地址http://heyuan110.com
- http://blog.csdn.net/poem_qianmo
- 【各种转】servlet汇总
- 使用HttpSessionListener接口监听Session的创建和失效
- HTTP协议状态码详解(HTTP Status Code)
- linux下的基本网络配置
- 使用android-async-http发送soap异步请求
- 网络请求库Volley详解
- Tomcat 输入http://localhost:8080打不开网页的解决方法
- 【HTTP】初识代理
- Java Client 与C++ Servertcp通信 demo
- 网络协议之socks---子网和公网的穿透
- HTTP - Methods
- 09_android入门_采用android-async-http开源项目的GET方式或POST方式实现登陆案例
- 使用UniversalImageLoader 加载网络图片并附带缓存效果
- Linux抓包工具tcpdump详解
- 谈谈如何训练一个性能不错的深度神经网络
- 7_文件上传实例
- [转]深入理解HTTP协议、HTTP协议原理分析
- 访问网络共享