Java FTP上传

发布于 2024-12-03 07:15:09 字数 2819 浏览 0 评论 0原文

我遇到了这个困扰我一周的问题,

我正在尝试使用 java 通过 ftp 上传文件 xml,没什么特别的。

经过几次网络搜索后,我决定使用 ftp4j 包,根据文档应该可以解决问题。

重点是这个 ftp 服务器实际上与 Filezilla 一起工作...

这些是我的几行代码:

    public void connect() {
    try {


        String[] con = client.connect(s);
        for (int i = 0; i < con.length; i++) {
            System.out.println(con[i]);
        }

        client.login(user, password);

        s = client.getHost();
        System.out.println(s);

        con = client.help();
        for (int i = 0; i < con.length; i++) {
            System.out.println(con[i]);
        }

        client.changeDirectory(sDir);
        s = client.currentDirectory();

        System.setProperty("ftp4j.passiveDataTransfer.useSuggestedAddress", "1");

        client.setPassive(true);

        System.out.println(client.isPassive());

        client.setType(FTPClient.TYPE_BINARY);

        client.upload(file);


    } catch (Exception ex) {
        Logger.getLogger(ftpConnector.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex.toString());

    }
}

我得到这个输出:

*
*                                 
*  Aruba.it                    
#  The Web Hosting Company     
#                                 
#
This is a private system - No anonymous login
ftp.open-eye.it
The following SITE commands are recognized
 CHMOD
 IDLE
 UTIME
Pure-FTPd - http://pureftpd.org/

true
[code=200, message=TYPE is now ASCII]

java.net.SocketException: Connection reset

我很确定连接重置与我用 client.upload(file 给出的上传命令相关) );

我所有的变量都是在上层声明的。

ava.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openPassiveDataTransferChannel(FTPClient.java:3534)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3468)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3297)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3208)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3073)

感谢您抽出时间, 弗朗西斯科.

I got this issue that is bothering me for a week now,

I'm trying to upload a file xml with ftp using java, nothing too fancy.

After a few web serch i decided to use the ftp4j pack wich, according to the documentation should do the trick.

Point is that this ftp server actually work with Filezilla...

these are a few line of my code:

    public void connect() {
    try {


        String[] con = client.connect(s);
        for (int i = 0; i < con.length; i++) {
            System.out.println(con[i]);
        }

        client.login(user, password);

        s = client.getHost();
        System.out.println(s);

        con = client.help();
        for (int i = 0; i < con.length; i++) {
            System.out.println(con[i]);
        }

        client.changeDirectory(sDir);
        s = client.currentDirectory();

        System.setProperty("ftp4j.passiveDataTransfer.useSuggestedAddress", "1");

        client.setPassive(true);

        System.out.println(client.isPassive());

        client.setType(FTPClient.TYPE_BINARY);

        client.upload(file);


    } catch (Exception ex) {
        Logger.getLogger(ftpConnector.class.getName()).log(Level.SEVERE, null, ex);
        System.out.println(ex.toString());

    }
}

well i get this output:

*
*                                 
*  Aruba.it                    
#  The Web Hosting Company     
#                                 
#
This is a private system - No anonymous login
ftp.open-eye.it
The following SITE commands are recognized
 CHMOD
 IDLE
 UTIME
Pure-FTPd - http://pureftpd.org/

true
[code=200, message=TYPE is now ASCII]

java.net.SocketException: Connection reset

I'm pretty sure the Connection reset is related to the upload command i give with client.upload(file);

all my vars are declared on an upper level.

ava.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:189)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:126)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
at java.io.InputStreamReader.read(InputStreamReader.java:168)
at it.sauronsoftware.ftp4j.NVTASCIIReader.readLine(NVTASCIIReader.java:105)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.read(FTPCommunicationChannel.java:142)
at it.sauronsoftware.ftp4j.FTPCommunicationChannel.readFTPReply(FTPCommunicationChannel.java:187)
at it.sauronsoftware.ftp4j.FTPClient.openPassiveDataTransferChannel(FTPClient.java:3534)
at it.sauronsoftware.ftp4j.FTPClient.openDataTransferChannel(FTPClient.java:3468)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3297)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3208)
at it.sauronsoftware.ftp4j.FTPClient.download(FTPClient.java:3073)

Thanks for your time,
Francesco.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文