zoukankan      html  css  js  c++  java
  • android 通过httpclient下载文件并保存

    代码:(主要针对图片、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;
        }
  • 相关阅读:
    求矩形最大面积
    异或相关知识
    算法模板-线段树区间最值
    矩阵递推关系的建立 and 矩阵快速幂
    二分答案模板
    关于STL,set,vector,map的时间
    域名解析
    CDN配置
    CDN的工作原理及好处
    打赏
  • 原文地址:https://www.cnblogs.com/feijian/p/4596159.html
Copyright © 2011-2022 走看看