在某些服务器上,Apache Java FTP客户端未切换到二进制传输模式 [英] Apache Java FTP client does not switch to binary transfer mode on some servers

查看:109
本文介绍了在某些服务器上,Apache Java FTP客户端未切换到二进制传输模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用org.apache.commons.net.ftp库与FTP服务器一起使用.我使用了大约5台FTP服务器,其中一台不能正常工作(对我来说).对于上传文件,我也使用方法ftpClient.setFileType(FTP.BINARY_FILE_TYPE),但是在此服务器上,上传文件的大小与本地HDD上的大小不同.通常是MP3文件,当我尝试播放上载的MP3时,会有一些嗡嗡声. 我怀疑FTP服务器的配置错误,但是负责此服务器的公司不太可能进行一些更改. 还有一个非常重要的注意事项:当我使用Total Commander或WinSCP上载文件时,上载的文件大小相同是正确的.

I'm using org.apache.commons.net.ftp library to work with FTP servers. I use about 5 FTP servers and one of them doesn't work properly (for me). For uploading files I also use method ftpClient.setFileType(FTP.BINARY_FILE_TYPE), but on this server the size of the uploaded file is not the same as on my local HDD. It is usually MP3 files and when I try to play uploaded MP3 there is some buzz. I suspects that FTP server has wrong configuration, but the company which takes care of this server would be very unlikely to do some changes. One more very important note: when I use Total Commander or WinSCP to upload file, the uploaded file is correct with same size.

这是我的代码:

public class FtpConnection {
    String FTP_SERVER;
    String FTP_USER;
    String FTP_PASSWORD;
    int FTP_PORT;
    FTPClient ftpClient;
    int attempts = 3;
    long FILE_SIZE;

    public FtpConnection() {
        FTP_SERVER = StradaAd.FTP_SERVER;
        FTP_USER = StradaAd.FTP_USER;
        FTP_PASSWORD = StradaAd.FTP_PASSWORD;
        FTP_PORT = StradaAd.FTP_PORT;

        ftpClient = new FTPClient();

        ftpClient.setCopyStreamListener(streamListener);

        ftpClient.setBufferSize(1024 * 1024 * 1024);
        ftpClient.setConnectTimeout(240000);
        ftpClient.setDataTimeout(240000);
        try {
            ftpClient.connect(FTP_SERVER, FTP_PORT);
            ftpClient.setControlKeepAliveTimeout(300);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            int replyCode = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(replyCode)) {
                StradaAd.errorArea.append("Operation failed. Server reply code: " + replyCode);
                return;
            }
            boolean success = ftpClient.login(FTP_USER, FTP_PASSWORD);

            if (!success) {
                //unsuccess
            }
        } catch (IOException ex) {
          //error
        }
    }

    public boolean uploadFile(File source, String destination, String chain) {
        try {
            FILE_SIZE = source.length();

            ftpClient.cwd("//" + chain + "/REKLAMY/");

            for(int i=0;i<attempts;i++) {
                ftpClient.storeFile(destination, new FileInputStream(source));

                for(FTPFile f : ftpClient.listFiles()) {
                    if(f.getName().equals(destination) && f.getSize() == source.length())
                        return true;
                }
            }
            return false;
        } catch (IOException ex) {
            Logger.getLogger(FtpConnection.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }    
}

有人知道解决方案如何更改此代码或将此FTP服务器设置为具有相同大小的上载文件吗? 预先感谢您的帮助.

Do someone know the solution how to change this code or set this FTP server to have the same size uploaded file? Thanks in advance for any help.

推荐答案

您调用

You call the FTPClient.setFileType method too early.

至少 ProFTPD FTP服务器会在身份验证后重置默认的传输模式(

At least the ProFTPD FTP server resets the default transfer mode after the authentication (FTPClient.login).

.login之后移动.setFileType调用.

这篇关于在某些服务器上,Apache Java FTP客户端未切换到二进制传输模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆