使用hutool進(jìn)行ftp文件下載和上傳詳細(xì)代碼示例
1 引入依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.15</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
2 工具類
package ftp;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.ftp.Ftp;
import cn.hutool.extra.ftp.FtpMode;
import org.apache.commons.lang.StringUtils;
import java.io.File;
import java.io.IOException;
/**
* @author :
* @description :
* @date : 2023/3/10 14:42
*/
public class FtpUtil {
/**
* 下載ftp服務(wù)器上的文件到本地
* @param remoteFile
* @param localFile
* @param ip
* @param port
* @param username
* @param password
* @param ftpMode
* @return 成功則返回字符串:success
*/
public static String download(String remoteFile, String localFile, String ip, Integer port, String username, String password, FtpMode ftpMode) {
if(StringUtils.isBlank(localFile)) {
return "本地保存路徑及名稱不能為空";
}
File lFile = FileUtil.file(localFile);
Ftp ftp = null;
try {
//匿名登錄(無需帳號(hào)密碼的FTP服務(wù)器)
ftp = new Ftp(ip,port == null ? 21 : port,username,password);
if(ftpMode != null) {
ftp.setMode(ftpMode);
}
ftp.download(remoteFile, lFile);
} catch (Exception e) {
return e.getMessage();
} finally {
//關(guān)閉連接
try {
if(ftp != null) ftp.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if(lFile.exists() && lFile.length() > 0) {
return "success";
} else {
lFile.delete();
return "download failure,"+ remoteFile +" maybe not exists !!";
}
}
/**
* 此方法不指定上傳后保存的名稱, 則按本地文件的名稱保存
* @param remoteDir
* @param localFile
* @param ip
* @param port
* @param username
* @param password
* @return 成功則返回字符串:success
*/
public static String upload(String remoteDir, String localFile, String ip, Integer port, String username, String password, FtpMode ftpMode) {
return upload(remoteDir, null, localFile, ip, port, username, password, ftpMode);
}
/**
*
* @param remoteDir 上傳的ftp目錄
* @param remoteFileName 保存到ftp服務(wù)器上的名稱
* @param localFile 本地文件全名稱
* @param ip
* @param port
* @param username
* @param password
* @return 成功則返回字符串:success
*/
public static String upload(String remoteDir, String remoteFileName, String localFile, String ip, Integer port, String username, String password, FtpMode ftpMode) {
if(StringUtils.isBlank(localFile)) {
return "本地文件名稱不能為空";
}
File lFile = FileUtil.file(localFile);
if(!lFile.exists()) {
return "本地文件不存在";
}
Ftp ftp = null;
try {
//匿名登錄(無需帳號(hào)密碼的FTP服務(wù)器)
ftp = new Ftp(ip,port == null ? 21 : port,username,password);
if(ftpMode != null) {
ftp.setMode(ftpMode);
}
if(StringUtils.isBlank(remoteFileName)) {
ftp.upload(remoteDir, lFile);
} else {
ftp.upload(remoteDir, remoteFileName, lFile);
}
} catch (Exception e) {
return e.getMessage();
} finally {
//關(guān)閉連接
try {
if(ftp != null) ftp.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return "success";
}
}
3 測(cè)試
public class FtpTest {
public static void main(String[] args) {
//上傳文件到ftp
String result = FtpUtil.upload("opt/upload","fff.zip", "D:/STM.zip", "192.168.68.55", 21, "ftpuser", "ftpuser!@#123", null);
System.out.println(result);
//下載遠(yuǎn)程文件
String result2 = FtpUtil.download("opt/upload/fff.zip", "D:/bbb.zip", "192.168.68.55", 21, "ftpuser", "ftpuser!@#123", null);
System.out.println(result2);
}
}總結(jié)
到此這篇關(guān)于使用hutool進(jìn)行ftp文件下載和上傳的文章就介紹到這了,更多相關(guān)hutool下載和上傳ftp文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot和spring cloud之間的版本關(guān)系
這篇文章主要介紹了spring boot和spring cloud之間的版本關(guān)系,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08
如何通過Java監(jiān)聽MySQL數(shù)據(jù)的變化
對(duì)于二次開發(fā)來說,很大一部分就找找文件和找數(shù)據(jù)庫的變化情況,下面這篇文章主要給大家介紹了關(guān)于如何通過Java監(jiān)聽MySQL數(shù)據(jù)的變化的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
java面試常見問題---ConcurrentHashMap
ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識(shí),一起看看吧2021-06-06
SpringBoot整合Canal與RabbitMQ監(jiān)聽數(shù)據(jù)變更記錄
這篇文章主要介紹了SpringBoot整合Canal與RabbitMQ監(jiān)聽數(shù)據(jù)變更記錄,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Java.lang.Long.parseLong()方法詳解及示例
這個(gè)java.lang.Long.parseLong(String s) 方法解析字符串參數(shù)s作為有符號(hào)十進(jìn)制長(zhǎng),下面這篇文章主要給大家介紹了關(guān)于Java.lang.Long.parseLong()方法詳解及示例的相關(guān)資料,需要的朋友可以參考下2023-01-01
Java中避免NullPointerException的方法總結(jié)
這篇文章主要介紹了Java中避免NullPointerException的方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-07-07

