一、springboot整合ftp使用
1、引入依赖
这里引用 apache commons-net依赖,用于ftp客户端操作。
<dependency>
<groupid>commons-net</groupid>
<artifactid>commons-net</artifactid>
<version>3.11.0</version>
</dependency>2、ftp配置信息类
(1)配置文件添加 ftp配置信息。
# 文件上传相关
file:
# ftp文件相关
ftp:
host: 192.168.xxx.xxx
port: 21
username: zhangsan
password: xxxxxx
basepath: /ftpv
filepathprefix: http://192.168.xxx.xxx:9090/viewfile(2)ftp自定义配置信息类
/**
* ftp自定义配置信息类
*/
@data
@configuration
public class ftpproperties {
/**
* ftp服务器的地址
*/
@value("${file.ftp.host}")
private string ftphost;
/**
* ftp服务器的端口号(连接端口号)
*/
@value("${file.ftp.port}")
private int ftpport;
/**
* ftp的用户名
*/
@value("${file.ftp.username}")
private string ftpusername;
/**
* ftp的密码
*/
@value("${file.ftp.password}")
private string ftppassword;
/**
* ftp用户上传的根目录
*/
@value("${file.ftp.basepath}")
private string ftpbasepath;
/**
* http访问文件的路径前缀,配合nginx静态资源访问
*/
@value("${file.ftp.filepathprefix}")
private string ftpfilepathprefix;
}3、ftp服务工具类
/**
* ftp服务工具类
*/
@slf4j
public class ftputils {
/**
* 获取 ftpclient对象
*
* @param ftphost ftp主机服务器
* @param ftpport ftp端口 默认为21
* @param ftpusername ftp 登录密码
* @param ftppassword ftp登录密码
* @return ftpclient对象
*/
private static ftpclient getftpclient(string ftphost, int ftpport, string ftpusername, string ftppassword) {
/**
* 创建 ftpclient对象(对于连接ftp服务器,以及上传和上传都必须要用到一个对象)
*/
try {
ftpclient ftpclient = new ftpclient();
/**
* 连接 ftp服务
*/
// 设置编码
ftpclient.setcontrolencoding("utf-8");
// 设置连接超时时间(单位:毫秒)
ftpclient.setconnecttimeout(10 * 1000);
// 连接
ftpclient.connect(ftphost, ftpport);
// 登录
ftpclient.login(ftpusername, ftppassword);
/**
* ftpclient.getreplycode():接受状态码(如果成功,返回230,如果失败返回503)
* ftpreply.ispositivecompletion():如果连接成功返回true,否则返回false
*/
if (!ftpreply.ispositivecompletion(ftpclient.getreplycode())) {
log.error("未连接到ftp服务,用户名或密码错误");
// 连接失败,断开连接
ftpclient.disconnect();
return null;
} else {
log.info("连接到ftp服务成功");
// 设置二进制方式传输文件
ftpclient.setfiletype(ftpclient.binary_file_type);
// 设置被动工作模式,文件传输端口设置,否则文件上传不成功,也不报错
ftpclient.enterlocalpassivemode();
}
return ftpclient;
} catch (socketexception e) {
e.printstacktrace();
log.error("ftp的ip地址错误,请正确配置。");
} catch (ioexception e) {
e.printstacktrace();
log.error("ftp的端口错误,请正确配置。");
} catch (exception e) {
e.printstacktrace();
log.error("获取ftp客户端异常");
}
return null;
}
/**
* 断开 ftpclient对象
*/
private static void closeconnect(ftpclient ftpclient) {
try {
if (ftpclient != null && ftpclient.isconnected()) {
ftpclient.logout();
// 断开ftp的连接
ftpclient.disconnect();
log.info("关闭ftp客户端成功");
}
} catch (exception e) {
e.printstacktrace();
log.error("关闭ftp客户端异常");
}
}
/**
* 创建文件夹
*
* @param ftphost ftp主机服务器
* @param ftpport ftp端口 默认为21
* @param ftpusername ftp 登录密码
* @param ftppassword ftp登录密码
* @param ftpbasepath ftp用户上传的根目录
* @param dirpath 需要创建的文件夹,多层使用/隔开
* @return
*/
public static boolean createdirectory(string ftphost, int ftpport, string ftpusername, string ftppassword, string ftpbasepath, string dirpath) {
ftpclient ftpclient = ftputils.getftpclient(ftphost, ftpport, ftpusername, ftppassword);
try {
/**
* 切换到ftp的服务器路径。
* ftp服务为ftp虚拟用户默认了根目录,所以我们可以切换也可以不切换,结果是一样的,都会到用户的根目录下。推荐显示指定。
* ftp服务会判断文件夹已存在,不会创建,不存在,则会创建。
*/
ftpclient.changeworkingdirectory(ftpbasepath);
if (stringutils.isblank(dirpath)) {
return false;
}
string[] dirpatharr = dirpath.split("/");
for (string dir : dirpatharr) {
if (stringutils.isnotblank(dir)) {
ftpclient.makedirectory(dir);
// 切换到ftp的创建目录
ftpclient.changeworkingdirectory(dir);
}
}
return true;
} catch (ioexception e) {
e.printstacktrace();
log.error("创建文件夹异常");
} finally {
closeconnect(ftpclient);
}
return false;
}
/**
* 查询指定路径下的所有文件的文件名
*
* @param ftphost ftp主机服务器
* @param ftpport ftp端口 默认为21
* @param ftpusername ftp 登录密码
* @param ftppassword ftp登录密码
* @param dirpath 查询指定路径
* @return
*/
public static list<string> listfilename(string ftphost, int ftpport, string ftpusername, string ftppassword, string dirpath) {
if (stringutils.isblank(dirpath)) {
return null;
}
ftpclient ftpclient = ftputils.getftpclient(ftphost, ftpport, ftpusername, ftppassword);
// 获得指定目录下所有文件名
ftpfile[] ftpfiles = null;
try {
//ftpclient.enterlocalpassivemode(); // 列出路径下的所有文件的文件名
ftpfiles = ftpclient.listfiles(dirpath);
} catch (ioexception e) {
e.printstacktrace();
log.info("关闭ftp客户端成功");
return null;
} finally {
closeconnect(ftpclient);
}
list<string> filenamelist = new linkedlist<string>();
for (int i = 0; ftpfiles != null && i < ftpfiles.length; i++) {
ftpfile file = ftpfiles[i];
if (file.isfile()) {
filenamelist.add(file.getname());
}
}
return filenamelist;
}
/**
* 上传文件到ftp服务
*
* @param ftphost ftp主机服务器
* @param ftpport ftp端口 默认为21
* @param ftpusername ftp 登录密码
* @param ftppassword ftp登录密码
* @param ftpbasepath ftp用户上传的根目录
* @param filedirpath 上传的文件存储目录
* @param filename 上传的文件名
* @param is 上传的文件输入流
*/
public static boolean uploadfiletoftp(string ftphost, int ftpport, string ftpusername, string ftppassword, string ftpbasepath, string filedirpath, string filename, inputstream is) {
ftpclient ftpclient = ftputils.getftpclient(ftphost, ftpport, ftpusername, ftppassword);
boolean result = false;
try {
// 创建文件存储目录
ftputils.createdirectory(ftphost, ftpport, ftpusername, ftppassword, ftpbasepath, filedirpath);
// 切换到ftp的文件目录,即文件上传目录
ftpclient.changeworkingdirectory(filedirpath);
ftpclient.setcontrolencoding("utf-8");
ftpclient.setbuffersize(1024 * 10);
// 设置文件类型为二进制方式传输文件
ftpclient.setfiletype(ftpclient.binary_file_type);
ftpclient.enterlocalpassivemode();
ftpclient.setdefaulttimeout(18000);
ftpclient.setconnecttimeout(6000);
ftpclient.setsotimeout(6000);
result = ftpclient.storefile(filename, is);
} catch (ioexception e) {
log.error("上传文件到ftp服务失败:{}", e);
} finally {
closeconnect(ftpclient);
}
return result;
}
/**
* 从ftp中获取文件的输入流
*
* @param ftphost ftp主机服务器
* @param ftpport ftp端口 默认为21
* @param ftpusername ftp 登录密码
* @param ftppassword ftp登录密码
* @param ftpfilepath ftp文件路径,根目录开始
* @return
*/
public static inputstream getinputstreamofftpfile(string ftphost, int ftpport, string ftpusername, string ftppassword, string ftpfilepath) {
ftpclient ftpclient = ftputils.getftpclient(ftphost, ftpport, ftpusername, ftppassword);
inputstream is = null;
try {
is = ftpclient.retrievefilestream(ftpfilepath);
} catch (ioexception e) {
e.printstacktrace();
log.error("获取文件输入流异常");
} finally {
closeconnect(ftpclient);
}
return is;
}
/**
* 删除ftp文件
*
* @param ftphost ftp主机服务器
* @param ftpport ftp端口 默认为21
* @param ftpusername ftp 登录密码
* @param ftppassword ftp登录密码
* @param ftpfilepath ftp文件路径,根目录开始
* @return
*/
public static boolean deleteftpfile(string ftphost, int ftpport, string ftpusername, string ftppassword, string ftpfilepath) {
ftpclient ftpclient = ftputils.getftpclient(ftphost, ftpport, ftpusername, ftppassword);
boolean result = false;
try {
result = ftpclient.deletefile(ftpfilepath);
} catch (ioexception e) {
log.error("删除ftp文件失败:{}", e);
} finally {
closeconnect(ftpclient);
}
return result;
}
}4、ftp工具类测试
在项目,通常我们提供一个 ftpservice实现类来封装 ftp服务工具类。
这里进行 ftp工具类测试即可。
public static void main(string[] args) throws exception {
ftpproperties ftpproperties = getftpproperties();
获取 ftpclient对象
//ftpclient ftpclient = ftputils.getftpclient(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword());
断开 ftpclient对象
//ftputils.closeconnect(ftpclient);
// 创建文件夹
//string dirpath = "dev_dir1/d11/d12";
//ftputils.createdirectory(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword(), ftpproperties.getftpbasepath(), dirpath);
// 查询指定路径下的所有文件的文件名
//string dirpath = "/dev_dir1";
//list<string> filenamelist = ftputils.listfilename(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword(), dirpath);
//for (string filename : optional.ofnullable(filenamelist).orelse(new arraylist<>())) {
// system.out.println(filename);
//}
// 上传文件到ftp服务
//string filedirpath = "dev_dir1";
//string filename = "dev_uploadfile001.jpg";
//inputstream is = new fileinputstream("d:\\tempfiles\\598a80e12f9ad-中文.jpg");
//ftputils.uploadfiletoftp(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword(), ftpproperties.getftpbasepath(), filedirpath, filename, is);
// 从ftp中获取文件的输入流
//string ftpfilepath = "dev_dir1/dev_uploadfile001.jpg";
//inputstream inputstreamofftpfile = ftputils.getinputstreamofftpfile(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword(), ftpfilepath);
//fileutils.copyinputstreamtofile(inputstreamofftpfile, new file("d:\\tempfiles\\dev_uploadfile001.jpg"));
//string ftpfilepath = "dev_dir1/zs_111.txt";
//inputstream inputstreamofftpfile = ftputils.getinputstreamofftpfile(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword(), ftpfilepath);
//fileutils.copyinputstreamtofile(inputstreamofftpfile, new file("d:\\tempfiles\\zs_111.txt"));
// 删除ftp文件
string ftpfilepath = "dev_dir1/dev_uploadfile002.jpg";
boolean result = ftputils.deleteftpfile(ftpproperties.getftphost(), ftpproperties.getftpport(), ftpproperties.getftpusername(), ftpproperties.getftppassword(), ftpfilepath);
log.info("删除ftp文件结果,result={}", result);
}

到此这篇关于springboot整合ftp使用的文章就介绍到这了,更多相关springboot整合ftp内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论