FTP 列表命令抛出 MalformedServerReplyException:截断的服务器回复
FTP list command throws MalformedServerReplyException: Truncated server reply
我有一个 Java 应用程序使用 Apache Commons Net 将 mp3 上传到我的 FTP 服务器。
但是,如果是一个新文件,它会抛出一个MalformedServerReplyException
。如果是已有文件,会完美上传覆盖。
异常已在行抛出:
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
但是,我检查了 Wireshark,命令 TYPE I
正常。
有人可以帮我解决这个问题吗?
这里是异常之前 Wireshark 的图像:
public static void transferFile(File file, boolean OverwriteAllowed) {
FtpLogin();
try {
System.out.println('\n');
System.out.println("DEBUG: "+ file.toString());
boolean isNewFile = false;
String filePath = getFtpPathToMusic() + file.getName();
ftpClient.sendNoOp();
showServerReply();
if (ftpClient.listFiles(filePath).length > 0) {
System.out.println("File exists, and overwrite allowed is: " + OverwriteAllowed);
} else {
isNewFile = true;
// System.out.println("File does not exist yet on server");
}
if (OverwriteAllowed || isNewFile) {
ftpClient.setStrictReplyParsing(false);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
showServerReply();
InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = ftpClient.storeFileStream(filePath);
byte[] bytesIn = new byte[4096];
int read;
long remaining = file.length();
long transferred = 0;
System.out.println("Transfering data...");
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
remaining -= read;
transferred += read;
System.out.print('\r');
System.out.print("Remaining: " + remaining/1024 + "kb | Transferred: " + transferred/1024 + "kb");
}
System.out.println();
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
showServerReply();
System.out.println("File " + filePath + " uploaded succesfully");
}
} else {
System.out.println("File is skipped");
}
} catch (IOException e) {
e.printStackTrace();
showServerReply();
}
}
预期:上传成功
实际:MalformedServerReplyException
org.apache.commons.net.MalformedServerReplyException: Truncated server reply:
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:332)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:300)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:523)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:648)
at org.apache.commons.net.ftp.FTP.type(FTP.java:1119)
at org.apache.commons.net.ftp.FTPClient.setFileType(FTPClient.java:1559)
at FtpSupport.FtpTransfer.transferFile(FtpTransfer.java:116)
at Home.ConverterGuiHome$convertMp3FilesExecute.actionPerformed(ConverterGuiHome.java:144)
应要求 - 完整 (FTP) 日志记录:
Getting files from: /ideaProjects/GsmMp3Manager/src/main/Mp3Files/export
Creating mp3 object from file: 377 test.mp3
220 DiskStation FTP server ready.
SERVER: 220 DiskStation FTP server ready.
NOOP
200 NOOP command successful.
SERVER: 200 NOOP command successful.
USER *******
331 Password required for Barbet.
PASS *******
230 User Barbet logged in.
SERVER: 230 User Barbet logged in.
LOGGED IN SERVER
File listing in directory :
SYST
215 UNIX Type: L8
PASV
227 Entering Passive Mode (77,164,214,202,216,242)
LIST
150 Opening BINARY mode data connection for 'file list'.
226 Transfer complete.
[home] 4096 2018-12-12 00:00:00
[Spotlight] 4096 2018-11-17 00:00:00
[music] 4096 2018-11-22 00:00:00
[Mission Possible] 4096 2013-11-13 00:00:00
DEBUG:377 test.mp3
DEBUG: src/main/Mp3Files/export/377 test.mp3
NOOP
200 NOOP command successful.
SERVER: 200 NOOP command successful.
PASV
227 Entering Passive Mode (77,164,214,202,216,241)
LIST /Spotlight/muziek/377 test.mp3
150 Opening BINARY mode data connection for 'file list'.
550 /Spotlight/muziek/377 test.mp3: No such file or directory.
TYPE I
org.apache.commons.net.MalformedServerReplyException: Truncated server reply:
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:332)
(... see stack trace)
Close ftp session
QUIT
200 Type set to I.
SERVER: 200 Type set to I.
必须通过完整的 Wireshark 转储来确认,但我相信日志文件表明 550
对 LIST
命令的响应后跟两个换行符。 Apache FTPClient
将第二个 "line" 视为对以下 TYPE I
命令的(无效)响应。所以它看起来像一个服务器端错误。
您是否尝试过使用其他方法检查文件是否存在?
请参阅我对 Checking file existence on FTP server
的回答
(正如答案所说,使用 listFiles
检查文件是否存在实际上违反了 FTP 协议)。
我有一个 Java 应用程序使用 Apache Commons Net 将 mp3 上传到我的 FTP 服务器。
但是,如果是一个新文件,它会抛出一个MalformedServerReplyException
。如果是已有文件,会完美上传覆盖。
异常已在行抛出:
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
但是,我检查了 Wireshark,命令 TYPE I
正常。
有人可以帮我解决这个问题吗?
这里是异常之前 Wireshark 的图像:
public static void transferFile(File file, boolean OverwriteAllowed) {
FtpLogin();
try {
System.out.println('\n');
System.out.println("DEBUG: "+ file.toString());
boolean isNewFile = false;
String filePath = getFtpPathToMusic() + file.getName();
ftpClient.sendNoOp();
showServerReply();
if (ftpClient.listFiles(filePath).length > 0) {
System.out.println("File exists, and overwrite allowed is: " + OverwriteAllowed);
} else {
isNewFile = true;
// System.out.println("File does not exist yet on server");
}
if (OverwriteAllowed || isNewFile) {
ftpClient.setStrictReplyParsing(false);
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
showServerReply();
InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = ftpClient.storeFileStream(filePath);
byte[] bytesIn = new byte[4096];
int read;
long remaining = file.length();
long transferred = 0;
System.out.println("Transfering data...");
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
remaining -= read;
transferred += read;
System.out.print('\r');
System.out.print("Remaining: " + remaining/1024 + "kb | Transferred: " + transferred/1024 + "kb");
}
System.out.println();
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
showServerReply();
System.out.println("File " + filePath + " uploaded succesfully");
}
} else {
System.out.println("File is skipped");
}
} catch (IOException e) {
e.printStackTrace();
showServerReply();
}
}
预期:上传成功
实际:MalformedServerReplyException
org.apache.commons.net.MalformedServerReplyException: Truncated server reply:
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:332)
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:300)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:523)
at org.apache.commons.net.ftp.FTP.sendCommand(FTP.java:648)
at org.apache.commons.net.ftp.FTP.type(FTP.java:1119)
at org.apache.commons.net.ftp.FTPClient.setFileType(FTPClient.java:1559)
at FtpSupport.FtpTransfer.transferFile(FtpTransfer.java:116)
at Home.ConverterGuiHome$convertMp3FilesExecute.actionPerformed(ConverterGuiHome.java:144)
应要求 - 完整 (FTP) 日志记录:
Getting files from: /ideaProjects/GsmMp3Manager/src/main/Mp3Files/export
Creating mp3 object from file: 377 test.mp3
220 DiskStation FTP server ready.
SERVER: 220 DiskStation FTP server ready.
NOOP
200 NOOP command successful.
SERVER: 200 NOOP command successful.
USER *******
331 Password required for Barbet.
PASS *******
230 User Barbet logged in.
SERVER: 230 User Barbet logged in.
LOGGED IN SERVER
File listing in directory :
SYST
215 UNIX Type: L8
PASV
227 Entering Passive Mode (77,164,214,202,216,242)
LIST
150 Opening BINARY mode data connection for 'file list'.
226 Transfer complete.
[home] 4096 2018-12-12 00:00:00
[Spotlight] 4096 2018-11-17 00:00:00
[music] 4096 2018-11-22 00:00:00
[Mission Possible] 4096 2013-11-13 00:00:00
DEBUG:377 test.mp3
DEBUG: src/main/Mp3Files/export/377 test.mp3
NOOP
200 NOOP command successful.
SERVER: 200 NOOP command successful.
PASV
227 Entering Passive Mode (77,164,214,202,216,241)
LIST /Spotlight/muziek/377 test.mp3
150 Opening BINARY mode data connection for 'file list'.
550 /Spotlight/muziek/377 test.mp3: No such file or directory.
TYPE I
org.apache.commons.net.MalformedServerReplyException: Truncated server reply:
at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:332)
(... see stack trace)
Close ftp session
QUIT
200 Type set to I.
SERVER: 200 Type set to I.
必须通过完整的 Wireshark 转储来确认,但我相信日志文件表明 550
对 LIST
命令的响应后跟两个换行符。 Apache FTPClient
将第二个 "line" 视为对以下 TYPE I
命令的(无效)响应。所以它看起来像一个服务器端错误。
您是否尝试过使用其他方法检查文件是否存在?
请参阅我对 Checking file existence on FTP server
的回答
(正如答案所说,使用 listFiles
检查文件是否存在实际上违反了 FTP 协议)。