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