在有些項目中我們需要將文件保存在FTP中,
但是在網上介紹FTP上傳的知識總是感覺很混亂,
現在將網上的一些代碼經過整理與測試,拿出來與大家分享。
有些地方寫的可能不是很好,還望大家見諒。
我覺得在向FTP上傳文件的時候上傳文件並不是最難的,而是如何在FTP上創建文件夾。
package com.ftp.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FtpDemo {
/**
* 連接FTP
*
* @param ip
* @param port
* @param userName
* @param password
* @param file
* @param remotePathName
* @param remoteName
*/
private static void upload(String ip, int port, String userName,
String password, File file, String remotePathName, String remoteName) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(ip, port);
ftpClient.login(userName, password);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
// 上傳文件
upload(ftpClient, file, remotePathName, remoteName);
} catch (Exception e) {
System.out.println("fail to upload files : " + e.getMessage());
} finally {
if (ftpClient != null && ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
System.out.println("ftp fails to disconnect : "
+ e.getMessage());
}
}
}
}
/**
* 上傳文件
*
* @param ftpClient
* @param file
* @param remotePathName
* @param remoteName
*/
private static void upload(FTPClient ftpClient, File file,
String remotePathName, String remoteName) throws Exception {
try {
// 1. 切目錄
changeDirectory(ftpClient, remotePathName);
// 2. 傳文件
uploadFile(ftpClient, file, remotePathName, remoteName);
// 3. 切回根目錄
backToRootDirectory(ftpClient);
} catch (IOException e) {
}
}
/**
* 切換目錄
*
* @param ftpClient
* @param path
* @throws IOException
*/
private static void changeDirectory(FTPClient ftpClient, String path)
throws IOException {
int nextSeperator = path.indexOf("/", 1);
String currentPath = null;
if (nextSeperator < 0) {
nextSeperator = path.length();
currentPath = path.substring(1, nextSeperator);
changeDirectory0(ftpClient, currentPath);
return;
} else {
currentPath = path.substring(1, nextSeperator);
changeDirectory0(ftpClient, currentPath);
changeDirectory(ftpClient, path.substring(nextSeperator));
}
}
/**
* 如果切換的目錄不存在 就創建一個新的目錄
*
* @param ftpClient
* @param path
* @throws IOException
*/
private static void changeDirectory0(FTPClient ftpClient, String path)
throws IOException {
if (!ftpClient.changeWorkingDirectory(path)) {
ftpClient.makeDirectory(path);
}
ftpClient.changeWorkingDirectory(path);
}
/**
* 回到根目錄根
*
* @param ftpClient
* @throws IOException
*/
private static void backToRootDirectory(FTPClient ftpClient)
throws IOException {
ftpClient.changeWorkingDirectory("/");
}
/**
* 真正的上傳文件方法
*
* @param ftpClient
* @param file
* @param remotePathName
* @param remoteName
*/
private static void uploadFile(FTPClient ftpClient, File file,
String remotePathName, String remoteName) throws Exception {
String localPathName = file.getAbsolutePath();
FTPFile[] files = ftpClient.listFiles(remoteName);
if (files.length == 1) {
if (!ftpClient.deleteFile(remoteName)) {
throw new Exception("fail to delete remote file ["
+ remotePathName + "] before uploading");
}
}
File f = new File(localPathName);
InputStream is = new FileInputStream(f);
if (!ftpClient.storeFile(remoteName, is)) {
is.close();
}
is.close();
}
}