记录:备忘录。
1.ftp的上传与下载
public class FtpUtils {
private static String hostName = "127.0.0.1";
private static int port = 21;
private static String userName = "root";
private static String password = "123456";
private static String pathName = "/";
private static String savePath = "D://ftpdown//";
private static String upPath = "D://ftpdown//";
public static void main(String[] args) {
System.out.println("测试开始");
downFileFromFtp("abcdef.txt");
upFileToFtp("12345.txt");
System.out.println("测试结束");
}
/**
* 上传文件到ftp服务器
*/
public static void upFileToFtp(String fileName) {
FTPClient ftpClient = null;
FileInputStream inputStream = null;
try {
// 创建客户端
ftpClient = new FTPClient();
// 连接FTP服务
ftpClient.connect(hostName, port);
// 登入FTP服务器
ftpClient.login(userName, password);
// 判断连接是否成功
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("连接失败.");
ftpClient.disconnect();
return;
}
// 设置FTP连接控制的字符编码
ftpClient.setControlEncoding("UTF-8");
// 设置文件传输类型
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置上传目录,即FTP服务器指定目录
ftpClient.changeWorkingDirectory(pathName);
// 设置缓存
ftpClient.setBufferSize(1024 * 8);
// 获取文件流
inputStream = new FileInputStream(new File(upPath + fileName));
// 上传文件
boolean tt = ftpClient.storeFile(fileName, inputStream);
ftpClient.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
/**
* 从ftp服务器下载文件到本地
*/
public static void downFileFromFtp(String fileName) {
StringBuffer sb = new StringBuffer();
FTPClient ftpClient = null;
File file = null;
FileOutputStream outputStream = null;
try {
// 创建客户端
ftpClient = new FTPClient();
// 连接FTP服务
ftpClient.connect(hostName, port);
// 登入FTP服务器
ftpClient.login(userName, password);
// 判断连接是否成功
int replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
System.out.println("连接失败.");
ftpClient.disconnect();
return;
}
// 设置FTP连接控制的字符编码
ftpClient.setControlEncoding("UTF-8");
// 设置文件传输类型
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置上传目录,即FTP服务器指定目录
ftpClient.changeWorkingDirectory(pathName);
// 设置缓存
ftpClient.setBufferSize(1024 * 8);
//获取FTP文件列表
FTPFile[] listFiles = ftpClient.listFiles();
for (FTPFile ftpFile : listFiles) {
sb.append(ftpFile.getName() + " ");
}
String ftpFile = sb.toString();
// 判断FTP中是否包含文件
if (ftpFile.contains(fileName)) {
//文件输出流
file = new File(savePath + "/" + fileName);
outputStream = new FileOutputStream(file);
//文件下载
ftpClient.retrieveFile(fileName, outputStream);
System.out.println(fileName + "文件已下载成功");
} else {
System.out.println(fileName + "文件下载失败,文件不存在");
}
ftpClient.logout();
return;
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件下载异常");
return;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ftpClient != null) {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
2.sftp的上传与下载
public class SftpUtils {
private static String hostName = "127.0.0.1";
private static int port = 10022;
private static String userName = "root";
private static String password = "123456";
private static String pathName = "/";
private static String savePath = "D://sftpdown//";
private static String upPath = "D://sftpdown//";
public static void main(String[] args) {
System.out.println("测试开始");
downFileFromSftp("abcdef.txt");
upFileToSftp("123456.txt");
System.out.println("测试结束");
}
/**
* 上传文件到sftp服务器
*/
@SuppressWarnings("resource")
public static void upFileToSftp(String fileName) {
ChannelSftp channelSftp = null;
Session sshSession = null;
OutputStream outputstream = null;
InputStream inputstream = null;
try {
JSch jsch = new JSch();
// 获取Session
sshSession = jsch.getSession(userName, hostName, port);
// 设置密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
// 严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
// 开启sshSession链接
sshSession.connect();
// 获取sftp通道
Channel channel = sshSession.openChannel("sftp");
// Channel连接
channel.connect();
channelSftp = (ChannelSftp) channel;
// 需要下载的文件
outputstream = channelSftp.put(fileName);
inputstream = new FileInputStream(new File(upPath + fileName));
byte buff[] = new byte[1024 * 8];
int length;
while ((length = inputstream.read(buff)) != -1) {
outputstream.write(buff, 0, length);
}
outputstream.flush();
System.out.println("文件已经上传.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("文件已经上传失败.");
return;
} finally {
if (outputstream != null) {
try {
outputstream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (channelSftp != null) {
if (channelSftp.isConnected()) {
channelSftp.disconnect();
}
}
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
}
/**
* 从sftp服务器下载文件
*/
public static void downFileFromSftp(String fileName) {
ChannelSftp channelSftp = null;
Session sshSession = null;
FileOutputStream outputStream = null;
try {
JSch jsch = new JSch();
// 获取Session
sshSession = jsch.getSession(userName, hostName, port);
// 设置密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
// 严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
// 开启sshSession链接
sshSession.connect();
// 获取sftp通道
Channel channel = sshSession.openChannel("sftp");
// Channel连接
channel.connect();
channelSftp = (ChannelSftp) channel;
if (pathName != null && !"".equals(pathName)) {
channelSftp.cd(pathName);
}
File file = null;
// 获取sftp目录下所有的文件
@SuppressWarnings("unchecked")
Vector<String> ls = channelSftp.ls(pathName);
String s = ls.toString();
if (s.contains(fileName)) {
file = new File(savePath + "/" + fileName);
outputStream = new FileOutputStream(file);
channelSftp.get(fileName, outputStream);
System.out.println(fileName + "文件已下载至本地");
} else {
System.out.println(fileName + "文件下载失败");
}
return;
} catch (Exception e) {
e.printStackTrace();
return;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (channelSftp != null) {
if (channelSftp.isConnected()) {
channelSftp.disconnect();
}
}
if (sshSession != null) {
if (sshSession.isConnected()) {
sshSession.disconnect();
}
}
}
}
}
3.使用测试工具
ftp使用工具FileZilla Server在windows搭建ftp服务器
sftp使用工具FreeSSHd在windows搭建sftp服务器
4.问题点
4.1 使用ftpClient.storeFile()一直返回false
本例是由于FileZilla Server启动后,需对ftp指定目录开发读写权限。
4.2 FileZilla Server和FreeSSHd在工具指定目录在代码中就是根目录,使用/开始。
以上,感谢。