package com;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
public class URLDownloadFiles {
private String _desRoot = null;
private String _locRoot = null;
private ArrayList<URL> _listURL = null;
private ArrayList<File> _listFile = null;
private final int _buffSize = 512;
public URLDownloadFiles(String desRoot, String locRoot) {
this._desRoot = desRoot;
this._locRoot = locRoot;
inti();
}
/*
* inti
*/
private void inti() {
_listURL = new ArrayList<URL>();
_listFile = new ArrayList<File>();
// Get all files' URL and files' list
if (!getFiles(_desRoot, _listFile, _listURL)) {
return;
}
// copy the dest files to local files
copyFiles();
}
/*
* @param desRoot
* @param listFile
* @param listURL
*/
private boolean getFiles(String desRoot, ArrayList<File> listFile,
ArrayList<URL> listURL) {
File[] fileList = null;
URL[] urlList = null;
File desRootFile = new File(desRoot);
try {
// desRootFile does not exist
if (!desRootFile.exists()) {
System.out.println("DesRoot does not exist");
return false;
}
// desRootFile exist
else {
// desRootFile is not a directory
if (!desRootFile.isDirectory()) {
System.out.println("This is not a directory!");
if (!desRootFile.isFile()) {
System.out
.println("This is not a file too! Plz chk the root!");
return false;
} else {
System.out.println("This is a file!");
return false;
}
}
// desRootFile is a directory, get all sub files and folders
else {
fileList = desRootFile.listFiles();
urlList = new URL[fileList.length];
for (int i = 0; i < fileList.length; i++) {
if (fileList[i].isDirectory()) {
listFile.add(fileList[i]);
urlList[i] = new URL("file:"
+ fileList[i].getPath());
listURL.add(urlList[i]);
// get sub folders
getFiles(fileList[i].getPath(), listFile, listURL);
} else {
listFile.add(fileList[i]);
urlList[i] = new URL("file:"
+ fileList[i].getPath());
listURL.add(urlList[i]);
}
}
}
}
} catch (IOException e) {
System.out.println(e);
return false;
} catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
/*
*/
private boolean copyFiles() {
File dir, locFile;
BufferedInputStream buffIn = null;
BufferedOutputStream buffOut = null;
dir = new File(_locRoot);
try {
// dir doesn't exist
if (!dir.exists()) {
//create dir
dir.mkdir();
//copy new files
for (int i = 0; i < _listFile.size(); i++) {
if (_listFile.get(i).isDirectory()) {
System.out.println(_listFile.get(i) + " isDirectory");
continue;
} else {
locFile = new File(dir, _listFile.get(i).getName());
buffIn = new BufferedInputStream(_listURL.get(i)
.openStream());
buffOut = new BufferedOutputStream(
new FileOutputStream(locFile));
byte buffer[] = new byte[_buffSize];
int count = _buffSize;
while ((count = buffIn.read(buffer)) != -1) {
buffOut.write(buffer, 0, count);
}
buffOut.flush();
buffIn.close();
buffOut.close();
buffIn = null;
buffOut = null;
}
}
}
// dir exist
else {
// delete old files
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
// copy new files
for (int i = 0; i < _listFile.size(); i++) {
if (_listFile.get(i).isDirectory()) {
System.out.println(_listFile.get(i) + " isDirectory");
continue;
} else {
locFile = new File(dir, _listFile.get(i).getName());
buffIn = new BufferedInputStream(_listURL.get(i)
.openStream());
buffOut = new BufferedOutputStream(
new FileOutputStream(locFile));
byte buffer[] = new byte[_buffSize];
int count = _buffSize;
while ((count = buffIn.read(buffer)) != -1) {
buffOut.write(buffer, 0, count);
}
buffOut.flush();
buffIn.close();
buffOut.close();
buffIn = null;
buffOut = null;
}
}
}
} catch (IOException e) {
System.out.println(e);
return false;
} catch (Exception e) {
System.out.println(e);
return false;
}
return true;
}
/**
* @param args temp
*/
public static void main(String[] args) {
String desRoot = "127.0.0.1:8080//AnRun//upload//product//20101";
// String desRoot = "//E://api";
String locRoot = "//E://api copy";
URLDownloadFiles test = new URLDownloadFiles(desRoot, locRoot);
// test
for (int i = 0; i < test._listFile.size(); i++) {
if (!test._listFile.get(i).isDirectory()) {
System.out.println(test._listFile.get(i));
System.out.println(test._listURL.get(i));
}
}
}
}