大家如果熟悉Linux系統(tǒng)話,對ssh,sftp,scp等命令非常熟悉。ssh是一個安全協(xié)議,用來在不同系統(tǒng)或者服務(wù)器之間進行安全連接。ssh 在連接和傳送的過程中會加密所有的數(shù)據(jù)。
而今天我要介紹的一個jar包,是使用 JSCH。JSCH是一個純粹的用Java實現(xiàn)SSH功能的java library.
官方地址為:http://www.jcraft.com/jsch/
GitHub 地址為:https://github.com/vngx/vngx-jsch
maven配置如下:
<!-- 加入sftp依賴包 --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.50</version> </dependency>
對于這樣的設(shè)計我們先設(shè)計一個FTP連接接口:
package com.xuanyuan.tools; import java.io.InputStream; import java.io.OutputStream; import java.util.Vector; /** * FTP 接口 * @author Punk Lin * @email lentr@sina.cn * @date 2016年12月23日 * */ public interface FTPService { /** * 登入 * @param host 登入主機地址 * @param userName 用戶名 * @param password 密碼 * @param port 端口 * @throws Exception */ public void login(String host, String userName, String password, int port) throws Exception; /** * 退出 */ public void logout(); /** * 上傳文件 * * @param in * 輸入流 * @param remoteFilePath * 遠程文件絕對路徑 * @throws Exception */ public void uploadFile(InputStream in, String remoteFilePath) throws Exception; /** * 文件下載到本地 * * @param sourceFilePath * 遠程文件絕對路徑 * @param localFilePath * 本地目錄或絕對路徑 * @throws Exception */ public void downloadFile(String sourceFilePath, String localFilePath) throws Exception; /** * 文件下載到輸出流 * * @param sourceFilePath * 遠程文件絕對路徑 * @param out * 輸出流 * @throws Exception */ public void downloadFile(String sourceFilePath, OutputStream out) throws Exception; /** * 刪除文件 * * @param directory * 要刪除文件所在目錄 * @param deleteFile * 要刪除的文件 * * @throws Exception */ public void deleteFile(String directory, String fileName) throws Exception; /** * 列出目錄下的文件,包括目錄 * * @param directory * 要列出的目錄 * * @return list 文件名列表 * * @throws Exception */ public Vector<?> listFiles(String directory) throws Exception; }
對于它的具體實現(xiàn),則如下:
package com.xuanyuan.tools.impl; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.Properties; import java.util.Vector; 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; import com.xuanyuan.tools.FTPService; public class SFTPServiceImpl implements FTPService { /** sftp會話session */ protected Session sshSession = null; /** sftp通道 */ protected ChannelSftp sftp = null; @Override public void login(String host, String userName, String password, int port) throws Exception { try { JSch jsch = new JSch(); this.sshSession = jsch.getSession(userName, host, port); this.sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); this.sshSession.setConfig(sshConfig); this.sshSession.connect(20000); this.sftp = (ChannelSftp) sshSession.openChannel("sftp"); this.sftp.connect(); this.sftp.setFilenameEncoding("UTF-8"); } catch (JSchException e) { throw new RuntimeException("無法使用sftp登陸,請檢查用戶名密碼或端口"); } } @Override public void logout() { if (this.sftp != null) { this.sftp.disconnect(); this.sshSession.disconnect(); this.sftp = null; this.sshSession = null; } } @Override public void uploadFile(InputStream in, String remoteFilePath) throws Exception { try { String filepath = remoteFilePath.substring(0, remoteFilePath.lastIndexOf("/")); String fileName = remoteFilePath.substring( remoteFilePath.lastIndexOf("/") + 1, remoteFilePath.length()); this.sftp.cd(filepath); this.sftp.put(in, fileName, 0); } catch (SftpException e) { throw new RuntimeException("上傳文件時發(fā)生錯誤!請檢查文件路徑是否正確"); } } @Override public void downloadFile(String sourceFilePath, String localFilePath) throws Exception { File file = new File(localFilePath); FileOutputStream out = null; try { if (file.isDirectory()) { this.sftp.get(sourceFilePath, localFilePath); } else { out = new FileOutputStream(file); this.sftp.get(sourceFilePath, out); } } catch (SftpException e) { throw new RuntimeException("下載文件時發(fā)生錯誤!請檢查文件路徑是否正確"); } finally { if (out != null) out.close(); } } @Override public void downloadFile(String sourceFilePath, OutputStream out) throws Exception { try { this.sftp.get(sourceFilePath, out); } catch (SftpException e) { throw new RuntimeException("下載文件時發(fā)生錯誤!請檢查文件路徑是否正確"); } } @Override public void deleteFile(String directory, String fileName) throws Exception { this.sftp.cd(directory); this.sftp.rm(fileName); } @Override public Vector<?> listFiles(String directory) throws Exception { return this.sftp.ls(directory); } }
至此工具完成了。這樣我們也可以通過這個工具提供,網(wǎng)頁這樣的上傳道服務(wù)器。
浙公網(wǎng)安備 33010602011771號