Download files and download files successfully admitted to another directory

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

  

void moveToSuccessFolders Private (fromPath String, String fileName, String toPath) { 
        String startPath the File.separator + + fileName = fromPath; 
        String EndPath = toPath; 
        the try { 
            File startFile = new new File (startPath); // if file exists copy failed 
            File tmpFile = new file (endPath); // get the file folder path 
            (! tmpFile.exists ()) if { // determines whether to create a folder without creating a new folder is created 
                tmpFile.mkdirs (); 
            } 
            iF (startFile. renameTo (new new File (EndPath the File.separator + + startFile.getName ()))) { 

            } the else { 
                logger.info (startFile.getName () + "File IS failed to Move!"); 
            } 
        } the catch (Exception E) {
            e.printStackTrace (); 
        } 
    }

  

Guess you like

Origin www.cnblogs.com/james-roger/p/11727857.html