windows安装sftp服务
一、下载安装
1.下载freeSSHD.exe
下载地址 freeSSHd 1.3.1 - Download
运行安装过程中会提示"提示私有密钥没安装",点击确认
是否运行 ,点击确认
2.设置
1.添加用户
2.设置SSH服务器ip,端口等信息
3. 设置登录授权选项,其中Public key auth意指通过公钥登录
4. 设置sftp服务器根目录
5. 启动服务器Server Status
如果出现错误弹窗:generic error for access violation
解决方法:服务被占用,关闭系统服务
二、使用xftp连接测试
输入正确用户名和密码提示denied或者key verification failed.,常规解决方案如下
删除C:UsersAdministrator下的.ssh文件夹,使用管理员程序重新运行freesshd,再次测试连接
三、服务集成(java)
1.pom.xml中引入jsch
com.jcraft
jsch
0.1.54
2.新建工具类package com.patrol.app.common; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class MySFTP { /** * 连接sftp服务器 * @param host 主机 * @param port 端口 * @param username 用户名 * @param password 密码 * @return */ public ChannelSftp connect(String host, int port, String username, String password) { ChannelSftp sftp = null; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); System.out.println("Session created."); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); System.out.println("Session connected."); System.out.println("Opening Channel."); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; System.out.println("Connected to " + host + "."); } catch (Exception e) { } return sftp; } /** * 上传文件 * @param directory 上传的目录 * @param uploadFile 要上传的文件 * @param sftp */ public void upload(String directory, String uploadFile, ChannelSftp sftp) { try { sftp.cd(directory); File file=new File(uploadFile); sftp.put(new FileInputStream(file), file.getName()); } catch (Exception e) { e.printStackTrace(); } } /** * 下载文件 * @param directory 下载目录 * @param downloadFile 下载的文件 * @param saveFile 存在本地的路径 * @param sftp */ public void download(String directory, String downloadFile,String saveFile, ChannelSftp sftp) { try { sftp.cd(directory); File file=new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } catch (Exception e) { e.printStackTrace(); } } /** * 删除文件 * @param directory 要删除文件所在目录 * @param deleteFile 要删除的文件 * @param sftp */ public void delete(String directory, String deleteFile, ChannelSftp sftp) { try { sftp.cd(directory); sftp.rm(deleteFile); } catch (Exception e) { e.printStackTrace(); } } /** * 列出目录下的文件 * @param directory 要列出的目录 * @param sftp * @return * @throws SftpException */ public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException{ return sftp.ls(directory); } public static void main(String[] args) { MySFTP sf = new MySFTP(); String host = "10.65.12.***"; int port = 22; String username = "hy**"; String password = "123456"; String directory = "/"; String uploadFile = "D:index.jpg"; String downloadFile = "upload.txt"; String saveFile = "D:tmpdownload.txt"; String deleteFile = "delete.txt"; //获取sftp ChannelSftp sftp=sf.connect(host, port, username, password); if(sftp != null){ //上传文件 sf.upload(directory, uploadFile, sftp); /* sf.download(directory, downloadFile, saveFile, sftp); sf.delete(directory, deleteFile, sftp);*/ } try{ sftp.cd(directory); sftp.mkdir("ss"); System.out.println("finished"); }catch(Exception e){ e.printStackTrace(); } } }
main仅为测试方便,实际应用时建议将
upload(String directory, String uploadFile, ChannelSftp sftp)方法的第二个参数类型改为FileInputStream