连续读取 ftp.exe 控制台输出,而不是逐行读取

Reading ftp.exe console output continually, not by lines

我有一个将执行 FTP 下载的批处理文件,如果只是单击它会在运行时显示所有输出,但是,它不会显示所有哈希值 (#### ) 直到 运行 在 Java 中以编程方式下载完成。有没有办法让我在下载过程中系统打印#?我假设程序正在等待完整的行进来,那么有没有办法把它分成几部分?

批处理文件:

echo user xxxxx>> ftpcmd.bat
echo xxxxx>> ftpcmd.bat
echo bin>> ftpcmd.bat
echo hash>> ftpcmd.bat
echo cd download>> ftpcmd.bat
echo get 01_MBytes.txt>> ftpcmd.bat
echo quit>> ftpcmd.bat
ftp -n -s:ftpcmd.bat xxx.xxx.xxx.xxx >> ftp_test.txt
del ftpcmd.bat

Java代码:

Process p = null;
InputStream processOutput;
BufferedReader reader = null;
String line = " ",output = " ";

p = Runtime.getRuntime().exec("cmd /c " + command);
processOutput = p.getInputStream();
reader = new BufferedReader(new InputStreamReader(processOutput));
while(line = reader.readLine()){
    System.out.println(line);
}
output += line + "\n";

执行期间的输出:

echo user xxxxx 1>>ftpcmd.bat 
echo xxxxx 1>>ftpcmd.bat 
echo bin 1>>ftpcmd.bat 
echo hash 1>>ftpcmd.bat 
echo cd download 1>>ftpcmd.bat 
echo get 01_MBytes.txt 1>>ftpcmd.bat 
echo quit 1>>ftpcmd.bat 
ftp -n -s:ftpcmd.bat xxx.xxx.xxx.xxx
Hash mark printing On  ftp: (2048 bytes/hash mark) .
user LTETester 
bin
hash
cd download
get 01_MBytes.txt

然后在下载完成后(再次,我试图看到它发生的情况):

#####################################################
#####################################################
#####################################################
#####################################################
#####################################################
quit

这与批处理文件无关。

流通常有一个 READ(numchar) 方法以及 readlinereadall。无论如何,基础知识和 Window 对象都可以。

您正在逐行阅读输出。所以很自然地 .readLine() returns 只有在 ftp.exe 输出整行之后,只有在传输完成后才发生什么。

您需要逐字节读取流,而不是逐行读取。例如。使用 .read():

public int read()