1、软件

使用 freesshd 进行搭建,官网下载:http://www.freesshd.com/?ctt=download , 只下载 freeSSHd.exe 就可以

2、安装

一路默认就可以。

1)安装后问你是否要生成账户登录的秘钥,选择是

image.png

2)是否第一次启动服务,选择否

  • freeSSHd是可以以不同的端口启动多个服务,第一次如果启动的话会占用22端口,后面22端口就无法使用了。而且第一次启动的服务由于没有进行配置启动了也没什么实际用。
  • 如果不小心点是,去服务里面关掉freeSSHd Server服务即可。

image.png

3、设置

1)Authentication 验证规则

  • 公钥地址建议在安装目录下或建一个Keys目录单独存放
  • 下面的是是否使用密码验证与秘钥验证方式,有禁用,允许,必须使用三种方式。

image.png
2)Server status 服务状态

  • 如果修改了配置文件,个人建议unload退出系统后,再以管理员重启服务,否则可能配置会不生效。

image.png

4、常见问题

  1. 账号密码正确确无法连接:没有使用管理员权限运行

    5、提供一个连接服务器的工具类

    ```java package test;

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties;

import org.apache.log4j.Logger;

import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpATTRS; import com.jcraft.jsch.SftpException;

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

  1. private String host;// 服务器连接ip
  2. private String username;// 用户名
  3. private String password;// 密码
  4. private int port = 23;// 端口号
  5. private static ChannelSftp sftp = null;
  6. private Session sshSession = null;
  7. public SFTPUtils() {
  8. }
  9. public SFTPUtils(String host, int port, String username, String password) {
  10. this.host = host;
  11. this.username = username;
  12. this.password = password;
  13. this.port = port;
  14. }
  15. /**
  16. * 通过SFTP连接服务器
  17. */
  18. public void connect() {
  19. try {
  20. JSch jsch = new JSch();
  21. jsch.getSession(username, host, port);
  22. sshSession = jsch.getSession(username, host, port);
  23. if (log.isInfoEnabled()) {
  24. log.info("Session created.");
  25. }
  26. sshSession.setPassword(password);
  27. Properties sshConfig = new Properties();
  28. sshConfig.put("StrictHostKeyChecking", "no");
  29. sshSession.setConfig(sshConfig);
  30. sshSession.connect();
  31. if (log.isInfoEnabled()) {
  32. log.info("Session connected.");
  33. }
  34. Channel channel = sshSession.openChannel("sftp");
  35. channel.connect();
  36. if (log.isInfoEnabled()) {
  37. log.info("Opening Channel.");
  38. }
  39. sftp = (ChannelSftp) channel;
  40. if (log.isInfoEnabled()) {
  41. log.info("Connected to " + host + ".");
  42. }
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. /**
  48. * 关闭连接
  49. */
  50. @SuppressWarnings("static-access")
  51. public void disconnect() {
  52. if (this.sftp != null) {
  53. if (this.sftp.isConnected()) {
  54. this.sftp.disconnect();
  55. if (log.isInfoEnabled()) {
  56. log.info("sftp is closed already");
  57. }
  58. }
  59. }
  60. if (this.sshSession != null) {
  61. if (this.sshSession.isConnected()) {
  62. this.sshSession.disconnect();
  63. if (log.isInfoEnabled()) {
  64. log.info("sshSession is closed already");
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 上传单个文件
  71. *
  72. * @param remotePath
  73. * :远程保存目录
  74. * @param remoteFileName
  75. * :保存文件名
  76. * @param localPath
  77. * :本地上传目录(以路径符号结束)
  78. * @param localFileName
  79. * :上传的文件名
  80. * @return
  81. */
  82. public boolean uploadFile(String remotePath, String remoteFileName,
  83. String localPath, String localFileName) {
  84. FileInputStream in = null;
  85. try {
  86. createDir(remotePath);
  87. File file = new File(localPath + localFileName);
  88. in = new FileInputStream(file);
  89. sftp.put(in, remoteFileName);
  90. return true;
  91. } catch (FileNotFoundException e) {
  92. e.printStackTrace();
  93. } catch (SftpException e) {
  94. e.printStackTrace();
  95. } finally {
  96. if (in != null) {
  97. try {
  98. in.close();
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. /**
  107. * 创建目录
  108. *
  109. * @param createpath
  110. * @return
  111. */
  112. public boolean createDir(String createpath) {
  113. try {
  114. if (isDirExist(createpath)) {
  115. sftp.cd(createpath);
  116. return true;
  117. }
  118. String pathArry[] = createpath.split("/");
  119. StringBuffer filePath = new StringBuffer("/");
  120. for (String path : pathArry) {
  121. if (path.equals("")) {
  122. continue;
  123. }
  124. filePath.append(path + "/");
  125. if (isDirExist(filePath.toString())) {
  126. sftp.cd(filePath.toString());
  127. } else {
  128. // 建立目录
  129. sftp.mkdir(filePath.toString());
  130. // 进入并设置为当前目录
  131. sftp.cd(filePath.toString());
  132. }
  133. }
  134. sftp.cd(createpath);
  135. return true;
  136. } catch (SftpException e) {
  137. e.printStackTrace();
  138. }
  139. return false;
  140. }
  141. /**
  142. * 判断目录是否存在
  143. *
  144. * @param directory
  145. * @return
  146. */
  147. public boolean isDirExist(String directory) {
  148. boolean isDirExistFlag = false;
  149. try {
  150. SftpATTRS sftpATTRS = sftp.lstat(directory);
  151. isDirExistFlag = true;
  152. return sftpATTRS.isDir();
  153. } catch (Exception e) {
  154. if (e.getMessage().toLowerCase().equals("no such file")) {
  155. isDirExistFlag = false;
  156. }
  157. }
  158. return isDirExistFlag;
  159. }
  160. public static void main(String[] args)
  161. {
  162. SFTPUtils sftp = null;
  163. try
  164. {
  165. sftp = new SFTPUtils("192.168.112.79",23,"meilin", "PwHqIdYZPmAU2WIqvMN2");//现在后台的SFTP的账户信息
  166. sftp.connect();
  167. // 下载
  168. //boolean flag = sftp.uploadFile("/test/", "201708081138_o7Lpot_9nrAvyz2dbLFbq7ftn374_ba89d4.jpg", "F:/", "201708081138_o7Lpot_9nrAvyz2dbLFbq7ftn374_ba89d4.jpg"); //上传文件
  169. //System.out.println(flag);
  170. }
  171. catch (Exception e)
  172. {
  173. e.printStackTrace();
  174. }
  175. finally
  176. {
  177. sftp.disconnect();
  178. }
  179. }

} ```