zoukankan      html  css  js  c++  java
  • util之下载文件工具类

    /**
         * 下载url的文件到指定文件路径里面,如果文件父文件夹不存在则自动创建
         * url 下载的http地址
         * path 文件存储地址
         * return 如果文件大小大于2k则返回true
         */
        public static boolean downloadCreateDir(String url,String path){
            HttpURLConnection connection=null;
            InputStream in = null;
            FileOutputStream o=null;
            try{
                URL httpUrl=new URL(url);
                connection = (HttpURLConnection) httpUrl.openConnection();
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("Charset", "gbk");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
                connection.setRequestMethod("GET");
                
                byte[] data=new byte[1024];
                File f=new File(path);
                File parentDir = f.getParentFile();
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }
                if(connection.getResponseCode() == 200){
                    in = connection.getInputStream();
                    o=new FileOutputStream(path);
                    int n=0;
                    while((n=in.read(data))>0){
                        o.write(data, 0, n);
                        o.flush();
                    }
                }
                if(f.length()>2048){  //代表文件大小
                    return true;  //如果文件大于2k则返回true
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }finally{
                try{
                    if(in != null){
                        in.close();
                    }
                }catch(IOException ex){
                    ex.printStackTrace();
                }
                try{o.close();}catch(Exception ex){}
                try{connection.disconnect();}catch(Exception ex){}
            }
            return false;
        }
  • 相关阅读:
    IT 已成为最疯狂的加班行业,没有之一
    编程和音乐真的很像吗?
    jquery:
    内存管理机制/垃圾回收机制:
    处理跨域:
    创建Djongo需要改url的地方:
    数据库连接池 DBUtils:
    C# 使用 SAP NCO3.0 调用SAP RFC函数接口
    java反射机制的原理与简单使用
    java反射机制的原理与简单使用
  • 原文地址:https://www.cnblogs.com/rdchen/p/13896903.html
Copyright © 2011-2022 走看看