JAVA之使用POOL2封装池化的FTPClient并且带有FTP上传删除文件操作

JAVA之使用commons-pool2连接池封装池化的FTPClient

欢迎各位小伙伴看到此文章,此项目是一个封装JAVA远程调用FTP文件服务器操作的工具,只需简单配置即可开箱直接使用!
此项目用org.apache.commons池化技术封装了FTPClient和FTP删除及上传操作。
将项目打包jar到本地,然后引入其jar包配置bean即可使用。
项目代码git下载地址 记得点赞收藏关注
项目代码github下载地址 记得点赞收藏关注

FtpUtli使用参数配置简介

1-FtpConfig参数
 *host: 				Ftp连接地址IP
 port: 				Ftp连接端口 		默认21
 *userName:			Ftp登录用户名称
 *password:			Ftp登录密码
 connectTimeOut: 	Ftp连接超时时间 	   默认5s					单位:ms
 controlEncoding:	Ftp字符编码		  	默认Utu-8
 bufferSize:		Ftp缓冲区大小	    默认1024 此参数根据你的带宽设置比例
 fileType:			Ftp传输文件类型      默认2=二进制
 dataTimeout:		Ftp数据传输超时时间   默认60s 				单位ms
 passiveMode:		是否启用ftp被动模式   默认true
 *basePath:			Ftp根路径 例子:/home/path ()  /home/path/ (×) 
 *必填
2-FtpPoolConfig

blockWhenExhausted:			连接耗尽时是否阻塞, false报异常,ture阻塞直到超时		  默认true
maxWaitMillis:				获取连接最大阻塞时间ms -1为无限等待					默认5s
maxTotal:					允许创建资源的最大数量  							  默认 16
maxIdle:					最大空闲资源数										 默认值 8
minIdle:					最小空闲资源数										 默认值 2
testOnBorrow:				获取连接时检测是否有效								  默认true
testOnReturn:				归还连接时检测是否有效								  默认false
testOnCreate:				创建连接时检测是否有效								  默认false
testWhileIdle:				是否检测空闲连接是否有效							  默认true
minEvictableIdleTimeMillis: 空闲连接超时时间									默认60M
softMinEvictableIdleTimeMillis:空闲连接超时时间									默认30M
timeBetweenEvictionRunsMillis:空闲对象回收(轮询间隔时间,单位毫秒)-1不启动回收 	  默认10M
numTestsPerEvictionRun:   设置为 小于0时,回收资源的个数为 (int)Math.ceil( 池中空闲资源个数 / Math.abs(numTestsPerEvictionRun) ); 
设置为 大于0时,回收资源的个数为 Math.min( numTestsPerEvictionRun,池中空闲的资源个数 );

3-配置要求
jdk			1.8+
pool2 		2.9+
commons-net 3.3+
4-SpringBoot配置示例
import com.lc.config.FtpConfig;
import com.lc.config.FtpPoolConfig;
import com.lc.ftp.FtpClientFactory;
import com.lc.ftp.FtpClientPool;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FtpAllConfig {

    @Bean
    public FtpConfig ftpConfig() {
        return new FtpConfig();
    }

    @Bean
    public FtpPoolConfig ftpPoolConfig() {
        return new FtpPoolConfig();
    }


    @Bean
    public FtpClientPool ftpClientPool(FtpConfig ftpConfig, FtpPoolConfig ftpPoolConfig) {
        FtpClientPool ftpClientPool=new FtpClientPool(new FtpClientFactory(ftpConfig),ftpPoolConfig,ftpConfig.getBasePath());
        return ftpClientPool;
    }
}

5-FtpUtli使用示例
import com.lc.ftp.FtpClientPool;
import com.lc.ftp.FtpClients;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@RestController
public class FtpController {

    @Resource
    private FtpClientPool ftpClientPool;

    /**
     * 上传图片
     * @return
     */
    @GetMapping("upload")
    public Boolean upload() {
        InputStream inputStream= null;
        try {
                inputStream = new FileInputStream("C:\\Users\\win10\\Pictures\\test.jpg");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        FtpClients ftpClients= null;
        try {
            ftpClients = ftpClientPool.getFtpClients();
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean success = ftpClients.upload(inputStream, "/t","tests.jpg");
        return success;
    }
    /**
     * 删除图片
     * @return
     */
    @GetMapping("delete")
    public Boolean delete() {
        FtpClients ftpClients= null;
        try {
            ftpClients = ftpClientPool.getFtpClients();
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean success = ftpClients.delete( "/t","tests.jpg");
        return success;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值