【问题标题】:.class uploaded on FTP server using FTPClient is corrupted [duplicate]使用 FTPClient 在 FTP 服务器上上传的 .class 已损坏 [重复]
【发布时间】:2014-04-18 07:37:01
【问题描述】:

我正在 FTP 服务器上上传一个 .class 文件。上传的文件不是空的,但肯定已损坏,因为我无法反编译它。我怀疑在 FTP 服务器上上传 .class 文件有不同的方式。

这里是sn-p:

public boolean uploadFileOnFTPServer(File file, String uploadToPath) { 
    //file - File stored on local system that is to be uploaded on server
    //uploadToPath - Remote server path where the file is to be stored

    boolean isUploaded = false;
    try {
        FileInputStream inputStream = new FileInputStream(file);
       //InputStream inputStream = new FileInputStream(file);

        if (connectToFTPServer()) { // connectToFTPServer() - method to connect to server
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // ftpClient is an object of FTPClient class

            if (ftpClient.login(userName, password)) {
                System.out.println("Logged in to server. Username: " + userName);
                if (ftpClient.changeWorkingDirectory(uploadToPath)) {
                    System.out.println("Navigated to path " + uploadToPath);                                                              

                    if (ftpClient.storeFile(file.getName(), inputStream)) {
                        inputStream.close();
                        System.out.println("File " + file.getName() + " uploaded to server.");
                        isUploaded = true;
                    }

                } 
        }
    } catch (Exception e) {
        strRemarks = "Exception reported: Unable to upload file. Error Details: " + e.toString();
        System.out.println(strRemarks);
        e.printStackTrace();
    } finally {
        disconnectFTPServer();
    }
    return isUploaded;
}

【问题讨论】:

  • 看起来不错。文件大小一样吗?
  • 没有。在本地系统上,.class 的大小是 7013,上传的大小是 7095
  • 这表明它是以文本模式上传的,并且发生了 CR/LF 翻译。尝试将文件类型设置移至登录后。
  • 非常感谢。它奏效了。

标签: java ftp ftp-client ftp-server


【解决方案1】:

您是否尝试过以二进制模式上传文本文件以查看实际传输的内容?

我也建议指定编码

client.setControlEncoding(encoding);
client.connect(host, port);

这应该在连接之前完成。

【讨论】:

    【解决方案2】:

    这行得通:

    在连接到服务器后但在传输文件之前添加 setFileType()。

                        ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);                 
                        String remoteFileName = file.getName();
                        if (ftpClient.storeFile(remoteFileName, inputStream)) {
                            inputStream.close();
                            System.out.println("File " + file.getName() + " uploaded to server.");
                            isUploaded = true;
                        }
    

    非常感谢 EJP :)

    【讨论】:

      猜你喜欢
      • 2011-09-05
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多