Java 如何将org.apache.commons.net.ftp.FTPFile转换为字符串?
我正在使用FTPClient.listFiles()从FTPClient检索文件列表;它返回一个具有正确文件名和适当数量的数组 我试图从文件中读取内容,但只有ftpClient.retrieveFileStream(文件名)可用于此目的。它在读取第一个文件后不知何故中断并返回null 有没有办法将FTPFile直接转换成字符串Java 如何将org.apache.commons.net.ftp.FTPFile转换为字符串?,java,apache-commons-net,Java,Apache Commons Net,我正在使用FTPClient.listFiles()从FTPClient检索文件列表;它返回一个具有正确文件名和适当数量的数组 我试图从文件中读取内容,但只有ftpClient.retrieveFileStream(文件名)可用于此目的。它在读取第一个文件后不知何故中断并返回null 有没有办法将FTPFile直接转换成字符串 ftp.connect(); ftp.changeWorkingDirectoryToFrom(); List&
ftp.connect();
ftp.changeWorkingDirectoryToFrom();
List<String> idsList = new ArrayList<String>();
List<String> names = ftp.listFileNames();
for (String fileName : names) {
String content = fromFTPFileToString(fileName);
Matcher matcher = FILES_PATTERN.matcher(content);
String id = extractId(content);
if (matcher.find()) {
boolean duplicate = idsList.contains(id);
LOG.info("MATCHED: " + fileName);
if (!duplicate) {
ftp.moveFileFromTo(fileName);
idsList.add(id);
} else {
LOG.info("DUPLICATE: " + fileName);
duplicated++;
ftp.deleteFileOnFromFtp(fileName);
}
}
processed++;
}
ftp.disconnect();
private String fromFTPFileToString(String fileName) {
String content = "";
try {
InputStream is = ftp.readContentFromFTPFile(fileName);
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, ENCODING);
content = writer.toString();
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(writer);
} catch (Exception ex) {
LOG.error(ex);
}
return content;
}
void deleteFileOnFromFtp(String fileName) {
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString()));
deleteFile(fileName);
}
InputStream readContentFromFTPFile(String fileName) {
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString()));
InputStream inputStream = null;
try {
inputStream = ftpClient.retrieveFileStream(fileName);
} catch (IOException ex) {
LOG.error("Unable to extract content from file:" + O_Q + fileName + C_Q);
}
return inputStream;
}
void moveFileFromTo(String fileName) {
String from = FORWARD_SLASH + properties.getProperty(PropertiesType.FOLDER_FROM.toString()) + FORWARD_SLASH + fileName;
String to = FORWARD_SLASH + properties.getProperty(PropertiesType.FOLDER_TO.toString()) + FORWARD_SLASH + fileName;
try {
ftpClient.rename(from, to);
} catch (IOException ex) {
LOG.error("Unable to move file file from:" + O_Q + from + C_Q + " to: " + O_Q + to + C_Q);
throw new RuntimeException(ex.getCause());
}
}
ftp.connect();
ftp.changeWorkingDirectoryToFrom();
List idsList=new ArrayList();
列表名称=ftp.listFileNames();
for(字符串文件名:名称){
字符串内容=fromFTPFileToString(文件名);
Matcher Matcher=文件\u PATTERN.Matcher(内容);
字符串id=提取id(内容);
if(matcher.find()){
布尔值重复=idsList.contains(id);
LOG.info(“匹配:”+文件名);
如果(!重复){
ftp.moveFileFromTo(文件名);
idsList.add(id);
}否则{
LOG.info(“重复:”+文件名);
复制++;
ftp.deleteFileOnFromFtp(文件名);
}
}
处理++;
}
ftp.disconnect();
FtpFileToString(字符串文件名)中的私有字符串{
字符串内容=”;
试一试{
InputStream=ftp.readContentFromFTPFile(文件名);
StringWriter编写器=新的StringWriter();
IOUtils.copy(is、writer、ENCODING);
content=writer.toString();
IOUtils.安静地关闭(is);
(作家);
}捕获(例外情况除外){
日志错误(ex);
}
返回内容;
}
void deleteFileOnFromFtp(字符串文件名){
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString());
删除文件(文件名);
}
InputStream readContentFromFTPFile(字符串文件名){
changeWorkingDirectory(properties.getProperty(PropertiesType.FOLDER_FROM.toString());
InputStream InputStream=null;
试一试{
inputStream=ftpClient.retrieveFileStream(文件名);
}捕获(IOEX异常){
LOG.error(“无法从文件中提取内容:”+O_Q+fileName+C_Q);
}
返回输入流;
}
void moveFileFromTo(字符串文件名){
String from=FORWARD\u SLASH+properties.getProperty(PropertiesType.FOLDER\u from.toString())+FORWARD\u SLASH+fileName;
字符串to=正斜杠+属性.getProperty(PropertiesType.FOLDER\u to.toString())+正斜杠+文件名;
试一试{
ftpClient.rename(从,到);
}捕获(IOEX异常){
LOG.error(“无法将文件从“+O_Q+从+C_Q+”移动到“+O_Q+到+C_Q”);
抛出新的RuntimeException(例如getCause());
}
}
3提示
1)您可以使用retrieveFile
并使用ByteArrayOutputStream作为第二个参数。
要从中获取字符串,只需“newstring(baos.toByteArray(),[您使用的字符集])
2)检查ftpClient.completePendingCommand()强>
如果某些命令仍然挂起
3)我曾经遇到过一个类似的问题,设置ftp.enterLocalPassiveMode()有帮助你能分享你到目前为止的代码吗?当我只使用moveFileFromTo时,它工作正常,但当我试图获取流时,它在任何第二个文件上都返回null。如果你使用Java 7或更高版本,
IOUtils.Closequity
是无用的。你可以用这个来代替我。谢谢我假设inputstream有一个bug,而outputstream是正常的。