javascript
channelsftp的put_JSch - Java实现的SFTP(文件上传详解篇)
public void put(String src, String dst)
將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。
采用默認(rèn)的傳輸模式:OVERWRITE
public void put(String src, String dst, int mode)
將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。
指定文件傳輸模式為mode(mode可選值為:ChannelSftp.OVERWRITE,ChannelSftp.RESUME,
ChannelSftp.APPEND)
public void put(String src, String dst, SftpProgressMonitor monitor)
將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。
采用默認(rèn)的傳輸模式:OVERWRITE
并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對(duì)象來監(jiān)控文件傳輸?shù)倪M(jìn)度。
public void put(String src, String dst,
SftpProgressMonitor monitor, int mode)
將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,若dst為目錄,則目標(biāo)文件名將與src文件名相同。
指定傳輸模式為mode
并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對(duì)象來監(jiān)控文件傳輸?shù)倪M(jìn)度。
public void put(InputStream src, String dst)
將本地的input stream對(duì)象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
采用默認(rèn)的傳輸模式:OVERWRITE
public void put(InputStream src, String dst, int mode)
將本地的input stream對(duì)象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
指定文件傳輸模式為mode
public void put(InputStream src, String dst, SftpProgressMonitor monitor)
將本地的input stream對(duì)象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
采用默認(rèn)的傳輸模式:OVERWRITE
并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對(duì)象來監(jiān)控傳輸?shù)倪M(jìn)度。
public void put(InputStream src, String dst,
SftpProgressMonitor monitor, int mode)
將本地的input stream對(duì)象src上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
指定文件傳輸模式為mode
并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對(duì)象來監(jiān)控傳輸?shù)倪M(jìn)度。
public OutputStream put(String dst)
該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
采用默認(rèn)的傳輸模式:OVERWRITE
public OutputStream put(String dst, final int mode)
該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
指定文件傳輸模式為mode
public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode)
該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
指定文件傳輸模式為mode
并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對(duì)象來監(jiān)控傳輸?shù)倪M(jìn)度。
public OutputStream put(String dst, final SftpProgressMonitor monitor, final int mode, long offset)
該方法返回一個(gè)輸出流,可以向該輸出流中寫入數(shù)據(jù),最終將數(shù)據(jù)傳輸?shù)侥繕?biāo)服務(wù)器,目標(biāo)文件名為dst,dst不能為目錄。
指定文件傳輸模式為mode
并使用實(shí)現(xiàn)了SftpProgressMonitor接口的monitor對(duì)象來監(jiān)控傳輸?shù)倪M(jìn)度。
offset指定了一個(gè)偏移量,從輸出流偏移offset開始寫入數(shù)據(jù)。
應(yīng)用實(shí)例:
SFTPTest.java
SFTPTest.java
packagecom.longyg.sftp;importjava.util.HashMap;importjava.util.Map;importcom.jcraft.jsch.ChannelSftp;public classSFTPTest {publicSFTPChannel getSFTPChannel() {return newSFTPChannel();
}/***@paramargs
*@throwsException*/
public static void main(String[] args) throwsException {
SFTPTest test= newSFTPTest();
Map sftpDetails = new HashMap();//設(shè)置主機(jī)ip,端口,用戶名,密碼
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "10.9.167.55");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME,"root");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD,"arthur");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT,"22");
String src= "D:\\DevSoft\\HB-SnagIt1001.rar"; //本地文件名
String dst = "/home/omc/ylong/sftp/HB-SnagIt1001.rar"; //目標(biāo)文件名
SFTPChannel channel=test.getSFTPChannel();
ChannelSftp chSftp= channel.getChannel(sftpDetails, 60000);/*** 代碼段1
OutputStream out = chSftp.put(dst, ChannelSftp.OVERWRITE); // 使用OVERWRITE模式
byte[] buff = new byte[1024 * 256]; // 設(shè)定每次傳輸?shù)臄?shù)據(jù)塊大小為256KB
int read;
if (out != null) {
System.out.println("Start to read input stream");
InputStream is = new FileInputStream(src);
do {
read = is.read(buff, 0, buff.length);
if (read > 0) {
out.write(buff, 0, read);
}
out.flush();
} while (read >= 0);
System.out.println("input stream read done.");
}
**/chSftp.put(src, dst, ChannelSftp.OVERWRITE);//代碼段2//chSftp.put(new FileInputStream(src), dst, ChannelSftp.OVERWRITE);//代碼段3
chSftp.quit();
channel.closeChannel();
}
}
注:請(qǐng)分別將代碼段1,代碼段2,代碼段3取消注釋,運(yùn)行程序來進(jìn)行測試。這三段代碼分別演示了如何使用JSch的不同的put方法來進(jìn)行文件上傳。
代碼段1:采用向put方法返回的輸出流中寫入數(shù)據(jù)的方式來傳輸文件。?需要由程序來決定寫入什么樣的數(shù)據(jù),這里是將本地文件的輸入流寫入輸出流。采用這種方式的好處是,可以自行設(shè)定每次寫入輸出流的數(shù)據(jù)塊大小,如本示例中的語句:
byte[] buff = new byte[1024 * 256]; //設(shè)定每次傳輸?shù)臄?shù)據(jù)塊大小為256KB
代碼段2:直接將本地文件名為src的文件上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst。(注:使用這個(gè)方法時(shí),dst可以是目錄,當(dāng)dst是目錄時(shí),上傳后的目標(biāo)文件名將與src文件名相同)
代碼段3:將本地文件名為src的文件輸入流上傳到目標(biāo)服務(wù)器,目標(biāo)文件名為dst。
這三段代碼實(shí)現(xiàn)的功能是一樣的,都是將本地的文件src上傳到了服務(wù)器的dst文件。使用時(shí)可根據(jù)具體情況選擇使用哪種實(shí)現(xiàn)方式。
監(jiān)控傳輸進(jìn)度
從前面的介紹中知道,JSch支持在文件傳輸時(shí)對(duì)傳輸進(jìn)度的監(jiān)控。可以實(shí)現(xiàn)JSch提供的SftpProgressMonitor接口來完成這個(gè)功能。
SftpProgressMonitor接口類的定義為:
SftpProgressMonitor.java
packagecom.jcraft.jsch;public interfaceSftpProgressMonitor{public static final int PUT=0;public static final int GET=1;void init(int op, String src, String dest, longmax);boolean count(longcount);voidend();
}
init():當(dāng)文件開始傳輸時(shí),調(diào)用init方法。
count(): 當(dāng)每次傳輸了一個(gè)數(shù)據(jù)塊后,調(diào)用count方法,count方法的參數(shù)為這一次傳輸?shù)臄?shù)據(jù)塊大小。
end():當(dāng)傳輸結(jié)束時(shí),調(diào)用end方法。
下面是一個(gè)簡單的實(shí)現(xiàn):
MyProgressMonitor.java
MyProgressMonitor.java
packagecom.longyg.sftp;importcom.jcraft.jsch.SftpProgressMonitor;public class MyProgressMonitor implementsSftpProgressMonitor {private longtransfered;
@Overridepublic boolean count(longcount) {
transfered= transfered +count;
System.out.println("Currently transferred total size: " + transfered + " bytes");return true;
}
@Overridepublic voidend() {
System.out.println("Transferring done.");
}
@Overridepublic void init(int op, String src, String dest, longmax) {
System.out.println("Transferring begin.");
}
}
此時(shí)如果改變SFTPTest main方法里調(diào)用的put方法,即可實(shí)現(xiàn)監(jiān)控傳輸進(jìn)度:
SFTPTest.java
SFTPTest.java
packagecom.longyg.sftp;importjava.util.HashMap;importjava.util.Map;importcom.jcraft.jsch.ChannelSftp;public classSFTPTest {publicSFTPChannel getSFTPChannel() {return newSFTPChannel();
}/***@paramargs
*@throwsException*/
public static void main(String[] args) throwsException {
SFTPTest test= newSFTPTest();
Map sftpDetails = new HashMap();//設(shè)置主機(jī)ip,端口,用戶名,密碼
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "10.9.167.55");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME,"root");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD,"arthur");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT,"22");
String src= "D:\\DevSoft\\HB-SnagIt1001.rar"; //本地文件名
String dst = "/home/omc/ylong/sftp/HB-SnagIt1001.rar"; //目標(biāo)文件名
SFTPChannel channel=test.getSFTPChannel();
ChannelSftp chSftp= channel.getChannel(sftpDetails, 60000);/*** 代碼段1
OutputStream out = chSftp.put(dst, new MyProgressMonitor(), ChannelSftp.OVERWRITE); // 使用OVERWRITE模式
byte[] buff = new byte[1024 * 256]; // 設(shè)定每次傳輸?shù)臄?shù)據(jù)塊大小為256KB
int read;
if (out != null) {
System.out.println("Start to read input stream");
InputStream is = new FileInputStream(src);
do {
read = is.read(buff, 0, buff.length);
if (read > 0) {
out.write(buff, 0, read);
}
out.flush();
} while (read >= 0);
System.out.println("input stream read done.");
}
**/chSftp.put(src, dst,new MyProgressMonitor(), ChannelSftp.OVERWRITE); //代碼段2//chSftp.put(new FileInputStream(src), dst, new MyProgressMonitor(), ChannelSftp.OVERWRITE);//代碼段3
chSftp.quit();
channel.closeChannel();
}
}
注意修改的內(nèi)容僅僅是put方法,在put方法中增加了SftpProgressMonitor的實(shí)現(xiàn)類對(duì)象monitor作為參數(shù),即添加了對(duì)進(jìn)度監(jiān)控的支持。
運(yùn)行,輸出結(jié)果如下:
logs
Start to read input stream
Currently transferred total size:262144bytes
Currently transferred total size:524288bytes
Currently transferred total size:786432bytes
Currently transferred total size:1048576bytes
Currently transferred total size:1310720bytes
Currently transferred total size:1572864bytes
Currently transferred total size:1835008bytes
Currently transferred total size:2097152bytes
Currently transferred total size:2359296bytes
Currently transferred total size:2621440bytes
Currently transferred total size:2883584bytes
Currently transferred total size:3145728bytes
Currently transferred total size:3407872bytes
Currently transferred total size:3670016bytes
Currently transferred total size:3848374bytes
input stream read done.
當(dāng)然這個(gè)SftpProgressMonitor的實(shí)現(xiàn)實(shí)在太簡單。JSch每次傳輸一個(gè)數(shù)據(jù)塊,就會(huì)調(diào)用count方法來實(shí)現(xiàn)主動(dòng)進(jìn)度通知。
現(xiàn)在我們希望每間隔一定的時(shí)間才獲取一下文件傳輸?shù)倪M(jìn)度。。。看看下面的SftpProgressMonitor實(shí)現(xiàn):
FileProgressMonitor.java
packagecom.longyg.sftp;importjava.text.DecimalFormat;importjava.util.Timer;importjava.util.TimerTask;importcom.jcraft.jsch.SftpProgressMonitor;public class FileProgressMonitor extends TimerTask implementsSftpProgressMonitor {private long progressInterval = 5 * 1000; //默認(rèn)間隔時(shí)間為5秒
private boolean isEnd = false; //記錄傳輸是否結(jié)束
private long transfered; //記錄已傳輸?shù)臄?shù)據(jù)總大小
private long fileSize; //記錄文件總大小
private Timer timer; //定時(shí)器對(duì)象
private boolean isScheduled = false; //記錄是否已啟動(dòng)timer記時(shí)器
public FileProgressMonitor(longfileSize) {this.fileSize =fileSize;
}
@Overridepublic voidrun() {if (!isEnd()) { //判斷傳輸是否已結(jié)束
System.out.println("Transfering is in progress.");long transfered =getTransfered();if (transfered != fileSize) { //判斷當(dāng)前已傳輸數(shù)據(jù)大小是否等于文件總大小
System.out.println("Current transfered: " + transfered + " bytes");
sendProgressMessage(transfered);
}else{
System.out.println("File transfering is done.");
setEnd(true); //如果當(dāng)前已傳輸數(shù)據(jù)大小等于文件總大小,說明已完成,設(shè)置end
}
}else{
System.out.println("Transfering done. Cancel timer.");
stop();//如果傳輸結(jié)束,停止timer記時(shí)器
return;
}
}public voidstop() {
System.out.println("Try to stop progress monitor.");if (timer != null) {
timer.cancel();
timer.purge();
timer= null;
isScheduled= false;
}
System.out.println("Progress monitor stoped.");
}public voidstart() {
System.out.println("Try to start progress monitor.");if (timer == null) {
timer= newTimer();
}
timer.schedule(this, 1000, progressInterval);
isScheduled= true;
System.out.println("Progress monitor started.");
}/*** 打印progress信息
*@paramtransfered*/
private void sendProgressMessage(longtransfered) {if (fileSize != 0) {double d = ((double)transfered * 100)/(double)fileSize;
DecimalFormat df= new DecimalFormat( "#.##");
System.out.println("Sending progress message: " + df.format(d) + "%");
}else{
System.out.println("Sending progress message: " +transfered);
}
}/*** 實(shí)現(xiàn)了SftpProgressMonitor接口的count方法*/
public boolean count(longcount) {if (isEnd()) return false;if (!isScheduled) {
start();
}
add(count);return true;
}/*** 實(shí)現(xiàn)了SftpProgressMonitor接口的end方法*/
public voidend() {
setEnd(true);
System.out.println("transfering end.");
}private synchronized void add(longcount) {
transfered= transfered +count;
}private synchronized longgetTransfered() {returntransfered;
}public synchronized void setTransfered(longtransfered) {this.transfered =transfered;
}private synchronized void setEnd(booleanisEnd) {this.isEnd =isEnd;
}private synchronized booleanisEnd() {returnisEnd;
}public void init(int op, String src, String dest, longmax) {//Not used for putting InputStream
}
}
再次修改SFTPTest main方法里的put方法,改為使用新的SftpProgressMonitor的實(shí)現(xiàn)類對(duì)象monitor作為參數(shù),注意新的monitor對(duì)象的構(gòu)造函數(shù)需要傳入文件大小作為參數(shù):
SFTPTest.java
packagecom.longyg.sftp;importjava.io.File;importjava.util.HashMap;importjava.util.Map;importcom.jcraft.jsch.ChannelSftp;public classSFTPTest {publicSFTPChannel getSFTPChannel() {return newSFTPChannel();
}/***@paramargs
*@throwsException*/
public static void main(String[] args) throwsException {
SFTPTest test= newSFTPTest();
Map sftpDetails = new HashMap();//設(shè)置主機(jī)ip,端口,用戶名,密碼
sftpDetails.put(SFTPConstants.SFTP_REQ_HOST, "10.9.167.55");
sftpDetails.put(SFTPConstants.SFTP_REQ_USERNAME,"root");
sftpDetails.put(SFTPConstants.SFTP_REQ_PASSWORD,"arthur");
sftpDetails.put(SFTPConstants.SFTP_REQ_PORT,"22");
String src= "D:\\DevSoft\\HB-SnagIt1001.rar"; //本地文件名
String dst = "/home/omc/ylong/sftp/HB-SnagIt1001.rar"; //目標(biāo)文件名
SFTPChannel channel=test.getSFTPChannel();
ChannelSftp chSftp= channel.getChannel(sftpDetails, 60000);
File file= newFile(src);long fileSize =file.length();/*** 代碼段1
OutputStream out = chSftp.put(dst, new FileProgressMonitor(fileSize), ChannelSftp.OVERWRITE); // 使用OVERWRITE模式
byte[] buff = new byte[1024 * 256]; // 設(shè)定每次傳輸?shù)臄?shù)據(jù)塊大小為256KB
int read;
if (out != null) {
System.out.println("Start to read input stream");
InputStream is = new FileInputStream(src);
do {
read = is.read(buff, 0, buff.length);
if (read > 0) {
out.write(buff, 0, read);
}
out.flush();
} while (read >= 0);
System.out.println("input stream read done.");
}
**/chSftp.put(src, dst,new FileProgressMonitor(fileSize), ChannelSftp.OVERWRITE); //代碼段2//chSftp.put(new FileInputStream(src), dst, new FileProgressMonitor(fileSize), ChannelSftp.OVERWRITE);//代碼段3
chSftp.quit();
channel.closeChannel();
}
}
再次運(yùn)行,結(jié)果輸出為:
logs
Try to start progress monitor.
Progress monitor started.
Transfering is in progress.
Current transfered: 98019 bytes
Sending progress message: 2.55%
Transfering is in progress.
Current transfered: 751479 bytes
Sending progress message: 19.53%
Transfering is in progress.
Current transfered: 1078209 bytes
Sending progress message: 28.02%
......
Transfering is in progress.
Current transfered: 3430665 bytes
Sending progress message: 89.15%
transfering end.
Transfering done. Cancel timer.
Try to stop progress monitor.
Progress monitor stoped.
現(xiàn)在,程序每隔5秒鐘才會(huì)打印一下進(jìn)度信息。可以修改FileProgressMonitor類里的progressInterval變量的值,來修改默認(rèn)的間隔時(shí)間。
總結(jié)
以上是生活随笔為你收集整理的channelsftp的put_JSch - Java实现的SFTP(文件上传详解篇)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3实用编程技巧_6.pyth
- 下一篇: 怎么做笔记标签贴_小红书笔记互动到底该怎