springBoot下的ftp下载
springboot-Environment
ftp登录、退出
打包下载
本实例将创建一个ftp打包文件的工具类
Environment类获取配置信息
springboot的Environment类,可以获取到所有的配置资源。如果我们用到ftp,就会有一些登录ftp的相关ip、port、username、password,按照程序员的尿性,肯定不能直接写在类里。
private FTPClient ftpClient;
private String strIp; //ftp ip
private int intPort; //ftp port
private String user; //ftp username
private String password; //ftp password
private Environment environment;
public FtpUtils(Environment environment){
this.environment = environment;
this.strIp = environment.getProperty("配置文件里咋写的你咋写");
String strPort = environment.getProperty("配置文件里咋写的你咋写");
if(!strPort.trim().equals("")){
this.intPort = Integer.valueOf(strPort);
}
this.user = environment.getProperty("配置文件里咋写的你咋写");
this.password = environment.getProperty("配置文件里咋写的你咋写");
this.ftpClient = new FTPClient();
}
FTPClient是啥玩意?
如果你没有commons-net包,肯定是不行的,这包咋来的就先不说了。
具体咋登陆的ftp服务器,其实FTPClient都写好了,人家就是干这个事的。我们只需要跟它提供登陆需要的信息,就比如我们用filezilla登陆时要填的信息
ftp登录、退出登录
登陆
/**
* 登录ftp服务器
* @return
*/
public boolean ftpLogin(){
boolean isOk = false;
// 这个编码的设定因情况而定,这里只告诉大家,能设置编码
ftpClient.setControlEncoding("GBK");
try {
// 有没有端口都要登上去的意思
if(intPort>0){
ftpClient.connect(this.strIp, this.intPort);
}else{
ftpClient.connect(this.strIp);
}
isOk = this.ftpClient.login(this.user, this.password);
this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
this.ftpClient.setBufferSize(1024 * 2);
this.ftpClient.setDataTimeout(30 * 1000);
} catch (IOException e) {
e.printStackTrace();
}
return isOk;
}
退出登录
/**
* 退出ftp服务器
*/
public void ftpLogout(){
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
this.ftpClient.logout();// 退出FTP服务器
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
this.ftpClient.disconnect();// 关闭FTP服务器的连接
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
哎,下载个文件还登录这个,退出个那个的,但是没办法,这又不是本地下载。用完就干掉它。
文件下载
public void downloadFile(String remoteFileName ,ZipOutputStream zipOutputStream){
try {
zipOutputStream.putNextEntry(new ZipEntry(remoteFileName));
this.ftpClient.enterLocalPassiveMode();
boolean isRetrieOk = this.ftpClient.retrieveFile(remoteFileName, zipOutputStream);
if(!isRetrieOk){
return;
}
zipOutputStream.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
remoteFileName 文件绝对路径+文件名
zipOutputStream 打包就是用它,具体从哪来的?下面是我调用的示例
@RequestMapping("/downloadFile")
public void downloadFile(HttpServletResponse res){
// 刚才的一系列代码都是FtpUtils中的
FtpUtils ftpUtils = new FtpUtils(environment);
boolean login = ftpUtils.ftpLogin(); // 登录
res.setContentType("application/pdf");
// 这里重新编码以免中文名称的压缩包出不来
String downloadFileName = new String("你自己命名的压缩包.zip").getBytes("GBK"), "ISO-8859-1");
res.setHeader("content-Disposition", "attachment;filename="+downloadFileName);
ZipOutputStream zipOutputStream = null;
try {
zipOutputStream = new ZipOutputStream(res.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 这里只下载了一个文件,你也可以通过循环或其他形式,多次调用,只要在压缩流关闭之前都会压缩到一个包内,而且保留文件路径
ftpUtils.downloadFile("你的文件",zipOutputStream);
try {
zipOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ftpUtils.ftpLogout();// 退出
}
以上某些部分为手打,可能有不严谨的地方,请谅解