版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第關(guān)于Springboot中JSCH的使用及說明目錄1.JSCH簡介2.JSCH依賴3.使用方法3.1連接遠(yuǎn)程主機(jī)3.2ChannelExec使用說明3.3ChannelSftp使用說明3.4ChannelShell使用說明3.5完整工具類代碼4.使用連接池
1.JSCH簡介
JSch是SSH2的一個(gè)純Java實(shí)現(xiàn)。它允許你連接到一個(gè)sshd服務(wù)器,使用端口轉(zhuǎn)發(fā),X11轉(zhuǎn)發(fā),文件傳輸?shù)鹊取?/p>
你可以將它的功能集成到你自己的程序中。同時(shí)該項(xiàng)目也提供一個(gè)J2ME版本用來在手機(jī)上直連SSHD服務(wù)器。
2.JSCH依賴
dependency
groupIdcom.jcraft/groupId
artifactIdjsch/artifactId
version0.1.55/version
/dependency
3.使用方法
3.1連接遠(yuǎn)程主機(jī)
/**
*初始化
*@paramip遠(yuǎn)程主機(jī)IP地址
*@paramport遠(yuǎn)程主機(jī)端口
*@paramusername遠(yuǎn)程主機(jī)登陸用戶名
*@parampassword遠(yuǎn)程主機(jī)登陸密碼
*@throwsJSchExceptionJSch異常
publicvoidinit(Stringip,Integerport,Stringusername,Stringpassword)throwsJSchException{
JSchjsch=newJSch();
session=jsch.getSession(username,ip,port);
session.setPassword(password);
PropertiessshConfig=newProperties();
sshConfig.put("StrictHostKeyChecking",strictHostKeyChecking);
session.setConfig(sshConfig);
session.connect(timeout);
("Sessionconnected!");
publicvoidinit(Stringip,Stringusername,Stringpassword)throwsJSchException{
init(ip,22,username,password);
3.2ChannelExec使用說明
/**
*連接多次執(zhí)行命令,執(zhí)行命令完畢后需要執(zhí)行close()方法
*@paramcommand需要執(zhí)行的指令
*@return執(zhí)行結(jié)果
*@throwsException沒有執(zhí)行初始化
publicStringexecCmd(Stringcommand)throwsException{
//打開執(zhí)行shell指令的通道
channel=session.openChannel("exec");
channelExec=(ChannelExec)channel;
if(session==null||channel==null||channelExec==null){
log.error("請先執(zhí)行init()");
thrownewException("請先執(zhí)行init()");
("execCmdcommand-{}",command);
channelExec.setCommand(command);
channel.setInputStream(null);
channelExec.setErrStream(System.err);
channel.connect();
StringBuildersb=newStringBuilder(16);
try(InputStreamin=channelExec.getInputStream();
InputStreamReaderisr=newInputStreamReader(in,StandardCharsets.UTF_8);
BufferedReaderreader=newBufferedReader(isr)){
Stringbuffer;
while((buffer=reader.readLine())!=null){
sb.append("\n").append(buffer);
("execCmdresult-{}",sb);
returnsb.toString();
*執(zhí)行命令關(guān)閉連接
*@paramcommand需要執(zhí)行的指令
*@return執(zhí)行結(jié)果
*@throwsException沒有執(zhí)行初始化
publicStringexecCmdAndClose(Stringcommand)throwsException{
Stringresult=execCmd(command);
close();
returnresult;
*釋放資源
publicvoidclose(){
if(channelExec!=nullchannelExec.isConnected()){
channelExec.disconnect();
if(channel!=nullchannel.isConnected()){
channel.disconnect();
if(session!=nullsession.isConnected()){
session.disconnect();
3.3ChannelSftp使用說明
3.3.1ChannelSftp簡介
ChannelSftp類是JSch實(shí)現(xiàn)SFTP核心類,它包含了所有SFTP的方法,如:
put():文件上傳get():文件下載cd():進(jìn)入指定目錄ls():得到指定目錄下的文件列表rename():重命名指定文件或目錄rm():刪除指定文件mkdir():創(chuàng)建目錄rmdir():刪除目錄
3.3.2JSch支持三種文件傳輸模式:
模式描述OVERWRITE完全覆蓋模式,這是JSch的默認(rèn)文件傳輸模式,即如果目標(biāo)文件已經(jīng)存在,傳輸?shù)奈募⑼耆采w目標(biāo)文件,產(chǎn)生新的文件。RESUME恢復(fù)模式,如果文件已經(jīng)傳輸一部分,這時(shí)由于網(wǎng)絡(luò)或其他任何原因?qū)е挛募鬏斨袛?,如果下一次傳輸相同的文件,則會(huì)從上一次中斷的地方續(xù)傳。APPEND追加模式,如果目標(biāo)文件已存在,傳輸?shù)奈募⒃谀繕?biāo)文件后追加。
3.3.3文件上傳
實(shí)現(xiàn)文件上傳可以調(diào)用ChannelSftp對象的put方法。ChannelSftp中有12個(gè)put方法的重載方法:
方法描述publicvoidput(Stringsrc,Stringdst)將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。采用默認(rèn)的傳輸模式:OVERWRITEpublicvoidput(Stringsrc,Stringdst,intmode)將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。指定文件傳輸模式為mode(mode可選值為:ChannelSftp.OVERWRITE,ChannelSftp.RESUME,ChannelSftp.APPEND)publicvoidput(Stringsrc,Stringdst,SftpProgressMonitormonitor)將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。采用默認(rèn)的傳輸模式:OVERWRITE,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件傳輸?shù)倪M(jìn)度。publicvoidput(Stringsrc,Stringdst,SftpProgressMonitormonitor,intmode)將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。指定傳輸模式為mode,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件傳輸?shù)倪M(jìn)度。publicvoidput(InputStreamsrc,Stringdst)將本地的inputstream對象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。采用默認(rèn)的傳輸模式:OVERWRITEpublicvoidput(InputStreamsrc,Stringdst,intmode)將本地的inputstream對象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。指定文件傳輸模式為modepublicvoidput(InputStreamsrc,Stringdst,SftpProgressMonitormonitor)將本地的inputstream對象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。采用默認(rèn)的傳輸模式:OVERWRITE,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控傳輸?shù)倪M(jìn)度。publicvoidput(InputStreamsrc,Stringdst,SftpProgressMonitormonitor,intmode)將本地的inputstream對象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。指定文件傳輸模式為mode,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控傳輸?shù)倪M(jìn)度。publicOutputStreamput(Stringdst)該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。采用默認(rèn)的傳輸模式:OVERWRITEpublicOutputStreamput(Stringdst,finalintmode)該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。指定文件傳輸模式為modepublicOutputStreamput(Stringdst,finalSftpProgressMonitormonitor,finalintmode)該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。指定文件傳輸模式為mode,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控傳輸?shù)倪M(jìn)度。publicOutputStreamput(Stringdst,finalSftpProgressMonitormonitor,finalintmode,longoffset)該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。指定文件傳輸模式為mode,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控傳輸?shù)倪M(jìn)度。offset指定了一個(gè)偏移量,從輸出流偏移offset開始寫入數(shù)據(jù)。
*SFTP文件上傳
*@paramsrc源地址
*@paramdst目的地址
*@throwsException上傳文件失敗
publicvoidputAndClose(Stringsrc,Stringdst)throwsException{
putAndClose(src,dst,ChannelSftp.OVERWRITE);
*SFTP文件上傳
*@paramsrc源地址
*@paramdst目的地址
*@parammode上傳模式默認(rèn)為ChannelSftp.OVERWRITE
*@throwsException上傳文件失敗
publicvoidputAndClose(Stringsrc,Stringdst,intmode)throwsException{
initChannelSftp();
("UploadFile{}-{}",src,dst);
channelSftp.put(src,dst,mode);
("UploadFileSuccess!");
close();
*SFTP文件上傳并監(jiān)控上傳進(jìn)度
*@paramsrc源地址
*@paramdst目的地址
*@throwsException上傳文件失敗
publicvoidputMonitorAndClose(Stringsrc,Stringdst)throwsException{
putMonitorAndClose(src,dst,ChannelSftp.OVERWRITE);
*SFTP文件上傳并監(jiān)控上傳進(jìn)度
*@paramsrc源地址
*@paramdst目的地址
*@parammode上傳模式默認(rèn)為ChannelSftp.OVERWRITE
*@throwsException上傳文件失敗
publicvoidputMonitorAndClose(Stringsrc,Stringdst,intmode)throwsException{
initChannelSftp();
UploadMonitormonitor=newUploadMonitor(newFile(src).length());
("UploadFile{}-{}",src,dst);
channelSftp.put(src,dst,monitor,mode);
("UploadFileSuccess!");
close();
*釋放資源
publicvoidclose(){
if(channelSftp!=nullchannelSftp.isConnected()){
channelSftp.disconnect();
if(channel!=nullchannel.isConnected()){
channel.disconnect();
if(session!=nullsession.isConnected()){
session.disconnect();
privatevoidinitChannelSftp()throwsException{
channel=session.openChannel("sftp");
channel.connect();//建立SFTP通道的連接
channelSftp=(ChannelSftp)channel;
if(session==null||channel==null||channelSftp==null){
log.error("請先執(zhí)行init()");
thrownewException("請先執(zhí)行init()");
3.3.4文件下載
JSch文件下載是通過調(diào)用ChannelSftp對象的get方法來實(shí)現(xiàn)的。ChannelSftp中有9個(gè)get方法的重載方法:
方法描述publicvoidget(Stringsrc,Stringdst)將目標(biāo)服務(wù)器上文件名為src的文件下載到本地,本地文件名為dst。若dst為目錄,則下載到本地的文件名將與src文件名相同。(注:src必須是文件,不能為目錄),采用默認(rèn)的傳輸模式:OVERWRITEpublicvoidget(Stringsrc,Stringdst,SftpProgressMonitormonitor)將目標(biāo)服務(wù)器上文件名為src的文件下載到本地,本地文件名為dst。若dst為目錄,則下載到本地的文件名將與src文件名相同。(注:src必須是文件,不能為目錄),采用默認(rèn)的傳輸模式:OVERWRITEpublicvoidget(Stringsrc,Stringdst,SftpProgressMonitormonitor,intmode)將目標(biāo)服務(wù)器上文件名為src的文件下載到本地,本地文件名為dst。若dst為目錄,則下載到本地的文件名將與src文件名相同。(注:src必須是文件,不能為目錄)指定文件傳輸模式為mode(mode可選值為:ChannelSftp.OVERWRITE,ChannelSftp.RESUME,ChannelSftp.APPEND),并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件的傳輸進(jìn)度。publicvoidget(Stringsrc,OutputStreamdst)將目標(biāo)服務(wù)器上文件名為src的文件下載到本地,下載的數(shù)據(jù)寫入到輸出流對象dst(如:文件輸出流)。采用默認(rèn)的傳輸模式:OVERWRITEpublicvoidget(Stringsrc,OutputStreamdst,SftpProgressMonitormonitor)將目標(biāo)服務(wù)器上文件名為src的文件下載到本地,下載的數(shù)據(jù)寫入到輸出流對象dst(如:文件輸出流)。采用默認(rèn)的傳輸模式:OVERWRITE,并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件的傳輸進(jìn)度。publicvoidget(Stringsrc,OutputStreamdst,SftpProgressMonitormonitor,intmode,longskip)將目標(biāo)服務(wù)器上文件名為src的文件下載到本地,下載的數(shù)據(jù)寫入到輸出流對象dst(如:文件輸出流)。指定文件傳輸模式為mode并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件的傳輸進(jìn)度。skip指定了一個(gè)跳讀量,即下載時(shí)從src文件跳過skip字節(jié)的數(shù)據(jù)。(一般不推薦使用該參數(shù),默認(rèn)設(shè)為0)publicInputStreamget(Stringsrc)該方法返回一個(gè)輸入流,該輸入流含有目標(biāo)服務(wù)器上文件名為src的文件數(shù)據(jù)??梢詮脑撦斎肓髦凶x取數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)奖镜兀ㄈ纾鹤x取數(shù)據(jù)后將數(shù)據(jù)寫入到本地的文件中)(注:該方法不支持多種文件傳輸模式,如何讀取與保存數(shù)據(jù)由應(yīng)用程序自己確定)publicInputStreamget(Stringsrc,SftpProgressMonitormonitor)該方法返回一個(gè)輸入流,該輸入流含有目標(biāo)服務(wù)器上文件名為src的文件數(shù)據(jù)。可以從該輸入流中讀取數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)奖镜兀ㄈ纾鹤x取數(shù)據(jù)后將數(shù)據(jù)寫入到本地的文件中)并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件的傳輸進(jìn)度。(注:該方法不支持多種文件傳輸模式,如何讀取與保存數(shù)據(jù)由應(yīng)用程序自己確定)publicInputStreamget(Stringsrc,finalSftpProgressMonitormonitor,finallongskip)該方法返回一個(gè)輸入流,該輸入流含有目標(biāo)服務(wù)器上文件名為src的文件數(shù)據(jù)??梢詮脑撦斎肓髦凶x取數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)奖镜兀ㄈ纾鹤x取數(shù)據(jù)后將數(shù)據(jù)寫入到本地的文件中)并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對象來監(jiān)控文件的傳輸進(jìn)度。(注:該方法不支持多種文件傳輸模式,如何讀取與保存數(shù)據(jù)由應(yīng)用程序自己確定)skip指定了一個(gè)跳讀量,即下載時(shí)從src文件跳過skip字節(jié)的數(shù)據(jù)。(一般不推薦使用該參數(shù),默認(rèn)設(shè)為0)
/**
*SFTP文件下載
*@paramsrc源文件地址
*@paramdst目的地址
*@throwsException下載文件失敗
publicvoidgetAndClose(Stringsrc,Stringdst)throwsException{
initChannelSftp();
("DownloadFile{}-{}",src,dst);
channelSftp.get(src,dst);
("DownloadFileSuccess!");
close();
publicvoidgetMonitorAndClose(Stringsrc,Stringdst)throwsException{
initChannelSftp();
FileProgressMonitormonitor=newFileProgressMonitor(newFile(src).length());
("DownloadFile{}-{}",src,dst);
channelSftp.get(src,dst,monitor);
("DownloadFileSuccess!");
close();
3.4ChannelShell使用說明
3.4.1shell代碼
/**
*執(zhí)行復(fù)雜shell命令
*@paramcmds多條命令
*@return執(zhí)行結(jié)果
*@throwsException連接異常
publicStringexecCmdByShell(String...cmds)throwsException{
returnexecCmdByShell(Arrays.asList(cmds));
*執(zhí)行復(fù)雜shell命令
*@paramcmds多條命令
*@return執(zhí)行結(jié)果
*@throwsException連接異常
publicStringexecCmdByShell(ListStringcmds)throwsException{
Stringresult="";
initChannelShell();
InputStreaminputStream=channelShell.getInputStream();
channelShell.setPty(true);
channelShell.connect();
OutputStreamoutputStream=channelShell.getOutputStream();
PrintWriterprintWriter=newPrintWriter(outputStream);
for(Stringcmd:cmds){
printWriter.println(cmd);
printWriter.flush();
byte[]tmp=newbyte[1024];
while(true){
while(inputStream.available()0){
inti=inputStream.read(tmp,0,1024);
if(i0){
break;
Strings=newString(tmp,0,i);
if(s.contains("--More--")){
outputStream.write(("").getBytes());
outputStream.flush();
System.out.println(s);
if(channelShell.isClosed()){
System.out.println("exit-status:"+channelShell.getExitStatus());
break;
try{
Thread.sleep(1000);
}catch(Exceptione){
e.printStackTrace();
outputStream.close();
inputStream.close();
returnresult;
privatevoidinitChannelShell()throwsException{
//打開執(zhí)行shell指令的通道
channel=session.openChannel("shell");
channelShell=(ChannelShell)channel;
if(session==null||channel==null||channelShell==null){
log.error("請先執(zhí)行init()");
thrownewException("請先執(zhí)行init()");
3.5完整工具類代碼
ShellUtil.java
@Slf4j
@Component
@Slf4j
@Component
@Scope(value="prototype")
publicclassShellUtil{
@Value("${ssh.strictHostKeyChecking:no}")
privateStringstrictHostKeyChecking;
@Value("${ssh.timeout:30000}")
privateIntegertimeout;
privateSessionsession;
privateChannelchannel;
privateChannelExecchannelExec;
privateChannelSftpchannelSftp;
privateChannelShellchannelShell;
*初始化
*@paramip遠(yuǎn)程主機(jī)IP地址
*@paramport遠(yuǎn)程主機(jī)端口
*@paramusername遠(yuǎn)程主機(jī)登陸用戶名
*@parampassword遠(yuǎn)程主機(jī)登陸密碼
*@throwsJSchExceptionJSch異常
publicvoidinit(Stringip,Integerport,Stringusername,Stringpassword)throwsJSchException{
JSchjsch=newJSch();
session=jsch.getSession(username,ip,port);
session.setPassword(password);
PropertiessshConfig=newProperties();
sshConfig.put("StrictHostKeyChecking",strictHostKeyChecking);
session.setConfig(sshConfig);
session.connect(timeout);
("Sessionconnected!");
publicvoidinit(Stringip,Stringusername,Stringpassword)throwsJSchException{
init(ip,22,username,password);
*連接多次執(zhí)行命令,執(zhí)行命令完畢后需要執(zhí)行close()方法
*@paramcommand需要執(zhí)行的指令
*@return執(zhí)行結(jié)果
*@throwsException沒有執(zhí)行初始化
publicStringexecCmd(Stringcommand)throwsException{
initChannelExec();
("execCmdcommand-{}",command);
channelExec.setCommand(command);
channel.setInputStream(null);
channelExec.setErrStream(System.err);
channel.connect();
StringBuildersb=newStringBuilder(16);
try(InputStreamin=channelExec.getInputStream();
InputStreamReaderisr=newInputStreamReader(in,StandardCharsets.UTF_8);
BufferedReaderreader=newBufferedReader(isr)){
Stringbuffer;
while((buffer=reader.readLine())!=null){
sb.append("\n").append(buffer);
("execCmdresult-{}",sb);
returnsb.toString();
*執(zhí)行命令關(guān)閉連接
*@paramcommand需要執(zhí)行的指令
*@return執(zhí)行結(jié)果
*@throwsException沒有執(zhí)行初始化
publicStringexecCmdAndClose(Stringcommand)throwsException{
Stringresult=execCmd(command);
close();
returnresult;
*執(zhí)行復(fù)雜shell命令
*@paramcmds多條命令
*@return執(zhí)行結(jié)果
*@throwsException連接異常
publicStringexecCmdByShell(String...cmds)throwsException{
returnexecCmdByShell(Arrays.asList(cmds));
*執(zhí)行復(fù)雜shell命令
*@paramcmds多條命令
*@return執(zhí)行結(jié)果
*@throwsException連接異常
publicStringexecCmdByShell(ListStringcmds)throwsException{
Stringresult="";
initChannelShell();
InputStreaminputStream=channelShell.getInputStream();
channelShell.setPty(true);
channelShell.connect();
OutputStreamoutputStream=channelShell.getOutputStream();
PrintWriterprintWriter=newPrintWriter(outputStream);
for(Stringcmd:cmds){
printWriter.println(cmd);
printWriter.flush();
byte[]tmp=newbyte[1024];
while(true){
while(inputStream.available()0){
inti=inputStream.read(tmp,0,1024);
if(i0){
break;
Strings=newString(tmp,0,i);
if(s.contains("--More--")){
outputStream.write(("").getBytes());
outputStream.flush();
System.out.println(s);
if(channelShell.isClosed()){
System.out.println("exit-status:"+channelShell.getExitStatus());
break;
try{
Thread.sleep(1000);
}catch(Exceptione){
e.printStackTrace();
outputStream.close();
inputStream.close();
returnresult;
*SFTP文件上傳
*@paramsrc源地址
*@paramdst目的地址
*@throwsException上傳文件失敗
publicvoidputAndClose(Stringsrc,Stringdst)throwsException{
putAndClose(src,dst,ChannelSftp.OVERWRITE);
*SFTP文件上傳
*@paramsrc源地址
*@paramdst目的地址
*@parammode上傳模式默認(rèn)為ChannelSftp.OVERWRITE
*@throwsException上傳文件失敗
publicvoidputAndClose(Stringsrc,Stringdst,intmode)throwsException{
put(src,dst,mode);
close();
publicvoidput(Stringsrc,Stringdst)throwsException{
put(src,dst,ChannelSftp.OVERWRITE);
publicvoidput(Stringsrc,Stringdst,intmode)throwsException{
initChannelSftp();
("UploadFile{}-{}",src,dst);
channelSftp.put(src,dst,mode);
("UploadFileSuccess!");
*SFTP文件上傳并監(jiān)控上傳進(jìn)度
*@paramsrc源地址
*@paramdst目的地址
*@throwsException上傳文件失敗
publicvoidputMonitorAndClose(Stringsrc,Stringdst)throwsException{
putMonitorAndClose(src,dst,ChannelSftp.OVERWRITE);
*SFTP文件上傳并監(jiān)控上傳進(jìn)度
*@paramsrc源地址
*@paramdst目的地址
*@parammode上傳模式默認(rèn)為ChannelSftp.OVERWRITE
*@throwsException上傳文件失敗
publicvoidputMonitorAndClose(Stringsrc,Stringdst,intmode)throwsException{
initChannelSftp();
FileProgressMonitormonitor=newFileProgressMonitor(newFile(src).length());
("UploadFile{}-{}",src,dst);
channelSftp.put(src,dst,monitor,mode);
("UploadFileSuccess!");
close();
*SFTP文件下載
*@paramsrc源文件地址
*@paramdst目的地址
*@throwsException下載文件失敗
publicvoidgetAndClose(Stringsrc,Stringdst)throwsException{
get(src,dst);
close();
publicvoidget(Stringsrc,Stringdst)throwsException{
initChannelSftp();
("DownloadFile{}-{}",src,dst);
channelSftp.get(src,dst);
("DownloadFileSuccess!");
*SFTP文件下載并監(jiān)控下載進(jìn)度
*@paramsrc源文件地址
*@paramdst目的地址
*@throwsException下載文件失敗
publicvoidgetMonitorAndClose(Stringsrc,Stringdst)throwsException{
initChannelSftp();
FileProgressMonitormonitor=newFileProgressMonitor(newFile(src).length());
("DownloadFile{}-{}",src,dst);
channelSftp.get(src,dst,monitor);
("DownloadFileSuccess!");
close();
*刪除指定目錄文件
*@parampath刪除路徑
*@throwsException遠(yuǎn)程主機(jī)連接異常
publicvoiddeleteFile(Stringpath)throwsException{
initChannelSftp();
channelSftp.rm(path);
("DeleteFile{}",path);
*刪除指定目錄
*@parampath刪除路徑
*@throwsException遠(yuǎn)程主機(jī)連接異常
publicvoiddeleteDir(Stringpath)throwsException{
initChannelSftp();
channelSftp.rmdir(path);
("DeleteDir{}",path);
*釋放資源
publicvoidclose(){
if(channelSftp!=nullchannelSftp.isConnected()){
channelSftp.disconnect();
if(channelExec!=nullchannelExec.isConnected()){
channelExec.disconnect();
if(channel!=nullchannel.isConnected()){
channel.disconnect();
if(session!=nullsession.isConnected()){
session.disconnect();
privatevoidinitChannelSftp()throwsException{
channel=session.openChannel("sftp");
channel.connect();//建立SFTP通道的連接
channelSftp=(ChannelSftp)channel;
if(session==null||channel==null||channelSftp==null){
log.error("請先執(zhí)行init()");
thrownewException("請先執(zhí)行init()");
privatevoidinitChannelExec()throwsException{
//打開執(zhí)行shell指令的通道
channel=session.openChannel("exec");
channelExec=(ChannelExec)channel;
if(session==null||channel==null||channelExec==null){
log.error("請先執(zhí)行init()");
thrownewException("請先執(zhí)行init()");
privatevoidinitChannelShell()throwsException{
//打開執(zhí)行shell指令的通道
channel=session.openChannel("shell");
channelShell=(ChannelShell)channel;
if(session==null||channel==null||channelShell==null){
log.error("請先執(zhí)行init()");
thrownewException("請先執(zhí)行init()");
FileProgressMonitor.java
@Slf4j
publicclassFileProgressMonitorextendsTimerTaskimplementsSftpProgressMonitor{
privatebooleanisEnd=false;
privatelongtransfered;
privatelongfileSize;
privateScheduledExecutorServiceexecutorService;
privatebooleanisScheduled=false;
longstartTime=0L;
publicFileProgressMonitor(longfileSize){
this.fileSize=fileSize;
@Override
publicvoidrun(){
if(!isEnd()){
("Transferingisinprogress.");
longtransfered=getTransfere
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年新疆第二醫(yī)學(xué)院馬克思主義基本原理概論期末考試模擬題含答案解析(奪冠)
- 2025年內(nèi)蒙古科技職業(yè)學(xué)院單招職業(yè)技能測試題庫附答案解析
- 2024年淮南師范學(xué)院馬克思主義基本原理概論期末考試題及答案解析(必刷)
- 2025年湖北省孝感市單招職業(yè)傾向性測試題庫帶答案解析
- 2025年貴州民用航空職業(yè)學(xué)院單招職業(yè)適應(yīng)性測試題庫帶答案解析
- 2025年中央美術(shù)學(xué)院馬克思主義基本原理概論期末考試模擬題帶答案解析
- 2025年樂山職業(yè)技術(shù)學(xué)院單招職業(yè)適應(yīng)性考試題庫帶答案解析
- 2025年惠東縣招教考試備考題庫含答案解析(奪冠)
- 2025年北京電子科技學(xué)院馬克思主義基本原理概論期末考試模擬題及答案解析(奪冠)
- 2026年哈爾濱北方航空職業(yè)技術(shù)學(xué)院單招職業(yè)傾向性考試題庫附答案解析
- 2025年農(nóng)村人居環(huán)境五年評估報(bào)告
- 浙江省杭州市拱墅區(qū)2024-2025學(xué)年四年級上冊期末考試數(shù)學(xué)試卷(含答案)
- 房屋過戶給子女的協(xié)議書的范文
- 超聲振動(dòng)珩磨裝置的總體設(shè)計(jì)
- 新媒體藝術(shù)的發(fā)展歷程及藝術(shù)特征
- 醫(yī)保違規(guī)行為分類培訓(xùn)課件
- 講課學(xué)生數(shù)學(xué)學(xué)習(xí)成就
- 醫(yī)療器械法規(guī)對互聯(lián)網(wǎng)銷售的限制
- 西葫蘆栽培技術(shù)要點(diǎn)
- 系桿拱橋系桿預(yù)應(yīng)力施工控制要點(diǎn)
- 三亞市海棠灣椰子洲島土地價(jià)格咨詢報(bào)告樣本及三洲工程造價(jià)咨詢有限公司管理制度
評論
0/150
提交評論