您的位置:首页 > 运维架构 > Apache

[Java] 使用 Apache的 Commons-net库 实现FTP操作

2016-03-18 21:48 513 查看
因为最近工作中需要用到FTP操作,而手上又没有现成的FTP代码。就去网上找了一下,发现大家都使用Apache的 Commons-net库中的FTPClient。

但是,感觉用起来不太方便。又在网上找到了很多封装过的。觉得也不是很好用。于是就自己写了一个。网上大多是例子都是直接对文件进行操作,而我更需要的是读到内存,或者从内存上写。并且有很多实用单例模式,但是我觉得如果调用比较多的话,可能会出现问题。

package com.best.oasis.util.helper;

/**
* 封装了一些FTP操作
* Created by bl05973 on 2016/3/11.
*/

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPCmd;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;

public class FTPUtil {
private static Logger logger = Logger.getLogger(FTPUtil.class);

private static FTPClient getConnection() {
FTPClient client = new FTPClient();
client.setControlEncoding("UTF-8");
client.setDataTimeout(30000);
client.setDefaultTimeout(30000);
return client;
}

public static FTPClient getConnection(String host) throws IOException {
FTPClient client = getConnection();
client.connect(host);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("connect error");
}
return client;
}
public static FTPClient getConnection(String host, int port) throws IOException {
FTPClient client = getConnection();
client.connect(host, port);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("connect error");
}
return client;
}

public static FTPClient getConnection(String host, String username, String password) throws
IOException {
FTPClient client= getConnection(host);
if (StringUtil.isNotBlank(username)) {
client.login(username, password);
}
//if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
//    throw new IOException("login error");
//}
return client;
}

public static FTPClient getConnection(String host, int port, String username, String password)
throws IOException {
FTPClient client = getConnection(host, port);
if (StringUtil.isNotBlank(username)) {
client.login(username, password);
}
//if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
//    throw new IOException("login error");
//}
return client;
}

/**
* 移动文件(若目标文件存在则不移动,并返回false)
*/
public static boolean moveFile(String curFileName, String targetFileName, FTPClient client)
throws IOException {
int reply;
reply = client.sendCommand(FTPCmd.RNFR, curFileName);
if (FTPReply.isNegativePermanent(reply)) {
//logger.error("FTP move file error. code:" + reply);
System.out.println("FTP move file error. code:" + reply);
return false;
}
reply = client.sendCommand(FTPCmd.RNTO, targetFileName);
if (FTPReply.isNegativePermanent(reply)) {
//logger.error("FTP move file error. code:" + reply);
System.out.println("FTP move file error. code:" + reply);
return false;
}
return true;
}

/**
* 读取文件列表
*/
public static List<String> getFileNameList(FTPClient client) throws IOException {
FTPFile[] files = client.listFiles();
List<String> fileNameList = new ArrayList<>();
for (FTPFile file : files) {
if (file.isFile()) {
fileNameList.add(file.getName());
}
}
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("get file name list error");
}
return fileNameList;
}

/**
* 读文件
*/
public static String readFile(String path, FTPClient client) throws IOException {
client.setFileType(FTP.EBCDIC_FILE_TYPE);
InputStream is = client.retrieveFileStream(path);
if (is == null) {
return null;
}
BufferedReader bf = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuilder sb = new StringBuilder();
String str;
while ((str = bf.readLine()) != null) {
sb.append(str).append("\n");
}
bf.close();
client.completePendingCommand();
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("Remote file net closed success");
}
return sb.toString();
}

@Deprecated
static boolean downFile(String remotePath, String localPath, FTPClient client)
throws IOException {
FileOutputStream fos = new FileOutputStream(localPath);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
client.retrieveFile(remotePath, fos);
client.completePendingCommand();
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
throw new IOException("Remote file net closed success");
}
return false;
}

/**
* 写文件
*/
public static boolean storeAsFile(String context, String remotePath, FTPClient client)
throws IOException {
OutputStream out = client.storeFileStream(remotePath);
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.write(context);
writer.flush();
writer.close();
return true;
}

public static void close(FTPClient client) {
try {
if (client != null) {
client.disconnect();
}
} catch (IOException e) {

}
}
}


FTPUtil
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: