• 下载文件并将下载成功的文件考入另一个目录


    //下载资源
                    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();
            }
        }
    

      

  • 相关阅读:
    sqlserver查询某个数据库有多少个表 ,存过,函数,视图
    C# 金额转为大写金额
    C# TextBox中只能输入数字的几种常用方法(C#)
    C# 设置Excel单元格属性
    MS SQL 维护小记
    webapi demo
    远程 TeamViewer
    https://github.com/
    C# Fun 类似委托
    技术点文章收集
  • 原文地址:https://www.cnblogs.com/james-roger/p/11727857.html
Copyright © 2020-2023  润新知