下面是关于“Java8实现FTP及SFTP文件上传下载”的完整攻略。
一、FTP文件上传下载
1.1 准备工作
在开始前,需要引入以下的Maven依赖:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
1.2 FTP文件上传
下面是FTP文件上传的主要代码:
public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream inputStream) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
try {
ftpClient.connect(host, port); // 连接FTP服务器
ftpClient.login(username, password); // 登录FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
return result;
}
ftpClient.changeWorkingDirectory(basePath);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile((filename), inputStream);
inputStream.close();
ftpClient.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
示例1:上传本地文件到FTP服务器
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(new File("D:\\temp\\test.txt"));
boolean result = uploadFile("10.10.10.100", 21, "username", "password", "/data/files", "test", "test.txt", fis);
System.out.println(result);
}
1.3 FTP文件下载
下面是FTP文件下载的主要代码:
public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) {
boolean result = false;
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
try {
ftpClient.connect(host, port); // 连接FTP服务器
ftpClient.login(username, password); // 登录FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
ftpClient.disconnect();
return result;
}
ftpClient.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(ff.getName(), os);
os.close();
}
}
ftpClient.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
示例2:从FTP服务器下载文件到本地
public static void main(String[] args) {
boolean result = downloadFile("10.10.10.100", 21, "username", "password", "/data/files", "test.txt", "D:\\temp\\download");
System.out.println(result);
}
二、SFTP文件上传下载
2.1 准备工作
在开始前,需要引入以下的Maven依赖:
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
2.2 SFTP文件上传
下面是SFTP文件上传的主要代码:
public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filePath, String filename, InputStream inputStream) {
Session session = null;
Channel channel = null;
boolean result = false;
try {
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(username, host, port); // 根据用户名、主机ip和端口号获取session对象
session.setPassword(password); // 设置密码
session.setConfig("StrictHostKeyChecking", "no");
session.connect(); // 通过Session建立连接
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
try {
sftp.cd(basePath);
} catch (SftpException e) {
sftp.mkdir(basePath);
sftp.cd(basePath);
}
sftp.put(inputStream, filename); // 上传文件
inputStream.close();
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.disconnect();
}
if (channel != null) {
channel.disconnect();
}
}
return result;
}
示例3:上传本地文件到SFTP服务器
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(new File("D:\\temp\\test.txt"));
boolean result = uploadFile("10.10.10.100", 22, "username", "password", "/data/files", "test", "test.txt", fis);
System.out.println(result);
}
2.3 SFTP文件下载
下面是SFTP文件下载的主要代码:
public static boolean downloadFile(String host, int port, String username, String password, String remotePath, String fileName, String localPath) {
Session session = null;
Channel channel = null;
boolean result = false;
try {
JSch jsch = new JSch(); // 创建JSch对象
session = jsch.getSession(username, host, port); // 根据用户名、主机ip和端口号获取session对象
session.setPassword(password); // 设置密码
session.setConfig("StrictHostKeyChecking", "no");
session.connect(); // 通过Session建立连接
channel = session.openChannel("sftp"); // 打开SFTP通道
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
sftp.cd(remotePath);
sftp.get(fileName, localPath); // 下载文件
result = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (session != null) {
session.disconnect();
}
if (channel != null) {
channel.disconnect();
}
}
return result;
}
示例4:从SFTP服务器下载文件到本地
public static void main(String[] args) {
boolean result = downloadFile("10.10.10.100", 22, "username", "password", "/data/files/test", "test.txt", "D:\\temp\\download");
System.out.println(result);
}
以上就是Java8实现FTP及SFTP文件上传下载的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java8实现FTP及SFTP文件上传下载 - Python技术站