Java FTP list命令引发格式错误的ServerReplyException:截断的服务器应答
我有一个Java应用程序,使用ApacheCommonsNet将mp3上传到我的FTP服务器 但是,如果它是一个新文件,它将抛出一个Java FTP list命令引发格式错误的ServerReplyException:截断的服务器应答,java,file-upload,ftp,apache-commons-net,Java,File Upload,Ftp,Apache Commons Net,我有一个Java应用程序,使用ApacheCommonsNet将mp3上传到我的FTP服务器 但是,如果它是一个新文件,它将抛出一个格式错误的ServerReplyException。如果它是一个现有的文件,它将上传和覆盖完美 异常已在第行引发: ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 但是,我签入了Wireshark,命令typei运行正常 有人能帮我吗 以下是Wireshark在异常之前的图像: 预期:成功上载 实际:格式错误的Serve
格式错误的ServerReplyException
。如果它是一个现有的文件,它将上传和覆盖完美
异常已在第行引发:
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
但是,我签入了Wireshark,命令typei
运行正常
有人能帮我吗
以下是Wireshark在异常之前的图像:
预期:成功上载实际:格式错误的ServerReplyException
org.apache.commons.net.MalformedServerReplyException:截断的服务器回复:
位于org.apache.commons.net.ftp.ftp.\uu getReply(ftp.java:332)
位于org.apache.commons.net.ftp.ftp.\uu getReply(ftp.java:300)
位于org.apache.commons.net.ftp.ftp.sendCommand(ftp.java:523)
位于org.apache.commons.net.ftp.ftp.sendCommand(ftp.java:648)
位于org.apache.commons.net.ftp.ftp.type(ftp.java:1119)
位于org.apache.commons.net.ftp.FTPClient.setFileType(FTPClient.java:1559)
位于FtpSupport.FtpTransfer.transferFile(FtpTransfer.java:116)
ConverterGuiHome$convertMp3FilesExecute.actionPerformed(ConverterGuiHome.java:144)
应请求-完整(FTP)日志记录:
从获取文件:/ideaProjects/GsmMp3Manager/src/main/Mp3Files/export
从文件377 test.mp3创建mp3对象
220磁盘站FTP服务器就绪。
服务器:220 DiskStation FTP服务器就绪。
努普
200 NOOP命令成功。
服务器:200 NOOP命令成功。
使用者*******
331 Barbet需要密码。
通过*******
230用户Barbet已登录。
服务器:230个用户Barbet已登录。
登录服务器
目录中的文件列表:
系统
215 UNIX类型:L8
PASV
227进入被动模式(77164214202216242)
列表
150打开“文件列表”的二进制模式数据连接。
226传输完成。
[主页]40962018-12-12 00:00:00
[聚光灯]40962018-11-17 00:00:00
[音乐]40962018-11-22 00:00:00
[可能的任务]40962013-11-13 00:00:00
调试:377 test.mp3
调试:src/main/Mp3Files/export/377 test.mp3
努普
200 NOOP命令成功。
服务器:200 NOOP命令成功。
PASV
227进入被动模式(77164214202216241)
LIST/Spotlight/muziek/377 test.mp3
150打开“文件列表”的二进制模式数据连接。
550/Spotlight/muziek/377 test.mp3:没有这样的文件或目录。
第一类
org.apache.commons.net.MalformedServerReplyException:截断的服务器回复:
位于org.apache.commons.net.ftp.ftp.\uu getReply(ftp.java:332)
(…请参阅堆栈跟踪)
关闭ftp会话
退出
200类型设置为I。
服务器:200类型设置为I。
必须通过完整的Wireshark转储来确认,但我相信日志文件表明550
对LIST
命令的响应后面有两行换行符。ApacheFTPClient
将第二个“行”视为对以下TYPE I
命令的(无效)响应。所以它看起来像一个服务器端错误
您是否尝试使用其他方法检查文件是否存在?查看我对
(正如答案所说,使用
listFiles
检查文件是否存在实际上违反了FTP协议)。您能用详细的stacktrace扩展您的问题吗?您使用的是FTP
协议还是SFTP
?谢谢您的回复。我正在使用FTP。我在原始帖子中添加了stacktrace。我添加了logger,这只确认了我在wireshark中看到的内容。尽管如此,还是要谢谢你,添加一个日志总是一个好主意,我不知道这么简单。是的,我添加了它,我相信你的问题标题是不正确的。你的问题实际上与“上传”无关。问题已经发生在listFiles
上,而不是storeFileStream
上。令人惊讶的是,我自己还没有弄明白这一点。非常感谢:)。我是一名初级程序员,从中我学到了很多。祝你周末愉快!还有一个问题,您提到Wireshark可以证实这一点。你希望在那里看到什么?仅供学习和理解:)。尝试使用Wireshark的“跟随TCP流”功能,看看是否确实有两条换行。
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();
}
}