你們若是熟悉Linux系統話,對ssh,sftp,scp等命令很是熟悉。ssh是一個安全協議,用來在不一樣系統或者服務器之間進行安全鏈接。ssh 在鏈接和傳送的過程當中會加密全部的數據。java
而今天我要介紹的一個jar包,是使用 JSCH。JSCH是一個純粹的用Java實現SSH功能的java library.git
官方地址爲:http://www.jcraft.com/jsch/
GitHub 地址爲:https://github.com/vngx/vngx-jschgithub
maven配置以下:安全
<!-- 加入sftp依賴包 --> <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.50</version> </dependency>
對於這樣的設計咱們先設計一個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; }
對於它的具體實現,則以下:session
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("上傳文件時發生錯誤!請檢查文件路徑是否正確"); } } @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("下載文件時發生錯誤!請檢查文件路徑是否正確"); } 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("下載文件時發生錯誤!請檢查文件路徑是否正確"); } } @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); } }
至此工具完成了。這樣咱們也能夠經過這個工具提供,網頁這樣的上傳道服務器。ssh