- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ftpClient.listFiles() 从 FTPClient 检索文件列表;它返回一个具有正确文件名和适当数量的数组。
我正在尝试从文件中读取内容,但只有 ftpClient.retrieveFileStream(fileName) 可用于此目的。它在读取第一个文件后以某种方式中断并返回 null。
有没有办法将FTPFile直接转换为String?
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());
}
}
最佳答案
3个提示
1)您可以使用retrieveFile
并使用 ByteArrayOutputStream 作为第二个参数。要从中获取字符串,只需“new String(baos.toByteArray(),[您使用的字符集]);”
2) 检查 ftpClient.completePendingCommand();
如果某些命令仍待处理
3)我曾经遇到过类似的问题,设置 ftp.enterLocalPassiveMode() 有所帮助
关于java - 如何从 org.apache.commons.net.ftp.FTPFile 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23833923/
我只是想知道是否有办法模拟 FTPFile 数组。我试图将一个伪造的 FTPFile[] 作为参数传递给我要测试的函数: protected void indexFolder(FTPClient, F
我正在从 FTP 服务器上的特定位置检索文件列表,然后我需要检查其中是否包含特定文件名。如果文件不存在,我想提供一个备用文件名来检查。 FTPFile[] ftpFiles = ftp.listFil
我喜欢按 date 对 FTPFile 列表进行排序,但不知道该怎么做。 我有以下代码: FTPFile[] files = ftpClient.listFiles(); for (FTPFile f
我需要从共享位置读取所有文件并返回文件映射。我使用 FTPClient 访问共享位置。使用 FTPClient 我能够将所有文件作为 FTPFile 检索。但我想将 FTPFile 转换为文件。请查看
我在没有 FTP 的情况下读取 ZipFiles 的常用方法如下: private void getLogFromZip(File logZip){ ZipFile zf = new ZipFil
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在使用 ftpClient.listFiles() 从 FTPClient 检索文件列表;它返回一个具有正确文件名和适当数量的数组。 我正在尝试从文件中读取内容,但只有 ftpClient.ret
我是一名优秀的程序员,十分优秀!