Python 官方文档:入门教程 => 点击学习
sftp和ftp两种方式区别,还不清楚的,请自行百度查询,此处不多赘述。完整代码地址在结尾!! 第一步,导入Maven依赖 <!-- FTP依赖包 --> <
sftp和ftp两种方式区别,还不清楚的,请自行百度查询,此处不多赘述。完整代码地址在结尾!!
<!-- FTP依赖包 -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- SFTP依赖包 -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Vector;
@Slf4j
public class SftpUtils {
public static void main(String[] args) throws Exception {
log.info("测试开始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// 1
File file = new File("E:\\2.xlsx");
InputStream inputStream = new FileInputStream(file);
SftpUtils.uploadFile("", "", "", 22, "/usr/local",
"/testfile/", "test.xlsx", null, inputStream);
// 2
SftpUtils.downloadFile("", "", "", 22,null,
"/usr/local/testfile/", "test.csv","/Users/ao/Desktop/test.csv");
// 3
SftpUtils.deleteFile("", "", "", 22,null,
"/usr/local/testfile/", "test.xlsx");
// 4
Vector<?> fileList = SftpUtils.getFileList("", "", "",
22, null,"/usr/local/testfile/");
log.info(fileList.toString());
log.info("测试结束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
public static void uploadFile(String userName, String passWord, String host, int port, String basePath,
String filePath, String filename, String privateKey, InputStream input) throws Exception {
Session session = null;
ChannelSftp sftp = null;
// 连接sftp服务器
try {
JSch jsch = new JSch();
if (privateKey != null) {
// 设置私钥
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHosTKEyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
// 将输入流的数据上传到sftp作为文件
try {
sftp.cd(basePath);
sftp.cd(filePath);
} catch (SftpException e) {
//目录不存在,则创建文件夹
String [] dirs=filePath.split("/");
String tempPath=basePath;
for(String dir:dirs){
if(null== dir || "".equals(dir)){
continue;
}
tempPath+="/"+dir;
try{
sftp.cd(tempPath);
}catch(SftpException ex){
sftp.mkdir(tempPath);
sftp.cd(tempPath);
}
}
}
//上传文件
sftp.put(input, filename);
//关闭连接 server
if (sftp != null) {
if (sftp.isConnected()) {
sftp.disconnect();
}
}
//关闭连接 server
if (session != null) {
if (session.isConnected()) {
session.disconnect();
}
}
}
public static void downloadFile(String userName, String password, String host, int port, String privateKey, String directory,
String downloadFile, String saveFile) throws Exception{
Session session = null;
ChannelSftp sftp = null;
// 连接sftp服务器
try {
JSch jsch = new JSch();
if (privateKey != null) {
// 设置私钥
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
}
public static byte[] downloadFile(String userName, String password, String host, int port, String privateKey,
String directory, String downloadFile) throws Exception{
Session session = null;
ChannelSftp sftp = null;
// 连接sftp服务器
try {
JSch jsch = new JSch();
if (privateKey != null) {
// 设置私钥
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
InputStream is = sftp.get(downloadFile);
byte[] fileData = IOUtils.toByteArray(is);
return fileData;
}
public static void deleteFile(String userName, String password, String host, int port, String privateKey,
String directory, String deleteFile) throws Exception{
Session session = null;
ChannelSftp sftp = null;
// 连接sftp服务器
try {
JSch jsch = new JSch();
if (privateKey != null) {
// 设置私钥
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
sftp.cd(directory);
sftp.rm(deleteFile);
}
public static Vector<?> getFileList(String userName, String password, String host, int port, String privateKey,
String directory) throws Exception {
Session session = null;
ChannelSftp sftp = null;
// 连接sftp服务器
try {
JSch jsch = new JSch();
if (privateKey != null) {
// 设置私钥
jsch.addIdentity(privateKey);
}
session = jsch.getSession(userName, host, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
return sftp.ls(directory);
}
}
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.*;
@Slf4j
public class FtpUtils {
public static void main(String[] args) throws Exception {
log.info("测试开始!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
// 1
File file = new File("E:\\2.xlsx");
InputStream inputStream = new FileInputStream(file);
FtpUtils.uploadFile("", 21, "", "", "/usr/local",
"/testfile/", "test.xlsx", inputStream);
// 2
FtpUtils.downloadFile("", 21, "", "","/usr/local/testfile/",
"test.csv", "/Users/ao/Desktop/test.csv");
log.info("测试结束!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
public static boolean uploadFile(String host, int port, String userName, String password, String basePath,
String filePath, String filename, InputStream input) throws Exception{
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
// 连接FTP服务器
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
// 登录
ftp.login(userName, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
//切换到上传目录
if (!ftp.changeWorkingDirectory(basePath+filePath)) {
//如果目录不存在创建目录
String[] dirs = filePath.split("/");
String tempPath = basePath;
for (String dir : dirs) {
if (null == dir || "".equals(dir)){
continue;
}
tempPath += "/" + dir;
if (!ftp.changeWorkingDirectory(tempPath)) {
if (!ftp.makeDirectory(tempPath)) {
return result;
} else {
ftp.changeWorkingDirectory(tempPath);
}
}
}
}
//设置上传文件的类型为二进制类型
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//上传文件
if (!ftp.storeFile(filename, input)) {
return result;
}
input.close();
ftp.loGout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
public static boolean downloadFile(String host, int port, String userName, String password, String remotePath,
String fileName, String localPath) throws Exception {
boolean result = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(host, port);
// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
// 登录
ftp.login(userName, password);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
// 转移到FTP服务器目录
ftp.changeWorkingDirectory(remotePath);
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
java.io.File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
result = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return result;
}
}
完整代码地址:https://GitHub.com/luoyusoft/java-demo
注:此工程包含多个包,FtpUtils代码均在com.luoyu.java.ftp包下
注:此工程包含多个包,SftpUtils代码均在com.luoyu.java.sftp包下
到此这篇关于Java使用Sftp和Ftp实现对文件的上传和下载的文章就介绍到这了,更多相关Java使用Sftp和Ftp文件上传和下载内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java使用Sftp和Ftp实现对文件的上传和下载
本文链接: https://www.lsjlt.com/news/122373.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0