Java將文件上傳到ftp服務器
首先簡單介紹一下什麼是FTP,以及如何在自己的電腦上搭建一個ftp服務器;
—— FTP是文件傳輸協議(FTP)是一種客戶端/服務器協議,用於將文件傳輸到主機或與主機交換文件。它可以使用用戶名和密碼進行身份驗證。匿名 FTP 允許用戶從 Internet 訪問文件,程序和其他數據,而無需用戶 ID 或密碼。總之就是方便一個可以上傳下載文件的地方。
要實現上傳文件,首先要在本地創建一個ftp服務器(win10系統);
一、本地創建一個其他用戶
二、創建FTP目錄
三、賬戶綁定FTP目錄,登錄驗證
四、FTP目錄創建好之後可以通過你選擇的ip 進行訪問 ftp://ip地址,賬號密碼就是你所設置的用戶的賬號密碼
下面寫javaFTP上傳工具類代碼,複製可用
maven依賴
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.1</version>
</dependency>
public class FtpUtil {
/**
* Description: 向FTP服務器上傳文件
* @param host FTP服務器hostname
* @param port FTP服務器端口
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param basePath FTP服務器基礎目錄
* @param filePath FTP服務器文件存放路徑。文件的路徑爲basePath+filePath
* @param filename 上傳到FTP服務器上的文件名
* @param input 輸入流
* @return 成功返回true,否則返回false
*/
public static boolean uploadFile(String host, int port, String username, String password, String basePath,
String filePath, String filename, InputStream input) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);// 連接FTP服務器
// 如果採用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
ftp.login(username, password);// 登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//切換到上傳目錄
if (!ftp.changeWorkingDirectory(basePath+filePath)) {
//如果目錄不存在創建目錄
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)) continue;
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) { //進不去目錄,說明該目錄不存在
if (!ftp.makeDirectory(tempPath)) { //創建目錄
//如果創建文件目錄失敗,則返回
System.out.println("創建文件目錄"+tempPath+"失敗");
return result;
} else {
//目錄存在,則直接進入該目錄
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
//設置上傳文件的類型爲二進制類型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上傳文件
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
/**
* Description: 從FTP服務器下載文件
* @param host FTP服務器hostname
* @param port FTP服務器端口
* @param username FTP登錄賬號
* @param password FTP登錄密碼
* @param remotePath FTP服務器上的相對路徑
* @param fileName 要下載的文件名
* @param localPath 下載後保存到本地的路徑
* @return
*/
public static boolean downloadFile(String host, int port, String username, String password, String remotePath,
String fileName, String localPath) {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
// 如果採用默認端口,可以使用ftp.connect(host)的方式直接連接FTP服務器
ftp.login(username, password);// 登錄
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
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();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
//ftp上傳文件測試main函數
public static void main(String[] args) {
//上傳
try {
FileInputStream in=new FileInputStream(new File("D:\\text.txt"));
boolean flag = uploadFile("ip", 21, "username", "password", "/text","/wenjian", "hello.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//下載
boolean b = downloadFile("ip", 21, "username", "password", "/text/wenjian", "hello.txt", "D://");
System.out.println(b);
}
java代碼中都有註釋,就不解釋了,下面有一個main 方法,可以直接進行測試。以上就是使用java向FTP文件上傳的全部內容。