1. 程式人生 > >Java實現SFTP客戶端

Java實現SFTP客戶端

txt nta 獲取 per logger rop ava pat java

1、SFTP下載Excel並解析,調試代碼

public InputStream downloadFileStream(String remotePath,
            String regexFileName) {
        InputStream fielInput = null;
        String requireRemoteFileName = "";
        try {
            Vector v = listFiles(remotePath);
            // sftp.cd(remotePath);
            if
(v.size() > 0) { log.info("本次處理文件個數為fileSize={}", v.size()); int matchFileMaxModifyTime = 0; Iterator it = v.iterator(); while (it.hasNext()) { LsEntry entry = (LsEntry) it.next(); String remoteFilename
= entry.getFilename(); if (remoteFilename.contains(regexFileName)) { SftpATTRS attrs = entry.getAttrs(); if (attrs.getSize() > 0) { int mTime = attrs.getMTime(); if (matchFileMaxModifyTime <= mTime) { matchFileMaxModifyTime
= mTime; requireRemoteFileName = remoteFilename; } } } } } fielInput = sftp.get(remotePath + requireRemoteFileName); if (log.isInfoEnabled()) { log.info("DownloadFile:" + requireRemoteFileName + " success from sftp."); } return fielInput; } catch (SftpException e) { log.error("downloadFileStream error:{}", e); } finally { } return fielInput; }
package sftp;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import excel.ExcelUtils;

public class SFTPTest {
    private static Logger log = LoggerFactory.getLogger(SFTPUtils.class);

    public static void main(String[] args) {
        logInit();
        // sftpTest();
        sftpTest02();
    }
    private static void sftpTest02() {
        String remotePath = "/sftpremote/";
        SFTPUtils sftp = null;
        InputStream inputStream = null;
        try {
            sftp = new SFTPUtils("127.0.0.1", "admin01", "123");
            sftp.connect();
            // 下載
            inputStream = sftp.downloadFileStream(remotePath, "201905");
            if (null != inputStream) {
                ExcelUtils.getDataFromExcel(inputStream);
                inputStream.close();
            }
        } catch (Exception e) {
            log.error("download error:{}", e);
        } finally {
            log.info("sftp disconnect");
            sftp.disconnect();
        }
    }
    private static void sftpTest() {
        SFTPUtils sftp = null;
        // 本地存放地址,需要加最後的“\\”,才能下載到指定目錄,否則是上一級目錄
        String localPath = "E:\\book\\sftptest\\sftpdownload\\";
        // Sftp下載路徑
        String remotePath = "/sftpremote/";
        try {
            sftp = new SFTPUtils("127.0.0.1", "admin01", "123");
            sftp.connect();
            // 下載
            sftp.downloadFile(remotePath, "test.txt", localPath,
                    "testLocal.txt");
        } catch (Exception e) {
            log.error("download error:{}", e);
        } finally {
            log.info("sftp disconnect");
            sftp.disconnect();
        }
    }
    public static void logInit() {
        Properties logProp = new Properties();
        try {
            FileInputStream logIn = new FileInputStream("log4j.properties");
            logProp.load(logIn);
            logIn.close();
            PropertyConfigurator.configure(logProp);
        } catch (Exception e) {
            log.error("logInit error:{}", e);
        }

    }

}
package excel;

import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExcelUtils {
    private static Logger log = LoggerFactory.getLogger(ExcelUtils.class);

    public static void getDataFromExcel(InputStream inputStream) {
        Workbook wookbook = null;
        try {
            wookbook = new XSSFWorkbook(inputStream);

        } catch (IOException e) {
            e.printStackTrace();
        }
        // 得到一個工作表
        Sheet sheet = wookbook.getSheetAt(0);
        // 獲得數據的總行數
        int totalRowNum = sheet.getLastRowNum();
        log.info("totalRowNum:{}", totalRowNum);
        // 獲得所有數據
        for (int i = 2; i <= totalRowNum; i++) {
            // 不搞反射,直接獲取數據
            // 獲得第i行對象
            Row row = sheet.getRow(i);
            // 獲得獲得第i行第0列的 String類型對象
            Cell cell = row.getCell(0);
            String row01 = cell.getStringCellValue().toString();
            // 獲得獲得第i行第1列的 String類型對象
            cell = row.getCell(1);
            int row02 = (int) cell.getNumericCellValue();
            log.info("{} {}", row01, row02);
        }
    }
}

Java實現SFTP客戶端