public boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = ftpClient.changeWorkingDirectory(directory);
if (flag) {
logger.debug("进入文件夹" + directory + " 成功!");
} else {
logger.debug("进入文件夹" + directory + " 失败!");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
public boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = ftpClient.makeDirectory(dir);
if (flag) {
logger.debug("创建文件夹" + dir + " 成功!");
} else {
logger.debug("创建文件夹" + dir + " 失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public boolean CreateDirecroty(String remote) throws IOException {
boolean success = true;
String directory = remote + "/";
if (!directory.equalsIgnoreCase("/") && !changeWorkingDirectory(new String(directory))) {
int start = 0;
int end = 0;
if (directory.startsWith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexOf("/", start);
String path = "";
String paths = "";
while (true) {
String subDirectory = new String(remote.substring(start, end).getBytes("GBK"), "iso-8859-1");
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
logger.debug("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}
paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
if (end <= start) {
break;
}
}
}
return success;
}
public boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
public boolean uploadFile(File localFile, String remoteFile)
throws IOException {
boolean flag = false;
InputStream in = new FileInputStream(localFile);
String remote = new String(remoteFile.getBytes("GBK"), "iso-8859-1");
try {
if (ftpClient.storeFile(remote, in)) {
flag = true;
logger.debug(localFile.getAbsolutePath() + "上传文件成功!");
} else {
logger.debug(localFile.getAbsolutePath() + "上传文件失败!");
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public boolean uploadFiles(String localPathFileName, String remotePathFileName) throws IOException {
File local = new File(localPathFileName);
if (local.isDirectory()) {
try {
this.ftpClient.changeWorkingDirectory("/");
CreateDirecroty(remotePathFileName);
} catch (Exception e) {
e.printStackTrace();
}
} else {
File local1 = new File(localPathFileName);
uploadFile(local1, remotePathFileName);
}
File sourceFile[] = local.listFiles();
for (int i = 0; i < sourceFile.length; i++) {
if (sourceFile[i].exists()) {
String path = sourceFile[i].getAbsolutePath().substring(localPathFileName.length());
if (sourceFile[i].isDirectory()) {
this.uploadFiles(sourceFile[i].getAbsolutePath(), remotePathFileName + path);
} else {
if (!path.equals("/.DS_Store")) {
File file2 = new File(sourceFile[i].getAbsolutePath());
uploadFile(file2, remotePathFileName + path);
}
}
}
}
return true;
}