1. 整体描述
接入ftp服务器,在springboot上实现起来也不算复杂,本文主要讲下如何在springboot下接入ftp服务上传文件,并对出现的问题做一些记录,ftp服务的参数配置等
2. 具体实现
2.1 引入pom
在springboot的配置文件引入:
<dependency>
<groupid>commons-net</groupid>
<artifactid>commons-net</artifactid>
<version>3.8.0</version>
</dependency>
apache的包,其中有ftp相关的类
2.2 创建ftps连接
其中使用ftpclient创建的是ftp连接,ftpsclient创建的是ftps连接,我这里创建了ftps连接
/**
* 打开ftp连接
*
* @param hostname hostname
* @param port port
* @param username username
* @param password password
*/
public static ftpsclient openftp(string hostname, int port, string username, string password) {
// 显示方式传false,隐式方式传true
ftpsclient ftpsclient = new ftpsclient(true);
ftpsclient.setcontrolencoding("utf-8");
try {
system.out.println("ftpsclient connect");
ftpsclient.connect(hostname, port);
boolean flag = ftpsclient.login(username, password);
// 切换到被动模式
ftpsclient.setcontrolencoding("utf-8");
ftpsclient.setfiletype(ftpclient.binary_file_type);
ftpsclient.enterlocalpassivemode();
ftpsclient.setfiletransfermode(ftp.stream_transfer_mode);
ftpsclient.execprot("p");
ftpsclient.setauthvalue("tls");
if (!flag) {
system.out.println("login failed");
return null;
}
string path = ftpsclient.printworkingdirectory();
system.out.println("path:" + path);
} catch (exception e) {
e.printstacktrace();
}
return ftpsclient;
}
2.3 上传文件
/**
* 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
*
* @param filepath 上传文件本地路径
* @param ftppath 上传文件ftp路径
* @param ftpsclient ftp客户端
*/
public static void uploadfiletoftp(string filepath, string ftppath, ftpsclient ftpsclient) {
try {
file file = new file(filepath);
boolean changeflag = ftpsclient.changeworkingdirectory(ftppath);
system.out.println("changeflag:" + changeflag);
if (!changeflag) {
boolean createflag = createdirectory(ftppath, ftpsclient);
system.out.println("createflag:" + createflag);
}
string uploadpath = ftpsclient.printworkingdirectory();
system.out.println("uploadpath:" + uploadpath);
ftpsclient.setfiletype(ftpclient.binary_file_type);
boolean uploadflag = ftpsclient.storeuniquefile(file.getname(), new fileinputstream(file));
system.out.println("uploadflag:" + uploadflag);
system.out.println("uploadflag:" + ftpsclient.getreplystring());
} catch (exception e) {
e.printstacktrace();
}
}
2.4 关闭连接
/**
* 关闭ftp连接
*
* @param ftpsclient ftp客户端
*/
public static void closeftp(ftpsclient ftpsclient) {
try {
ftpsclient.logout();
ftpsclient.disconnect();
} catch (exception e) {
e.printstacktrace();
}
}
2.5 创建ftp目录
/**
* 创建多层目录文件,如果有ftp服务器已存在该文件,则不创建,如果无,则创建
*
* @param remote 目录
* @param ftpsclient ftp客户端
*/
public static boolean createdirectory(string remote, ftpsclient ftpsclient) {
string directory = remote;
try {
if (!remote.endswith("/")) {
directory = directory + "/";
}
// 如果远程目录不存在,则递归创建远程服务器目录
if (!directory.equals("/") && !ftpsclient.changeworkingdirectory(directory)) {
int start = 0;
int end = 0;
if (directory.startswith("/")) {
start = 1;
} else {
start = 0;
}
end = directory.indexof("/", start);
string paths = "", path = "";
while (true) {
string subdirectory = remote.substring(start, end);
path = path + "/" + subdirectory;
// 目录不存在就创建
if (!ftpsclient.changeworkingdirectory(subdirectory)) {
if (ftpsclient.makedirectory(subdirectory)) {
ftpsclient.changeworkingdirectory(subdirectory);
}
}
paths = paths + "/" + subdirectory;
start = end + 1;
end = directory.indexof("/", start);
// 检查所有目录是否创建完毕
if (end <= start) {
break;
}
}
}
return true;
} catch (exception e) {
e.printstacktrace();
}
return false;
}
2.6 main方法
public static void main(string[] args) {
// 打开ftp连接
ftpsclient ftpsclient = openftp("192.168.1.100", 10012, "test1", "123456");
if (null == ftpsclient) {
return;
}
// 上传文件
uploadfiletoftp("c:\\users\\10187\\desktop\\ftp.txt", "/test/picture", ftpsclient);
// 下载文件
downloadfilefromftp(ftpsclient);
// 关闭ftp连接
closeftp(ftpsclient);
}
2.7 运行结果

log显示上传成功,去ftp对应目录也能看到上传的文件。
3. ftp服务器配置
服务器相关配置也需要修改一下,要不登录上传等会报错。ftp的配置文件一般在/etc/vsftpd下面,有个vsftpd.conf的文件
3.1 修改require_ssl_reuse参数
如果上传文件的时候报错:522 ssl connection failed; session reuse required: see require_ssl_reuse option in vsftpd.conf man page。
需要修改这个参数,默认是yes,需要改成no
3.2 设置ftps连接
第一步:创建私钥、证书
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/vsftpd/vsftpd_ssl_key.pem -out /etc/vsftpd/vsftpd_ssl_cert.pem
第二步:添加以下配置到配置文件
ssl_enable=yes ssl_tlsv1=yes ssl_sslv2=yes ssl_sslv3=yes allow_anon_ssl=no force_local_data_ssl=yes force_local_logins_ssl=yes rsa_cert_file=/etc/vsftpd/vsftpd_ssl_cert.pem rsa_private_key_file=/etc/vsftpd/vsftpd_ssl_key.pem require_ssl_reuse=no
第三步:重启ftp服务
systemctl restart vsftpd
3.3 设置ftps连接为隐式连接
ftps支持显示连接和隐式连接两种,如果需要隐式连接,修改配置文件。添加如下配置:
implicit_ssl=yes
然后重启ftp服务即可。
4. 总结
以上就是配置和连接ftp/ftps的基本操作,当然还有一些复杂的操作和配置,就自己探索吧。
到此这篇关于springboot接入ftp/ftps并上传文件和配置的代码指南的文章就介绍到这了,更多相关springboot接入ftp/ftps并上传和配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论