5

我想用 Apache Commons Net 实现一个 FTP 客户端,仅用于上传数据。FTP 服务器的连接和登录工作正常。但是上传不正常。这些文件与原件相比有点大。并且文件已损坏。我尝试了图像、视频和文本文件。只有文本文件没问题。

现在我在调试时看到

boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

给我false。所以不能设置。为什么?(也许这不是问题?)

这是我的其余代码

client=new FTPClient();

    try {           
        int reply;
        client.connect(url, port);
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }


        client.login(user, pw);
        boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();

if (client.isConnected())
    {
    try {
        File file=new File(<FILE>);
        FileInputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = client.storeFileStream(file.getName());

          byte[] buffer = new byte[4096];
          int l;
       while((l = inputStream.read(buffer))!=-1)
               {
                outputStream.write(buffer, 0, l);
            }

          inputStream.close();
          outputStream.flush();
          outputStream.close();}
4

3 回答 3

12

更改以下内容:

boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

应该:

boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);

您将 FileTransferModes 与 FileTypes 混淆了。

可用的文件类型是:

可用的 FileTransferModes 是:

我想如果 apache 为这些常量类型引入了枚举,那么可以避免这种问题,但是该库将无法用于 pre-java-5 运行时。
我想知道 java 1.4 兼容性到底有多大问题。

于 2011-07-11T21:21:49.940 回答
2

如果只有文本文件传输成功,我怀疑您需要设置二进制传输文件类型。

请参阅setFileType方法以了解如何执行此操作。

commons-net wiki提到这是大多数文件损坏问题的原因。

于 2011-07-11T14:01:16.933 回答
0

这对我有用,上传图片并在没问题后下载:使用

    FTP.LOCAL_FILE_TYPE

此代码使用记录器,为您替换记录器或使用 System.out.println("");

    private void cargarData(File filelocal) {
    FTPClient client = new FTPClient();

    try {

        client.connect("URLHOSTFTP", "PORT: DEFAULT 21");
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            logger.error("FTP server refused connection.");
            System.exit(1);
        }
        client.login("USER FTP", "PASS FTP");
        boolean type = client.setFileType(FTP.LOCAL_FILE_TYPE);

        logger.info("Tipo Aceptado:" + type);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();
        if (client.isConnected()) {
            FileInputStream fis = null;
            fis = new FileInputStream(filelocal);
            client.storeFile(filelocal.getName(), fis);
            client.logout();
            if (fis != null) {
                fis.close();
            }
        }
        logger.info(client.getReplyString());
    } catch (IOException e) {
        logger.error("error" + e.getMessage());
        e.printStackTrace();

    } catch (Exception e) {
        logger.error("error" + e.getMessage());
        e.printStackTrace();

    }
}
于 2019-06-23T03:17:19.580 回答