JAVA代碼時間SFTP檔案的下載

來源:互聯網
上載者:User

標籤:password   div   ppa   alt   名稱   not   nts   image   mon   

參考文章:http://blog.csdn.net/smallerpig/article/details/50976191

SFTP檔案的下載與FTP檔案的下載差別較大,需要下載jsch-0.1.54jar包

package com.cd.util;  import com.jcraft.jsch.JSchException;      public class SftpDownloadTestScript {          public static void main (String[] args)throws JSchException {                    String ftpHost = "192.168.188.155";          String ftpUserName = "exftp";          String ftpPassword = "sFtP06222013";          int ftpPort = 22;          String ftpPath = ".";          String localPath = "D:/SFTP/uploadFiles/demo";          String fileName = "aa.jpg";          FtpUtil.downloadSftpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName);      }  }  
package com.cd.util;import java.io.File;  import java.io.FileNotFoundException;  import java.io.FileOutputStream;  import java.io.IOException;  import java.io.OutputStream;  import java.net.SocketException;  import java.util.Properties;    import org.apache.commons.logging.Log;  import org.apache.commons.logging.LogFactory;    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;    public class FtpUtil {        private final static Log logger = LogFactory.getLog(FtpUtil.class);              /*      * 從SFTP伺服器下載檔案      *       * @param ftpHost SFTP IP地址      *       * @param ftpUserName SFTP 使用者名稱      *       * @param ftpPassword SFTP使用者名稱密碼      *       * @param ftpPort SFTP連接埠      *       * @param ftpPath SFTP伺服器中檔案所在路徑 格式: ftptest/aa      *       * @param localPath 下載到本地的位置 格式:H:/download      *       * @param fileName 檔案名稱      */      public static void downloadSftpFile(String ftpHost, String ftpUserName,              String ftpPassword, int ftpPort, String ftpPath, String localPath,              String fileName) throws JSchException {          Session session = null;          Channel channel = null;            JSch jsch = new JSch();          System.out.println(1);        session = jsch.getSession(ftpUserName, ftpHost, ftpPort);          System.out.println(2);        session.setPassword(ftpPassword);          System.out.println(3);        session.setTimeout(100000);          System.out.println(4);        Properties config = new Properties();          System.out.println(5);        config.put("StrictHostKeyChecking", "no");          System.out.println(6);        session.setConfig(config);          System.out.println(7);        session.connect();          System.out.println(8);          channel = session.openChannel("sftp");          System.out.println(9);        channel.connect();          ChannelSftp chSftp = (ChannelSftp) channel;          System.out.println(10);          String ftpFilePath = ftpPath + "/" + fileName;          String localFilePath = localPath + File.separatorChar + fileName;          System.out.println(11);        try {              System.out.println("ftpFilePath="+ftpFilePath);            System.out.println("localFilePath="+localFilePath);            System.out.println("localPath="+localPath);            chSftp.get(ftpFilePath, localPath);             System.out.println(13);        } catch (Exception e) {              e.printStackTrace();              logger.info("download error.");          } finally {              chSftp.quit();              channel.disconnect();              session.disconnect();          }        }    }  

下面是SFTP根目錄: 

 

JAVA代碼時間SFTP檔案的下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.