記錄:備忘錄。
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在工具指定目錄在代碼中就是根目錄,使用/開始。
以上,感謝。