Apache FTPClient.retrieveFileStream(字符串)returns 空
Apache FTPClient.retrieveFileStream(String) returns null
我正在使用 Java 11 和 SpringBoot。我正在尝试创建一个 FTP 客户端来从现有的 FTP 服务器读取文件。使用 Apache FTPClient.
问题:
InputStream is = ftp.retrieveFileStream(file.getName());
下面总是returns一个null
InputStream.
信息:
logger.info("File name: "+file.getName()+", type: "+file.getType()+", size: "+file.getSize()+".");
returns:
File name: cam_test, type: 1, size: 28.
问题:
可以看到有一个文件叫'cam_test',为什么这个不能转换成InputStream呢?
代码:
import org.apache.commons.net.ftp.FTPClient;
public void processFiles() {
FTPClient ftp = new FTPClient();
try {
ftp.connect(IP_ADDRESS);
int reply = ftp.getReplyCode();
logger.info("Connected to " + IP_ADDRESS + ". ReplyCode: '"+reply+"'.");
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
logger.error("FTP server ('"+IP_ADDRESS+"') refused connection.");
} else {
// transfer files
ftp.login(USERNAME, PASSWORD);
FTPFile[] files = ftp.listFiles(BASE_DIR);
logger.info("Connected to " + IP_ADDRESS + ". Got '"+files.length+"' files from working directory: "+ftp.printWorkingDirectory()+BASE_DIR+".");
for (FTPFile file : files) {
logger.info("File name: '"+file.getName()+"', type: '"+file.getType()+"', size: '"+file.getSize()+"', directory: '"+file.isDirectory()+"', file: '"+file.isFile()+"'.");
if (file.isFile() && 0 == file.getType()) {
InputStream is = ftp.retrieveFileStream(file.getName()); // <--- is always null
processFile(file, is);
if (is != null) {
is.close();
}
boolean completePendingCommand = ftp.completePendingCommand();
logger.info("completePendingCommand: "+completePendingCommand);
}
}
}
} catch(IOException e) {
logger.error(e.getMessage(), e);
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) {
// do nothing
}
}
}
}
您必须在每个命令后添加 completePendingCommand
。
在下面的一段代码中completePendingCommand被注释掉了。
for (FTPFile file : files) {
System.out.println(
"File name: " + file.getName() + ", type: " + file.getType() + ", size: " + file.getSize() + ".");
InputStream is = ftp.retrieveFileStream(file.getName());
System.out.println(is == null);
// ftp.completePendingCommand();
}
结果是:
Connected to ftp.dlptest.com. 220
File name: actorstoday.txt, type: 0,
size: 0.
false
File name: actorstomorrow.txt, type: 0, size: 0.
true
如您所见,第二个请求返回空 InputStream。
更改为:
for (FTPFile file : files) {
System.out.println(
"File name: " + file.getName() + ", type: " + file.getType() + ", size: " + file.getSize() + ".");
InputStream is = ftp.retrieveFileStream(file.getName());
System.out.println(is == null);
ftp.completePendingCommand();
}
结果是:
Connected to ftp.dlptest.com. 220
File name: actorstoday.txt, type: 0,
size: 0.
false
File name: actorstomorrow.txt, type: 0, size: 0.
false
在 for 循环的每次迭代中,您必须调用 completePendingCommand
。
我正在使用 Java 11 和 SpringBoot。我正在尝试创建一个 FTP 客户端来从现有的 FTP 服务器读取文件。使用 Apache FTPClient.
问题:
InputStream is = ftp.retrieveFileStream(file.getName());
下面总是returns一个null
InputStream.
信息:
logger.info("File name: "+file.getName()+", type: "+file.getType()+", size: "+file.getSize()+".");
returns:
File name: cam_test, type: 1, size: 28.
问题:
可以看到有一个文件叫'cam_test',为什么这个不能转换成InputStream呢?
代码:
import org.apache.commons.net.ftp.FTPClient;
public void processFiles() {
FTPClient ftp = new FTPClient();
try {
ftp.connect(IP_ADDRESS);
int reply = ftp.getReplyCode();
logger.info("Connected to " + IP_ADDRESS + ". ReplyCode: '"+reply+"'.");
if(!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
logger.error("FTP server ('"+IP_ADDRESS+"') refused connection.");
} else {
// transfer files
ftp.login(USERNAME, PASSWORD);
FTPFile[] files = ftp.listFiles(BASE_DIR);
logger.info("Connected to " + IP_ADDRESS + ". Got '"+files.length+"' files from working directory: "+ftp.printWorkingDirectory()+BASE_DIR+".");
for (FTPFile file : files) {
logger.info("File name: '"+file.getName()+"', type: '"+file.getType()+"', size: '"+file.getSize()+"', directory: '"+file.isDirectory()+"', file: '"+file.isFile()+"'.");
if (file.isFile() && 0 == file.getType()) {
InputStream is = ftp.retrieveFileStream(file.getName()); // <--- is always null
processFile(file, is);
if (is != null) {
is.close();
}
boolean completePendingCommand = ftp.completePendingCommand();
logger.info("completePendingCommand: "+completePendingCommand);
}
}
}
} catch(IOException e) {
logger.error(e.getMessage(), e);
} finally {
if(ftp.isConnected()) {
try {
ftp.disconnect();
} catch(IOException ioe) {
// do nothing
}
}
}
}
您必须在每个命令后添加 completePendingCommand
。
在下面的一段代码中completePendingCommand被注释掉了。
for (FTPFile file : files) {
System.out.println(
"File name: " + file.getName() + ", type: " + file.getType() + ", size: " + file.getSize() + ".");
InputStream is = ftp.retrieveFileStream(file.getName());
System.out.println(is == null);
// ftp.completePendingCommand();
}
结果是:
Connected to ftp.dlptest.com. 220
File name: actorstoday.txt, type: 0, size: 0. false
File name: actorstomorrow.txt, type: 0, size: 0. true
如您所见,第二个请求返回空 InputStream。
更改为:
for (FTPFile file : files) {
System.out.println(
"File name: " + file.getName() + ", type: " + file.getType() + ", size: " + file.getSize() + ".");
InputStream is = ftp.retrieveFileStream(file.getName());
System.out.println(is == null);
ftp.completePendingCommand();
}
结果是:
Connected to ftp.dlptest.com. 220
File name: actorstoday.txt, type: 0, size: 0. false
File name: actorstomorrow.txt, type: 0, size: 0. false
在 for 循环的每次迭代中,您必须调用 completePendingCommand
。