Java連接服務(wù)器的兩種方式SFTP和FTP
區(qū)別
FTP是一種文件傳輸協(xié)議,一般是為了方便數(shù)據(jù)共享的。包括一個FTP服務(wù)器和多個FTP客戶端。FTP客戶端通過FTP協(xié)議在服務(wù)器上下載資源。FTP客戶端通過FTP協(xié)議在服務(wù)器上下載資源。而一般要使用FTP需要在服務(wù)器上安裝FTP服務(wù)。
而SFTP協(xié)議是在FTP的基礎(chǔ)上對數(shù)據(jù)進(jìn)行加密,使得傳輸?shù)臄?shù)據(jù)相對來說更安全,但是傳輸?shù)男时菷TP要低,傳輸速度更慢(不過現(xiàn)實使用當(dāng)中,沒有發(fā)現(xiàn)多大差別)。SFTP和SSH使用的是相同的22端口,因此免安裝直接可以使用。
總結(jié):
一;FTP要安裝,SFTP不要安裝。
二;SFTP更安全,但更安全帶來副作用就是的效率比FTP要低。
FtpUtil
<!--ftp文件上傳-->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
@Component
public class FtpUtil {
private static final Logger logger = LoggerFactory.getLogger(FtpUtil.class);
//ftp服務(wù)器地址
@Value("${ftp.server}")
private String hostname;
//ftp服務(wù)器端口
@Value("${ftp.port}")
private int port;
//ftp登錄賬號
@Value("${ftp.userName}")
private String username;
//ftp登錄密碼
@Value("${ftp.userPassword}")
private String password;
/**
* 初始化FTP服務(wù)器
*
* @return
*/
public FTPClient getFtpClient() {
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
try {
//設(shè)置連接超時時間
ftpClient.setDataTimeout(1000 * 120);
logger.info("連接FTP服務(wù)器中:" + hostname + ":" + port);
//連接ftp服務(wù)器
ftpClient.connect(hostname, port);
//登錄ftp服務(wù)器
ftpClient.login(username, password);
// 是否成功登錄服務(wù)器
int replyCode = ftpClient.getReplyCode();
if (FTPReply.isPositiveCompletion(replyCode)) {
logger.info("連接FTP服務(wù)器成功:" + hostname + ":" + port);
} else {
logger.error("連接FTP服務(wù)器失敗:" + hostname + ":" + port);
closeFtpClient(ftpClient);
}
} catch (IOException e) {
logger.error("連接ftp服務(wù)器異常", e);
}
return ftpClient;
}
/**
* 上傳文件
*
* @param pathName 路徑
* @param fileName 文件名
* @param inputStream 輸入文件流
* @return
*/
public boolean uploadFileToFtp(String pathName, String fileName, InputStream inputStream) {
boolean isSuccess = false;
FTPClient ftpClient = getFtpClient();
try {
if (ftpClient.isConnected()) {
logger.info("開始上傳文件到FTP,文件名稱:" + fileName);
//設(shè)置上傳文件類型為二進(jìn)制,否則將無法打開文件
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
//路徑切換,如果目錄不存在創(chuàng)建目錄
if (!ftpClient.changeWorkingDirectory(pathName)) {
boolean flag = this.changeAndMakeWorkingDir(ftpClient, pathName);
if (!flag) {
logger.error("路徑切換(創(chuàng)建目錄)失敗");
return false;
}
}
//設(shè)置被動模式,文件傳輸端口設(shè)置(如上傳文件夾成功,不能上傳文件,注釋這行,否則報錯refused:connect)
ftpClient.enterLocalPassiveMode();
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
isSuccess = true;
logger.info(fileName + "文件上傳到FTP成功");
} else {
logger.error("FTP連接建立失敗");
}
} catch (Exception e) {
logger.error(fileName + "文件上傳異常", e);
} finally {
closeFtpClient(ftpClient);
closeStream(inputStream);
}
return isSuccess;
}
/**
* 刪除文件
*
* @param pathName 路徑
* @param fileName 文件名
* @return
*/
public boolean deleteFile(String pathName, String fileName) {
boolean flag = false;
FTPClient ftpClient = getFtpClient();
try {
logger.info("開始刪除文件");
if (ftpClient.isConnected()) {
//路徑切換
ftpClient.changeWorkingDirectory(pathName);
ftpClient.enterLocalPassiveMode();
ftpClient.dele(fileName);
ftpClient.logout();
flag = true;
logger.info("刪除文件成功");
} else {
logger.info("刪除文件失敗");
}
} catch (Exception e) {
logger.error(fileName + "文件刪除異常", e);
} finally {
closeFtpClient(ftpClient);
}
return flag;
}
/**
* 關(guān)閉FTP連接
*
* @param ftpClient
*/
public void closeFtpClient(FTPClient ftpClient) {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
logger.error("關(guān)閉FTP連接異常", e);
}
}
}
/**
* 關(guān)閉文件流
*
* @param closeable
*/
public void closeStream(Closeable closeable) {
if (null != closeable) {
try {
closeable.close();
} catch (IOException e) {
logger.error("關(guān)閉文件流異常", e);
}
}
}
/**
* 路徑切換(沒有則創(chuàng)建)
*
* @param ftpClient FTP服務(wù)器
* @param path 路徑
*/
public void changeAndMakeWorkingDir(FTPClient ftpClient, String path) {
boolean flag = false;
try {
String[] path_array = path.split("/");
for (String s : path_array) {
boolean b = ftpClient.changeWorkingDirectory(s);
if (!b) {
ftpClient.makeDirectory(s);
ftpClient.changeWorkingDirectory(s);
}
}
flag = true;
} catch (IOException e) {
logger.error("路徑切換異常", e);
}
return flag;
}
/**
* 從FTP下載到本地文件夾
*
* @param ftpClient FTP服務(wù)器
* @param pathName 路徑
* @param targetFileName 文件名
* @param localPath 本地路徑
* @return
*/
public boolean downloadFile(FTPClient ftpClient, String pathName, String targetFileName, String localPath) {
boolean flag = false;
OutputStream os = null;
try {
System.out.println("開始下載文件");
//切換FTP目錄
ftpClient.changeWorkingDirectory(pathName);
ftpClient.enterLocalPassiveMode();
FTPFile[] ftpFiles = ftpClient.listFiles();
for (FTPFile file : ftpFiles) {
String ftpFileName = file.getName();
if (targetFileName.equalsIgnoreCase(ftpFileName)) {
File localFile = new File(localPath + targetFileName);
os = new FileOutputStream(localFile);
ftpClient.retrieveFile(file.getName(), os);
os.close();
}
}
ftpClient.logout();
flag = true;
logger.info("下載文件成功");
} catch (Exception e) {
logger.error("下載文件失敗", e);
} finally {
closeFtpClient(ftpClient);
closeStream(os);
}
return flag;
}
}SFTPUtil
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;
@Component
public class SFTPUtil {
private static final Logger logger = LoggerFactory.getLogger(SFTPUtil.class);
private Session session = null;
private ChannelSftp channel = null;
private int timeout = 60000;
/**
* 連接sftp服務(wù)器
*/
public boolean connect(String ftpUsername, String ftpAddress, int ftpPort, String ftpPassword) {
boolean isSuccess = false;
if (channel != null) {
System.out.println("通道不為空");
return false;
}
//創(chuàng)建JSch對象
JSch jSch = new JSch();
try {
// 根據(jù)用戶名,主機(jī)ip和端口獲取一個Session對象
session = jSch.getSession(ftpUsername, ftpAddress, ftpPort);
//設(shè)置密碼
session.setPassword(ftpPassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
//為Session對象設(shè)置properties
session.setConfig(config);
//設(shè)置超時
session.setTimeout(timeout);
//通過Session建立連接
session.connect();
System.out.println("Session連接成功");
// 打開SFTP通道
channel = (ChannelSftp) session.openChannel("sftp");
// 建立SFTP通道的連接
channel.connect();
System.out.println("通道連接成功");
isSuccess = true;
} catch (JSchException e) {
logger.error("連接服務(wù)器異常", e);
}
return isSuccess;
}
/**
* 關(guān)閉連接
*/
public void close() {
//操作完畢后,關(guān)閉通道并退出本次會話
if (channel != null && channel.isConnected()) {
channel.disconnect();
}
if (session != null && session.isConnected()) {
session.disconnect();
}
}
/**
* 文件上傳
* 采用默認(rèn)的傳輸模式:OVERWRITE
* @param src 輸入流
* @param dst 上傳路徑
* @param fileName 上傳文件名
*
* @throws SftpException
*/
public boolean upLoadFile(InputStream src, String dst, String fileName) throws SftpException {
boolean isSuccess = false;
try {
if(createDir(dst)) {
channel.put(src, fileName);
isSuccess = true;
}
} catch (SftpException e) {
logger.error(fileName + "文件上傳異常", e);
}
return isSuccess;
}
/**
* 創(chuàng)建一個文件目錄
*
* @param createpath 路徑
* @return
*/
public boolean createDir(String createpath) {
boolean isSuccess = false;
try {
if (isDirExist(createpath)) {
channel.cd(createpath);
return true;
}
String pathArry[] = createpath.split("/");
StringBuffer filePath = new StringBuffer("/");
for (String path : pathArry) {
if (path.equals("")) {
continue;
}
filePath.append(path + "/");
if (isDirExist(filePath.toString())) {
channel.cd(filePath.toString());
} else {
// 建立目錄
channel.mkdir(filePath.toString());
// 進(jìn)入并設(shè)置為當(dāng)前目錄
channel.cd(filePath.toString());
}
}
channel.cd(createpath);
isSuccess = true;
} catch (SftpException e) {
logger.error("目錄創(chuàng)建異常!", e);
}
return isSuccess;
}
/**
* 判斷目錄是否存在
* @param directory 路徑
* @return
*/
public boolean isDirExist(String directory) {
boolean isSuccess = false;
try {
SftpATTRS sftpATTRS = channel.lstat(directory);
isSuccess = true;
return sftpATTRS.isDir();
} catch (Exception e) {
if (e.getMessage().toLowerCase().equals("no such file")) {
isSuccess = false;
}
}
return isSuccess;
}
/**
* 重命名指定文件或目錄
*
*/
public boolean rename(String oldPath, String newPath) {
boolean isSuccess = false;
try {
channel.rename(oldPath, newPath);
isSuccess = true;
} catch (SftpException e) {
logger.error("重命名指定文件或目錄異常", e);
}
return isSuccess;
}
/**
* 列出指定目錄下的所有文件和子目錄。
*/
public Vector ls(String path) {
try {
Vector vector = channel.ls(path);
return vector;
} catch (SftpException e) {
logger.error("列出指定目錄下的所有文件和子目錄。", e);
}
return null;
}
/**
* 刪除文件
*
* @param directory linux服務(wù)器文件地址
* @param deleteFile 文件名稱
*/
public boolean deleteFile(String directory, String deleteFile) {
boolean isSuccess = false;
try {
channel.cd(directory);
channel.rm(deleteFile);
isSuccess = true;
} catch (SftpException e) {
logger.error("刪除文件失敗", e);
}
return isSuccess;
}
/**
* 下載文件
*
* @param directory 下載目錄
* @param downloadFile 下載的文件
* @param saveFile 下載到本地路徑
*/
public boolean download(String directory, String downloadFile, String saveFile) {
boolean isSuccess = false;
try {
channel.cd(directory);
File file = new File(saveFile);
channel.get(downloadFile, new FileOutputStream(file));
isSuccess = true;
} catch (SftpException e) {
logger.error("下載文件失敗", e);
} catch (FileNotFoundException e) {
logger.error("下載文件失敗", e);
}
return isSuccess;
}
}問題
文件超出默認(rèn)大小
#單文件上傳最大大小,默認(rèn)1Mb
spring.http.multipart.maxFileSize=100Mb
#多文件上傳時最大大小,默認(rèn)10Mb
spring.http.multipart.maxRequestSize=500MB
到此這篇關(guān)于Java連接服務(wù)器的兩種方式SFTP和FTP的文章就介紹到這了,更多相關(guān)Java SFTP和FTP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用Spring將Service注入到Servlet中的流程步驟
在Java Web開發(fā)中,??Servlet??是一個非常重要的組件,它用于處理客戶端的請求并生成響應(yīng),而?Spring??框架則是一個廣泛使用的依賴注入框架,可以幫助開發(fā)者管理應(yīng)用中的對象及其依賴關(guān)系,本文將介紹如何使用Spring框架將Service層的對象注入到Servlet中2025-01-01
使用Java實現(xiàn)在Excel中添加動態(tài)數(shù)組公式
動態(tài)數(shù)組公式是?Excel?引入的一項重要功能,它允許用戶從單個單元格中的公式返回多個結(jié)果值,并將這些值自動填充到與公式單元格相鄰的單元格中,本文主要介紹了如何使用Java實現(xiàn)在Excel中添加動態(tài)數(shù)組公式,x需要的可以參考下2023-12-12
深入理解SpringBoot中關(guān)于Mybatis使用方法
這篇文章主要介紹了SpringBoot中關(guān)于Mybatis使用方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-03-03
Java編程中快速排序算法的實現(xiàn)及相關(guān)算法優(yōu)化
這篇文章主要介紹了Java編程中快速排序算法的實現(xiàn)及相關(guān)算法優(yōu)化,快速排序算法的最差時間復(fù)雜度為(n^2),最優(yōu)時間復(fù)雜度為(n\log n),存在優(yōu)化的空間,需要的朋友可以參考下2016-05-05
OpenFeign無法遠(yuǎn)程調(diào)用問題及解決
文章介紹了在使用Feign客戶端時遇到的讀超時問題,并分析了原因是系統(tǒng)啟動時未先加載Nacos配置,為了解決這個問題,建議將Nacos配置放在`bootstrap.yml`文件中,以便項目啟動時優(yōu)先加載Nacos配置2024-11-11
Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決
這篇文章主要介紹了Spring Boot多模塊化后,服務(wù)間調(diào)用的坑及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

