import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.junit.Test;
public class FtpUtil {
/**
* Description: 向FTP伺服器上傳檔案 @Version1.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 // 輸入流
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);// 連接配接FTP伺服器
// 如果采用預設端口,可以使用ftp.connect(url)的方式直接連接配接FTP伺服器
ftp.login(username, password);// 登入
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 從FTP伺服器下載下傳檔案 @Version1.0
*
* @param url
* FTP伺服器hostname
* @param port
* FTP伺服器端口
* @param username
* FTP登入賬号
* @param password
* FTP登入密碼
* @param remotePath
* FTP伺服器上的相對路徑
* @param fileName
* 要下載下傳的檔案名
* @param localPath
* 下載下傳後儲存到本地的路徑
* @return
*/
public static boolean downFile(String url, // FTP伺服器hostname
int port, // FTP伺服器端口
String username, // FTP登入賬号
String password, // FTP登入密碼
String remotePath, // FTP伺服器上的相對路徑
String fileName, // 要下載下傳的檔案名
String localPath// 下載下傳後儲存到本地的路徑
) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用預設端口,可以使用ftp.connect(url)的方式直接連接配接FTP伺服器
ftp.login(username, password);// 登入
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 轉移到FTP伺服器目錄
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* 将FTP伺服器上檔案下載下傳到本地
*
*/
@Test
public void testDownFile() {
try {
boolean flag = downFile("127.0.0.1", 21, "root", "root", "test", "test.txt", "D:/");
System.out.println(flag);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将本地檔案上傳到FTP伺服器上
*
*/
@Test
public void testUpLoadFromDisk() {
try {
FileInputStream in = new FileInputStream(new File("D:/test.txt"));
boolean flag = uploadFile("127.0.0.1", 21, "root", "root", "test", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 在FTP伺服器上生成一個檔案,并将一個字元串寫入到該檔案中
*
*/
@Test
public void testUpLoadFromString() {
try {
String str = "這是要寫入的字元串!";
InputStream input = new ByteArrayInputStream(str.getBytes("utf-8"));
boolean flag = uploadFile("127.0.0.1", 21, "root", "root", "test", "test.txt", input);
System.out.println(flag);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}