Here you can find the source of downloadDirectory(URL dirUrl, File destDir)
Downloads url directory to target directory (url directory will be subdirectory of target directory); example: dowloadDirectory(new URL("http://dir0/dir1/dir2"), new File("/home/junk")) behaves like cp -r dir2 /home/junk/dir2 such that the directory /home/junk/dir2 exists with all its sub dirs and files.
Parameter | Description |
---|---|
dirUrl | a parameter |
destDir | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void downloadDirectory(URL dirUrl, File destDir) throws IOException
//package com.java2s; /*/*from ww w . ja v a 2 s .c o m*/ * Copyright 2009 the original author or authors. * Copyright 2009 SorcerSoft.org. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.net.URLConnection; import java.util.Scanner; import java.util.Vector; public class Main { /** * Downloads url directory to target directory (url directory will be * subdirectory of target directory); example: * * dowloadDirectory(new URL("http://dir0/dir1/dir2"), new File("/home/junk")) * * behaves like * * cp -r dir2 /home/junk/dir2 * * such that the directory /home/junk/dir2 exists with all its sub dirs and * files. * * * @param dirUrl * @param destDir * @throws IOException */ public static void downloadDirectory(URL dirUrl, File destDir) throws IOException { if (!destDir.exists()) throw new IOException(destDir + " does not exist."); if (!destDir.isDirectory()) throw new IOException(destDir + " is not a directory."); Vector<String> dirListing = getFileContents(dirUrl); //printVect(dirListing); String urlDirName = (new File(dirUrl.getFile()).getName()); File destDir2 = new File(destDir, urlDirName); //System.out.println("destDir2 = " + destDir2); for (String line : dirListing) { String[] fields = line.split("\\s"); //System.out.println("=============================="); //printArray(fields); //System.out.println("fields[0] = " + fields[0]); //System.out.println("dirUrl.getFile() = " + dirUrl.getFile()); String fileField = fields[0].replace("\\", "/"); String[] fileFieldArray = fileField.split("/"); String filename = fileFieldArray[fileFieldArray.length - 1]; //System.out.println("filename = " + filename); URL fileUrl = new URL(dirUrl.toString() + "/" + filename); //System.out.println("fileUrl = " + fileUrl); boolean madeDirs = destDir2.mkdirs(); //System.out.println(madeDirs + ": made " + destDir2); destDir2.setWritable(true); destDir2.setReadable(true); destDir2.setExecutable(true); if (fields[1].equals("f")) { File fileTarget = new File(destDir2, new File(fileUrl.getFile()).getName()); //System.out.println("downloading " + fileUrl + " to " + fileTarget); download(fileUrl, fileTarget); } if (fields[1].equals("d")) { downloadDirectory(fileUrl, destDir2); } } } /** * This method was originally implemented by S. A. Burton, but has been * rewritten by E. D. Thompson in a a cleaner more efficient syntax. The * function gets the path to a file with the path file as an argument and * returns the file's contents in a vector of strings where each element of * the vector corresponds to a line in the file. * * @param file * Path to the file * @return Vector containing the file at the path file contents * * @author E. D. Thompson, S. A. Burton * @throws FileNotFoundException */ public static Vector<String> getFileContents(File file) throws FileNotFoundException { Vector<String> fileContents = null; // Check if file exist if (file.exists()) { // Creating a vector of strings fileContents = new Vector<String>(); // Creating a input stream Scanner fin = new Scanner(new FileReader(file)); String line; // Looping over files lines while there are lines to loop over while (fin.hasNext()) { line = fin.nextLine(); fileContents.add(line); } // Closing file input stream fin.close(); } else { // File not found System.out.printf("getFileContents:\nFile, %s, does not exist. Returning a NULL vector of strings\n", file.getAbsolutePath()); } return fileContents; } public static Vector<String> getFileContents(URL url) throws IOException { URLConnection myConnect = url.openConnection(); myConnect.setDoInput(true); myConnect.setDoOutput(true); myConnect.setUseCaches(false); InputStreamReader iSR = new InputStreamReader(myConnect.getInputStream()); BufferedReader bR = new BufferedReader((Reader) iSR); boolean eof = false; Vector<String> fileContents = new Vector<String>(); while (!eof) { String line = bR.readLine(); if (line == null) { eof = true; } else { fileContents.add(line); } } bR.close(); return fileContents; } /** * This method given a URL sourceURL and file path destinationFile writes * the contents of the URL to file. * * @param sourceUrl * Source URL * @param destinationFile * Destination file * @see writeUrlToFile * @throws IOException */ public static void download(URL sourceUrl, File destinationFile) throws IOException { writeUrlToFile(sourceUrl, destinationFile); } /** * Method that writes the content of URL inputURL to file path * locatlInputFile * * @param inputUrl * Input URL * @param localInputFile * File path * @throws IOException */ public static void writeUrlToFile(URL inputUrl, File localInputFile) throws IOException { InputStream is = inputUrl.openStream(); redirectInputStream2File(is, localInputFile); } /** * This method redirects an input stream to a file * * @param is Input stream * @param file File * @throws IOException */ public static void redirectInputStream2File(InputStream is, File file) throws IOException { int bufferSize = 4096; FileOutputStream fos = new FileOutputStream(file); byte[] buffer = new byte[bufferSize]; int bytesRead; while ((bytesRead = is.read(buffer)) != -1) { // EOF fos.write(buffer, 0, bytesRead); } fos.close(); is.close(); } }