Apache Commons FTP 问题

Apache Commons FTP problems(Apache Commons FTP 问题)
本文介绍了Apache Commons FTP 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I want to implement a FTP Client with Apache Commons Net only for uploading data. The Connection and Login to FTP-Server works fine. But the upload does not work right. The files are a little to big as the originals. And the files are damaged. I tried an image, a video and a textfile. Only the textfile is alright.

现在我在调试时看到了

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

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

gives me false. So it can not be set. Why? (Maybe this is not the problem?)

这是我的其余代码

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

推荐答案

更改如下:

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

应该是:

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

您将 FileTransferModes 与 FileTypes 混淆了.

You have confused FileTransferModes with FileTypes.

可用的文件类型有:

  • FTP.ASCII_FILE_TYPE(默认)
  • FTP.BINARY_FILE_TYPE
  • FTP.EBCDIC_FILE_TYPE
  • FTP.LOCAL_FILE_TYPE

可用的 FileTransferMode 有:

The available FileTransferModes are:

  • FTP.STREAM_TRANSFER_MODE(默认)
  • FTP.BLOCK_TRANSFER_MODE
  • FTP.COMPRESSED_TRANSFER_MODE

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

I suppose if apache introduced enums for these constant types, then this kind of problem could be avoided, but then the library would not be available to pre-java-5 runtimes.
I wonder how much of an issue java 1.4 compatibility really is.

这篇关于Apache Commons FTP 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)