JSch教程使用sftp協(xié)議實現(xiàn)服務(wù)器文件載操作
Jsch是什么?
JSch 是SSH2的一個純Java實現(xiàn)。它允許你連接到一個sshd 服務(wù)器,使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)鹊?。你可以將它的功能集成到你自己?程序中。同時該項目也提供一個J2ME版本用來在手機上直連SSHD服務(wù)器
Jsch功能很強大,博主這里主要用來做文件操作
怎么使用?
添加jar依賴
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.53</version> </dependency>
我把我的SftpUtil貼下面了,注釋還算清楚
/**
* Content :sftp協(xié)議文件上傳下載
* Created by kl on 2016/5/6.
*/
public class SftpUtil {
/**
* 上傳文件到指定服務(wù)器
* @param ip 連接ip
* @param user 用戶名
* @param psw 密碼
* @param port 端口 <=0 為默認端口
* @param fielPath 上傳的服務(wù)器路徑
* @param serverFileName 服務(wù)器保存的文件名
* @param instream 要上傳的文件流
* @throws Exception
*/
public static void sftpFileUpload(String ip,int port, String user, String psw, String fielPath,String serverFileName,InputStream instream) throws Exception {
Session session =getSession( ip, user, psw, port);
Channel channel = null;
try {
//創(chuàng)建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//進入服務(wù)器指定的文件夾
sftp.cd(fielPath);
OutputStream outstream = sftp.put(serverFileName);
byte b[] = new byte[1024*200];//每次傳輸200k
int n;
while ((n = instream.read(b)) != -1) {
outstream.write(b, 0, n);
}
outstream.flush();
outstream.close();
instream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
}
/**
* 從指定服務(wù)器下載文件到本地
* @param ip 連接ip
* @param user 用戶名
* @param psw 密碼
* @param port 端口 <=0 為默認端口
* @param fielPath 服務(wù)器文件路徑
* @param serverFileName 要下載的文件名
* @param outputStream 輸出到本地的文件流
* @throws Exception
*/
public static void sftpFileDownload(String ip,int port, String user, String psw, String fielPath,String serverFileName,OutputStream outputStream) throws Exception {
Session session =getSession( ip, user, psw, port);
Channel channel = null;
try {
//創(chuàng)建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//進入服務(wù)器指定的文件夾
sftp.cd(fielPath);
sftp.get(serverFileName,outputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
}
/**
* 刪除服務(wù)器指定文件
* @param ip 連接ip
* @param user 用戶名
* @param psw 密碼
* @param port 端口 <=0 為默認端口
* @param fielPath 服務(wù)器文件路徑
* @param serverFileName 要刪除的文件名
* @throws Exception
*/
public static void sftpFileDelete(String ip,int port, String user, String psw, String fielPath,String serverFileName) throws Exception {
Session session =getSession( ip, user, psw, port);
Channel channel = null;
try {
//創(chuàng)建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//進入服務(wù)器指定的文件夾
sftp.cd(fielPath);
sftp.rm(serverFileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
}
/**
* 查看指定目錄所有文件
* @param ip
* @param user
* @param psw
* @param port
* @param fielPath
* @throws Exception
*/
public static Vector seeServerFileList(String ip, int port,String user, String psw, String fielPath)throws Exception{
Session session =getSession( ip, user, psw, port);
Channel channel = null;
Vector v=null;
try {
//創(chuàng)建sftp通信通道
channel = (Channel) session.openChannel("sftp");
channel.connect(1000);
ChannelSftp sftp = (ChannelSftp) channel;
//進入服務(wù)器指定的文件夾
sftp.cd(fielPath);
//列出服務(wù)器指定的文件列表
v = sftp.ls(fielPath);
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
if (channel!=null)channel.disconnect();
}
return v;
}
/**
* 連接服務(wù)器
* @param ip 服務(wù)器地址
* @param user 用戶名
* @param psw 密碼
* @param port 連接端口
* @return
* @throws Exception
*/
public static Session getSession(String ip, String user, String psw, int port)throws Exception{
Session session = null;
JSch jsch = new JSch();
if (port <= 0) {
//連接服務(wù)器,采用默認端口
session = jsch.getSession(user, ip);
} else {
session = jsch.getSession(user, ip, port);
}
//如果服務(wù)器連接不上,則拋出異常
if (session == null) {
throw new Exception("sftp session is null");
}
session.setPassword(psw);//設(shè)置密碼
//設(shè)置登陸超時時間
session.connect(30000);//30s
return session;
}
}以上就是JSch教程使用sftp協(xié)議實現(xiàn)服務(wù)器文件上傳下載操作的詳細內(nèi)容,更多關(guān)于JSch sftp協(xié)議服務(wù)器文件上傳下載的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java中hasNextInt判斷后無限循環(huán)輸出else項的解決方法
這篇文章主要介紹了java中hasNextInt判斷后無限循環(huán)輸出else項的解決方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
教你如何把Eclipse創(chuàng)建的Web項目(非Maven)導(dǎo)入Idea
這篇文章主要介紹了教你如何把Eclipse創(chuàng)建的Web項目(非Maven)導(dǎo)入Idea,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Spring Boot實戰(zhàn)之靜態(tài)資源處理
這篇文章主要介紹了Spring Boot實戰(zhàn)之靜態(tài)資源處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
Java基礎(chǔ)強化訓(xùn)練輸入錯誤即結(jié)束進程
本文主要介紹了Java編程的基礎(chǔ)知識強化應(yīng)用,文中實例涉及到了許多基礎(chǔ)知識,new對象,控制臺輸入,if語句等。很實用,需要的朋友可以參考下2017-09-09
Java淺析代碼塊與構(gòu)造塊及靜態(tài)塊三者之間的關(guān)系
所謂代碼塊是指用"{}"括起來的一段代碼,根據(jù)其位置和聲明的不同,可以分為普通代碼塊、構(gòu)造塊、靜態(tài)塊、和同步代碼塊。如果在代碼塊前加上synchronized關(guān)鍵字,則此代碼塊就成為同步代碼塊2022-07-07

