sftp java 上傳

來源:互聯網
上載者:User

標籤:ext   toc   pat   select   substr   ber   color   blog   protocol   

1. 注意問題

 uri的格式: sftp://zhangsan:[email protected]:22

 dir問題 : 判斷有沒有 沒有建立 然後進入 類推

 config問題: StrictHostKeyChecking=no

 進度問題 : 需要變數儲存

2. 代碼

    private void transfer_sftp(String attrPath, Element groupNode, long size) throws JSchException, SftpException, UnsupportedEncodingException{        attrPath = attrPath.replaceAll("sftp://", "");        String[] tmps = attrPath.split(":");        String dstUserName = tmps[0];        int port = 22;        String portstr = tmps[2].replaceAll("/", "");        try {            port = Integer.parseInt(portstr);        } catch (NumberFormatException e) {            e.printStackTrace();        }        String[] tmp = tmps[1].split("@");        String dstPassword = tmp[0];        String sftpIP = tmp[1];        List<?> files = groupNode.selectNodes("File");        for(int k=0; k<files.size(); k++){            JSch jsch = new JSch();            Session session = jsch.getSession(dstUserName, sftpIP, port);            logger.info("JSch Session created: user:"+dstUserName + ",password="+dstPassword+",sftpIP="+sftpIP+",port="+port);            session.setPassword(dstPassword);            Properties sshConfig = new Properties();            sshConfig.put("StrictHostKeyChecking", "no");            session.setConfig(sshConfig);            session.connect();            logger.info("JSch Session connected > user:"+dstUserName);            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");            channelSftp.connect();            logger.info("Connected to "+ sftpIP);            Element file = (Element) files.get(k);            String srcFilePath = file.elementText("SrcFileName");            String dstFilePath = file.elementText("DstFileName");            logger.info("upload file on sftp protocol >> srcFilePath="+srcFilePath + ",dstFilePath="+dstFilePath);            int index = dstFilePath.lastIndexOf("/");            String path = dstFilePath.substring(0, index + 1);            String filename = dstFilePath.substring(index + 1, dstFilePath.length());            if (path.startsWith("/"))                path = path.substring(1);            logger.info("dest path:"+path);            if (filename.startsWith("/"))                filename = filename.substring(1);            String[] pathArray = path.split("/");            for (String pathStr : pathArray) {                try {                    logger.info("channelsftp dir ::"+pathStr);                    Vector ls = channelSftp.ls(pathStr);                    if(ls != null){                        channelSftp.cd(pathStr);                    }                } catch (Exception e) {                    logger.info("channelsftp catch mkdir ::"+pathStr);                    channelSftp.mkdir(pathStr);                    channelSftp.cd(pathStr);                    e.printStackTrace();                }            }//            String dir = new String(path.getBytes("gb2312"), "iso-8859-1");//            String destName = new String(filename.getBytes("gb2312"), "iso-8859-1");//            System.out.println("上傳目錄名名:::"+dir);//            System.out.println("上傳後檔案名稱:::"+destName);//            channelSftp.put(srcFilePath, dstFilePath, new SftpProgressMonitor() {            channelSftp.put(srcFilePath, filename, new SftpProgressMonitor() {                                long n = 0L;                long _max = 0L;                                public long getN() {                    return n;                }                public void setN(long n) {                    this.n = n;                }                @Override                public void init(int arg0, String arg1, String arg2, long max) {                    this._max = max;                    logger.info("SFTP Transferring begin....._max="+_max);                }                                @Override                public void end() {                    logger.info("SFTP Transferring end....");                }                                @Override                public boolean count(long count) {                    n += count;//                    logger.info("SFTP Transferring  count="+count+",n="+n);                    if(_max!=0){                        run_progress = (int) ((n*100)/_max);//                        logger.info("the progress of upload file through sftp :" + run_progress + "%");                    }                    return true;                }            }, ChannelSftp.OVERWRITE);            channelSftp.quit();            session.disconnect();                    }    }

 

sftp java 上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.