public boolean uploadFile() throws UploadException{
//the name of file will be changed to avoid iteration
String fileName = path.substring(path.lastIndexOf("\\")+1);
try{
ftpClient.put(path,fileName);
return true;
}catch(Exception e){
throw new UploadException(e.toString(),"FileUpload.uploadFile()");
}
}
public boolean uploadFile(String name) throws UploadException{
//the name of file will be changed to avoid iteration
String fileName = name.substring(name.lastIndexOf("\\")+1);
try{
ftpClient.put(name,fileName);
return true;
}catch(Exception e){
throw new UploadException(e.toString(),"FileUpload.uploadFile(name)");
}
}
}
*
* @(#)DirectoryUpload.java 1.0 04/11/17
*
* 上传文件夹
*/
package com.head.upload;
import com.enterprisedt.net.ftp.*;
import java.io.*;
public class DirectoryUpload extends Upload {
DirectoryUpload(){}
public boolean uploadFile(String dir) throws UploadException{
//the name of directory will be changed to avoid iteration
String dirName = dir.substring(dir.lastIndexOf("\\")+1);
ftpMkDir(dirName);
try{
String currentDir = ftpClient.pwd();
ftpClient.chdir(dirName);
String[] subFiles = new Directory(dir).fileNames();
for(int i=0;i<subFiles.length;i++) {
String tmpP = dir +"\\" + subFiles;
if(new File(tmpP).isFile()) {
System.out.println("upload file:"+tmpP);
new FileUpload().uploadFile(tmpP);
}
else{
System.out.println("into the directory:"+tmpP);
uploadFile(tmpP);
}
}
ftpClient.chdir(currentDir);
}catch(Exception e){
throw new UploadException(e.toString(),"DirecotryUpload.uploadFile()");
}