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;
        }
  • 相关阅读:
    DBGridEh表尾显示合计 .....
    03004_Web开发
    雷林鹏分享:Flask请求对象
    雷林鹏分享:Flask静态文件
    雷林鹏分享:Flask模板
    雷林鹏分享:Flask HTTP方法
    雷林鹏分享:Flask URL构建
    雷林鹏分享:Flask变量规则
    雷林鹏分享:Flask路由
    雷林鹏分享:Flask应用程序
  • 原文地址:https://www.cnblogs.com/rdchen/p/13896903.html
Copyright © 2011-2022 走看看