最近需要開發FTP功能, 使用Jsch.
問題一:
其中一個功能為抓取固定路徑下的所有檔案, 一個一個透過SFTP丟到Web applinacion,
跑迴圈, 每put一個檔案就呼叫file.delete(), 檔案可以正確傳過去,
但是資料夾內的檔案卻不會被刪除, 看log也沒有exception,
是因為java執行到delete時, outputStream還沒關閉嗎? 但卻不會丟錯?
問題二:
FTP功能是透過web application執行, 而檔案禁止放在web server內,
所以FTP的取檔功能目前想法是一次讀取完放記憶體, 開放inputStream拿檔.
但jsch提供的功能似乎只有inputStream,
請問要如何在web server內既不create File又能全部讀完串流?
因為檔案被管制能碰到的人有限, 所以先不用考慮同步問題.
問題三:
另外請教, 在一個執行中的web application, SFTP去replace .class跟.html,
來更新程式碼, 會有任何後遺症嗎?
目前不多人用, 系統看起來沒有出什麼奇怪的錯.
感覺是server偵測到.class修改時間有變化, 會load最新版本class進記憶體?
程式如下:
JSch jsch = new JSch();
Session sshSession = jsch.getSession(userName, ip, port);
Channel channel;
try {
        sshSession.setPassword(PASSWORD);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        config.put("PreferredAuthentications", "password");
        sshSession.setConfig(config);
        sshSession.setTimeout(5000);
        sshSession.connect();
        channel = sshSession.openChannel("sftp");
        channel.connect();
        ChannelSftp sftp = null;
        sftp = (ChannelSftp) channel;
        StringBuilder sb = new StringBuilder();
        for (File f$ : new File(SOURCE_PATH).listFiles()) {
                if(f$.isDirectory()){
                        continue;
                }
                String aixPath = getRemoteFilePath(f$, sb);
                if ("".equals(aixPath)) {
                        continue;
                }
                sftp.put(new FileInputStream(f$), aixPath);
                System.err.println(ip+":"+f$.getName());
                f$.delete();
        }
        channel.disconnect();
        sshSession.disconnect();
} finally {
        if (sshSession.isConnected()) {
                sshSession.disconnect();
        }
}