java spring ftp连接池_fd-cloud-ftp-starter

public class FtpUtils {

/**

* FTP的连接池

*/

public static FtpClientPool ftpClientPool;

/**

* FTPClient对象

*/

public static FTPClient ftpClient;

//@Autowired static方式可以采用setter方法

public FtpUtils(FtpClientPool ftpClientPool) {

FtpUtils.ftpClientPool= ftpClientPool;

}

//private static FtpUtils ftpUtils;

/* *//**

* 初始化设置

* @return

*//*

@PostConstruct

public boolean init() {

ftpUtils = this;

return true;

}*/

/**

* 获取连接对象

* @return

* @throws Exception

*/

public static FTPClient getFTPClient() throws Exception {

//初始化的时候从队列中取出一个连接

synchronized (ftpClientPool) {

ftpClient = ftpClientPool.borrowObject();

}

return ftpClient;

}

/**

* 当前命令执行完成命令完成

* @throws IOException

*/

public void complete() throws IOException {

ftpClient.completePendingCommand();

}

/**

* 当前线程任务处理完成,加入到队列的最后

* @return

*/

public void disconnect() throws Exception {

ftpClientPool.returnObject(ftpClient);

}

/**

* Description: 向FTP服务器上传文件

*

* @Version1.0

* @param remoteFile

* 上传到FTP服务器上的文件名

* @param input

* 本地文件流

* @return 成功返回true,否则返回false

*/

public static boolean uploadFile(String remoteFile, InputStream input) {

boolean result = false;

try {

getFTPClient();

ftpClient.enterLocalPassiveMode();

result = ftpClient.storeFile(remoteFile, input);

input.close();

ftpClient.disconnect();

} catch (Exception e) {

e.printStackTrace();

}

return result;

}

/**

* Description: 向FTP服务器上传文件

*

* @Version1.0

* @param remoteFile

* 上传到FTP服务器上的文件名

* @param localFile

* 本地文件

* @return 成功返回true,否则返回false

*/

public static boolean uploadFile(String remoteFile, String localFile){

FileInputStream input = null;

try {

input = new FileInputStream(new File(localFile));

} catch (FileNotFoundException e) {

e.printStackTrace();

}

return uploadFile(remoteFile, input);

}

/**

* 拷贝文件

* @param fromFile

* @param toFile

* @return

* @throws Exception

*/

public boolean copyFile(String fromFile, String toFile) throws Exception {

InputStream in=getFileInputStream(fromFile);

getFTPClient();

boolean flag = ftpClient.storeFile(toFile, in);

in.close();

return flag;

}

/**

* 获取文件输入流

* @param fileName

* @return

* @throws IOException

*/

public static InputStream getFileInputStream(String fileName) throws Exception {

ByteArrayOutputStream fos=new ByteArrayOutputStream();

getFTPClient();

ftpClient.retrieveFile(fileName, fos);

ByteArrayInputStream in=new ByteArrayInputStream(fos.toByteArray());

fos.close();

return in;

}

/**

* Description: 从FTP服务器下载文件

*

* @Version1.0

* @return

*/

public static boolean downFile(String remoteFile, String localFile){

boolean result = false;

try {

getFTPClient();

OutputStream os = new FileOutputStream(localFile);

ftpClient.retrieveFile(remoteFile, os);

ftpClient.logout();

ftpClient.disconnect();

result = true;

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

} catch (Exception e) {

e.printStackTrace();

}

}

return result;

}

/**

* 从ftp中获取文件流

* @param filePath

* @return

* @throws Exception

*/

public static InputStream getInputStream(String filePath) throws Exception {

getFTPClient();

InputStream inputStream = ftpClient.retrieveFileStream(filePath);

return inputStream;

}

/**

* ftp中文件重命名

* @param fromFile

* @param toFile

* @return

* @throws Exception

*/

public boolean rename(String fromFile,String toFile) throws Exception {

getFTPClient();

boolean result = ftpClient.rename(fromFile,toFile);

return result;

}

/**

* 获取ftp目录下的所有文件

* @param dir

* @return

*/

public FTPFile[] getFiles(String dir) throws Exception {

getFTPClient();

FTPFile[] files = new FTPFile[0];

try {

files = ftpClient.listFiles(dir);

}catch (Throwable thr){

thr.printStackTrace();

}

return files;

}

/**

* 获取ftp目录下的某种类型的文件

* @param dir

* @param filter

* @return

*/

public FTPFile[] getFiles(String dir, FTPFileFilter filter) throws Exception {

getFTPClient();

FTPFile[] files = new FTPFile[0];

try {

files = ftpClient.listFiles(dir, filter);

}catch (Throwable thr){

thr.printStackTrace();

}

return files;

}

/**

* 创建文件夹

* @param remoteDir

* @return 如果已经有这个文件夹返回false

*/

public boolean makeDirectory(String remoteDir) throws Exception {

getFTPClient();

boolean result = false;

try {

result = ftpClient.makeDirectory(remoteDir);

} catch (IOException e) {

e.printStackTrace();

}

return result;

}

public boolean mkdirs(String dir) throws Exception {

boolean result = false;

if (null == dir) {

return result;

}

getFTPClient();

ftpClient.changeWorkingDirectory("/");

StringTokenizer dirs = new StringTokenizer(dir, "/");

String temp = null;

while (dirs.hasMoreElements()) {

temp = dirs.nextElement().toString();

//创建目录

ftpClient.makeDirectory(temp);

//进入目录

ftpClient.changeWorkingDirectory(temp);

result = true;

}

ftpClient.changeWorkingDirectory("/");

return result;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值