简体   繁体   English

Java的。 从FTP读取文件但不要整体下载

[英]Java. Read file from FTP but DON'T download it whole

I need to read CSV file header from FTP. 我需要从FTP读取CSV文件头。

As these files can be very huge, I don't need to download them. 由于这些文件非常庞大,我不需要下载它们。

Is there a way to read first line of CSV file from FTP and abort connection? 有没有办法从FTP读取第一行CSV文件并中止连接?

Just read only the first line, ignore the remnant and close the stream. 只读第一行,忽略残余并关闭流。 A smart FTP client won't buffer the entire stream in memory before providing anything for read. 在提供任何要读取的内容之前,智能FTP客户端不会将整个流缓冲在内存中。

Assuming you're using Apache Commons Net FTPClient : 假设您正在使用Apache Commons Net FTPClient

BufferedReader reader = null;
String firstLine = null;

try {
    InputStream stream = ftpClient.retrieveFileStream(ftpFile.getName());
    reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    firstLine = reader.readLine();
} finally {
    if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}

doYourThingWith(firstLine);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM