sftp下载文件并监控文件新增内容并按照指定速率把新内容追加到已下载内容后面

package com.lb.web.service.file;

import com.jcraft.jsch.JSchException;

/**
 * 在固定时间间隔调用一次downloadSftpFile(可以配合定时任务使用 )
 * 可以把文件新增的内容按自定的传输速率下载下来
 * 并把新增的内容增加到文件末尾
 */
public class FileDownload {

    public static String file1 = "pros-Tomcat-dc1odprdev11.log"; //需要下载的文件的文件名
    public static String path5r=""; 
    public static String pathdst5r = "E:\\app";
    public static String url5=""; 
    public static String username5="";
    public static String password5="";
    public static int port = 22;


    public static void main(String[] args) throws JSchException, Exception{
            //index为下载总量的大小,存入项目缓存,如需使用需要保证该线程不中断
            //模拟定时任务 每10秒进行一次下载操作
            while(true){
                Sftp.downloadSftpFile(url5, username5, password5, port, path5r, pathdst5r, file1);
                Thread.sleep(10000);
            }
    }
}
----------------------------------------------------------------------------------
package com.lb.web.service.file;

import com.jcraft.jsch.SftpProgressMonitor;

public class ProgressMonitor implements SftpProgressMonitor {
    public static long transfered;
    //init():    当文件开始传输时,调用init方法。
    //count():   当每次传输了一个数据块后,调用count方法,count方法的参数为这一次传输的数据块大小。
    //end():     当传输结束时,调用end方法。

    public boolean count(long count) {
//      count=this.count;
        System.out.println("count="+count);
        transfered = transfered + count; 
        System.out.println("Currently transferred total size: " + transfered + " bytes");
        System.out.println("此次传输的数据大小为:" + count);
        return true;
    }

    public void end() {
        System.out.println("Transferring done.");
    }

    public void init(int op, String src, String dest, long max) {
        System.out.println("Transferring begin.");
    }
}
----------------------------------------------------------------------------------
package com.lb.web.service.file;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class Sftp {
    private static long index=0;
    public static Session getSession(String host,String user,String psw,int port){
        Session session =null;
        try {
            JSch jsch=new JSch();
            session = jsch.getSession(user, host, port);
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.setPassword(psw);
            session.connect();
            System.out.println("Session connected. session has connected");
        } catch (JSchException e) {
            e.printStackTrace();
        }finally{
        }
        return session;
    }

    public static void downloadSftpFile(String ftpHost, String ftpUserName,  
            String ftpPassword, int ftpPort, String ftpPath, String localPath, String file1) throws JSchException, Exception {  
        Session session = null;  
        Channel channel = null;  
        JSch jsch = new JSch();  
        session = jsch.getSession(ftpUserName, ftpHost, ftpPort);  
        session.setPassword(ftpPassword);  
        session.setTimeout(100000);  
        Properties config = new Properties();  
        config.put("StrictHostKeyChecking", "no");  
        session.setConfig(config);  
        session.connect();  
        channel = session.openChannel("sftp");  
        channel.connect();  
        ChannelSftp chSftp = (ChannelSftp) channel;  

        OutputStream  out=  new FileOutputStream(localPath+"/"+file1, true);     

        try {
            InputStream is =chSftp.get(ftpPath+"/"+file1,new ProgressMonitor(),index);
             byte[] buff = new byte[1024 * 2];//按指定速率下载
             int read;
             if (is != null) {
                 System.out.println("Start to read input stream");
                // is.skip(30000);
                 do {
                     read = is.read(buff, 0, buff.length);
                     if (read > 0) {
                         out.write(buff, 00, read);
                     }
                     out.flush();
                 } while (read >= 0);
                 System.out.println("input stream read done.");
                 index=ProgressMonitor.transfered;
             }

        } catch (Exception e) {  
            e.printStackTrace();  
                try {
                    chSftp.get(ftpPath+"/"+file1, localPath);
                } catch (SftpException e1) {
                    e1.printStackTrace();
                }
        } finally {  
            chSftp.quit();  
            channel.disconnect();  
            session.disconnect();
            out.close();
        }  
    } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

StrideBin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值