import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FTPTool {
public static void main(String[] args) {
if(upLoadFromProduction("192.168.1.190",21,"zhangbo","enss5129.","admin/中文测试/ASD","测试.txt","D:/上传文件新建数据库.txt")){
System.out.println("success");
}else{
System.out.println("error");
}
}
static FTPClient ftp = new FTPClient();
/*** 将本地文件上传到FTP服务器上 **/
public static boolean upLoadFromProduction(String url,// FTP服务器hostname int port,// FTP服务器端口 String username, // FTP登录账号 String password, // FTP登录密码 String path, // FTP服务器保存目录 String filename, // 上传到FTP服务器上的文件名 String orginfilename // 输入流文件名 ){
FileInputStream in;
try {
in = new FileInputStream(new File(orginfilename));
if(uploadFile(url,port,username,password,path,filename,in)){
return true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
}
return false;
}
/*** Description: 向FTP服务器上传文件* @Version 1.0* @param url FTP服务器hostname* @param port FTP服务器端口* @param username FTP登录账号* @param password FTP登录密码* @param path FTP服务器保存目录* @param filename 上传到FTP服务器上的文件名* @param input 输入流* @return 成功返回true,否则返回false **/
public static boolean uploadFile(String url,// FTP服务器hostname int port,// FTP服务器端口 String username, // FTP登录账号 String password, // FTP登录密码 String path, // FTP服务器保存目录 String filename, // 上传到FTP服务器上的文件名 InputStream input // 输入流
){
ftp.setControlEncoding("GBK");
try {
int reply;
ftp.connect(url);// 连接FTP服务器 // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 ftp.login(username, password);// 登录 reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return false;
}
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
//判断ftp服务器目录是否存在 System.out.println(path);
if(!existFile(path)){
System.out.println("目录不存在");
CreateDirecroty(path);
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return true;
}
//创建目录 public static boolean makeDirectory(String dir) {
boolean flag = true;
try {
flag = ftp.makeDirectory(dir);
if (flag) {
System.out.println("创建文件夹" + dir + " 成功!");
} else {
System.out.println("创建文件夹" + dir + " 失败!");
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//改变目录路径 public static boolean changeWorkingDirectory(String directory) {
boolean flag = true;
try {
flag = ftp.changeWorkingDirectory(directory);
if (flag) {
System.out.println("进入文件夹" + directory + " 成功!");
} else {
System.out.println("进入文件夹" + directory + " 失败!");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
return flag;
}
//创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建 public static boolean CreateDirecroty(String remote) throws IOException {
boolean success = true;
String directory = remote + "/";
// String directory = remote.substring(0, remote.lastIndexOf("/") + 1); // 如果远程目录不存在,则递归创建远程服务器目录 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 = remote.substring(start, end);
path = path + "/" + subDirectory;
if (!existFile(path)) {
if (makeDirectory(subDirectory)) {
changeWorkingDirectory(subDirectory);
} else {
System.out.println("创建目录[" + subDirectory + "]失败");
changeWorkingDirectory(subDirectory);
}
} else {
changeWorkingDirectory(subDirectory);
}
paths = paths + "/" + subDirectory;
start = end + 1;
end = directory.indexOf("/", start);
// 检查所有目录是否创建完毕 if (end <= start) {
break;
}
}
}
return success;
}
//判断ftp服务器文件是否存在 public static boolean existFile(String path) throws IOException {
boolean flag = false;
FTPFile[] ftpFileArr = ftp.listFiles(path);
if (ftpFileArr.length > 0) {
flag = true;
}
return flag;
}
}