zoukankan      html  css  js  c++  java
  • 下载文件并将下载成功的文件考入另一个目录

    //下载资源
                    try {
                        URL urlfile = null;
                        HttpURLConnection httpUrl = null;
                        BufferedInputStream bis = null;
                        BufferedOutputStream bos = null;
                        urlfile = new URL(downloadPath);
                        httpUrl = (HttpURLConnection) urlfile.openConnection();
                        httpUrl.setRequestProperty("Cookie",cookie);//字符串
                        httpUrl.connect();
    //获取文件名
                        Map<String,List<String>> map = httpUrl.getHeaderFields();
                        List<String> disposition = map.get("Content-Disposition");
                        String filename = disposition.get(0);
                        filename = filename.substring(filename.indexOf("=") + 1);
    
                        File f = new File(path + File.separator + filename);
                        bis = new BufferedInputStream(httpUrl.getInputStream());
                        bos = new BufferedOutputStream(new FileOutputStream(f));
                        int len = 2048;
                        byte[] b = new byte[len];
                        while ((len = bis.read(b)) != -1) {
                            bos.write(b, 0, len);
                        }
                        bos.flush();
                        bos.close();
                        bis.close();
                       
                        httpUrl.disconnect();
                        moveToSuccessFolders(path, filename, BaseConfig.fileDownloadSuccess);
                    }catch (IOException e){
                      
                        logger.info("--------LANDSAT FAILED----------downloadId: " + id);
                        e.printStackTrace();
                    }
    

      

     private void moveToSuccessFolders(String fromPath, String fileName, String toPath){
            String startPath = fromPath + File.separator + fileName;
            String endPath = toPath;
            try {
                File startFile = new File(startPath);//如果文件存在 则拷贝失败
                File tmpFile = new File(endPath);//获取文件夹路径
                if(!tmpFile.exists()){//判断文件夹是否创建,没有创建则创建新文件夹
                    tmpFile.mkdirs();
                }
                if (startFile.renameTo(new File(endPath + File.separator + startFile.getName()))) {
    
                } else {
                    logger.info( startFile.getName() + " File is failed to move!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

      

  • 相关阅读:
    [LeetCode] 131. 分割回文串
    [LeetCode] 130. 被围绕的区域
    [LeetCode] 128. 最长连续序列
    [LeetCode] 129. 求根到叶子节点数字之和
    转:阿里巴巴集团技术丛书——淘宝一线团队实践
    转:开源知识库
    转:php使用websocket示例详解
    阿里巴巴上市背后的技术力量
    转:php中实现精确设置session过期时间的方法
    转:Java程序员最常用的8个Java日志框架
  • 原文地址:https://www.cnblogs.com/james-roger/p/11727857.html
Copyright © 2011-2022 走看看