FTP列表命令引发MalformedServerReplyException:服务器截断了回复

时间:2019-01-04 19:31:27

标签: java file-upload ftp apache-commons-net

我有一个使用Apache Commons Net的Java应用程序,将mp3上传到我的FTP服务器。

但是,如果它是一个新文件,它将抛出一个<div class="flex-container"> <div style="flex-grow: 1"> <div class="flex-container"> <div style="flex-grow: 0; flex-shrink: 0;">Some label</div> <div style="flex-grow: 0; flex-shrink: 0">An Input</div> <div style="flex-grow: 0; flex-shrink: 0; flex-basis: 120;">Some label</div> <div style="flex-grow: 0; flex-shrink: 0">An Input</div> <div style="flex-grow: 0; flex-shrink: 0; flex-basis: 120;">Some label</div> <div style="flex-grow: 0; flex-shrink: 0">An Input</div> </div> <div class="flex-container"> <div style="flex-grow: 0; flex-shrink: 0">Some label</div> <div style="flex-grow: 1;">An Input</div> <div style="flex-grow: 0; flex-shrink: 0">Btn</div> </div> </div> <div style="flex-grow: 0; flex-shrink: 0">Big Btn</div> </div>。如果它是现有文件,它将完美上传并覆盖。

该异常已经抛出:

MalformedServerReplyException

但是,我签入了Wireshark,命令ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 正常运行。

有人可以帮我吗?

以下是Wireshark的图像,直到出现异常: enter image description here

TYPE I

预期:成功上传
实际:MalformedServerReplyException

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();
    }

}

根据要求-完整(FTP)日志记录:

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)

1 个答案:

答案 0 :(得分:1)

必须通过完整的Wireshark转储来确认,但是我相信日志文件指示对550命令的LIST响应后跟两个换行符。 Apache FTPClient将第二个“行”视为对以下TYPE I命令的(无效)响应。因此,它看起来像是服务器端错误。

您尝试使用其他方法检查文件是否存在吗?
查看我对Checking file existence on FTP server的回答
(正如答案所说,使用listFiles检查文件是否存在实际上违反了FTP协议。)