1、软件
使用 freesshd 进行搭建,官网下载:http://www.freesshd.com/?ctt=download , 只下载 freeSSHd.exe 就可以
2、安装
1)安装后问你是否要生成账户登录的秘钥,选择是
2)是否第一次启动服务,选择否
- freeSSHd是可以以不同的端口启动多个服务,第一次如果启动的话会占用22端口,后面22端口就无法使用了。而且第一次启动的服务由于没有进行配置启动了也没什么实际用。
- 如果不小心点是,去服务里面关掉freeSSHd Server服务即可。
3、设置
1)Authentication 验证规则
- 公钥地址建议在安装目录下或建一个Keys目录单独存放
- 下面的是是否使用密码验证与秘钥验证方式,有禁用,允许,必须使用三种方式。

2)Server status 服务状态
- 如果修改了配置文件,个人建议unload退出系统后,再以管理员重启服务,否则可能配置会不生效。
4、常见问题
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());
private String host;// 服务器连接ipprivate String username;// 用户名private String password;// 密码private int port = 23;// 端口号private static ChannelSftp sftp = null;private Session sshSession = null;public SFTPUtils() {}public SFTPUtils(String host, int port, String username, String password) {this.host = host;this.username = username;this.password = password;this.port = port;}/*** 通过SFTP连接服务器*/public void connect() {try {JSch jsch = new JSch();jsch.getSession(username, host, port);sshSession = jsch.getSession(username, host, port);if (log.isInfoEnabled()) {log.info("Session created.");}sshSession.setPassword(password);Properties sshConfig = new Properties();sshConfig.put("StrictHostKeyChecking", "no");sshSession.setConfig(sshConfig);sshSession.connect();if (log.isInfoEnabled()) {log.info("Session connected.");}Channel channel = sshSession.openChannel("sftp");channel.connect();if (log.isInfoEnabled()) {log.info("Opening Channel.");}sftp = (ChannelSftp) channel;if (log.isInfoEnabled()) {log.info("Connected to " + host + ".");}} catch (Exception e) {e.printStackTrace();}}/*** 关闭连接*/@SuppressWarnings("static-access")public void disconnect() {if (this.sftp != null) {if (this.sftp.isConnected()) {this.sftp.disconnect();if (log.isInfoEnabled()) {log.info("sftp is closed already");}}}if (this.sshSession != null) {if (this.sshSession.isConnected()) {this.sshSession.disconnect();if (log.isInfoEnabled()) {log.info("sshSession is closed already");}}}}/*** 上传单个文件** @param remotePath* :远程保存目录* @param remoteFileName* :保存文件名* @param localPath* :本地上传目录(以路径符号结束)* @param localFileName* :上传的文件名* @return*/public boolean uploadFile(String remotePath, String remoteFileName,String localPath, String localFileName) {FileInputStream in = null;try {createDir(remotePath);File file = new File(localPath + localFileName);in = new FileInputStream(file);sftp.put(in, remoteFileName);return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (SftpException e) {e.printStackTrace();} finally {if (in != null) {try {in.close();} catch (IOException e) {e.printStackTrace();}}}return false;}/*** 创建目录** @param createpath* @return*/public boolean createDir(String createpath) {try {if (isDirExist(createpath)) {sftp.cd(createpath);return true;}String pathArry[] = createpath.split("/");StringBuffer filePath = new StringBuffer("/");for (String path : pathArry) {if (path.equals("")) {continue;}filePath.append(path + "/");if (isDirExist(filePath.toString())) {sftp.cd(filePath.toString());} else {// 建立目录sftp.mkdir(filePath.toString());// 进入并设置为当前目录sftp.cd(filePath.toString());}}sftp.cd(createpath);return true;} catch (SftpException e) {e.printStackTrace();}return false;}/*** 判断目录是否存在** @param directory* @return*/public boolean isDirExist(String directory) {boolean isDirExistFlag = false;try {SftpATTRS sftpATTRS = sftp.lstat(directory);isDirExistFlag = true;return sftpATTRS.isDir();} catch (Exception e) {if (e.getMessage().toLowerCase().equals("no such file")) {isDirExistFlag = false;}}return isDirExistFlag;}public static void main(String[] args){SFTPUtils sftp = null;try{sftp = new SFTPUtils("192.168.112.79",23,"meilin", "PwHqIdYZPmAU2WIqvMN2");//现在后台的SFTP的账户信息sftp.connect();// 下载//boolean flag = sftp.uploadFile("/test/", "201708081138_o7Lpot_9nrAvyz2dbLFbq7ftn374_ba89d4.jpg", "F:/", "201708081138_o7Lpot_9nrAvyz2dbLFbq7ftn374_ba89d4.jpg"); //上传文件//System.out.println(flag);}catch (Exception e){e.printStackTrace();}finally{sftp.disconnect();}}
} ```
