简体   繁体   English

FTP没有用户名和密码连接?

[英]FTP connect with no username and password?

Hello I have connected to the FTP using URLConnection with just the IP address with not specifing the login credentials. 您好,我已使用URLConnection仅使用IP地址连接到FTP,而未指定登录凭据。

URLConnection conn = new URLConnection(new URL("ftp://222.101.102.1/etc"));

It works, with no credentials because the server probably allows annonymous connection. 它可以在没有凭据的情况下工作,因为服务器可能允许匿名连接。

But for practice I wanted to make an ftp connection from scratch with Socket client, and I created a new socket connection to that IP: 但是为了练习,我想使用Socket客户端从头开始建立ftp连接,然后我创建了一个到该IP的新套接字连接:

Socket socket = new Socket("222.101.102.1", 21);
if (socket != null) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String version = reader.readLine();
    System.out.println(version);
}

This prints 220 vsFTPd (2.0.5) 这将打印220 vsFTPd (2.0.5)

but when I try to do command like LS \\r\\n 但是当我尝试执行LS \\r\\n类的命令时

I get 530 Please login with USER and PASS 我得到530 Please login with USER and PASS

Why is it asking me for login if with URLConnection it did not? 为什么用URLConnection要求我登录?

This code was not compiled because it was memorized from my other computer so let me know if you notice something wrong and I'll edit right away. 该代码未编译,因为它是从我的另一台计算机上记住的,所以如果您发现错误,请告诉我,我将立即进行编辑。

I managed to figure it out. 我设法弄清楚了。

URL.openConnection() basically returns a class that implements URLStreamHandler based on the protocol. URL.openConnection()基本上返回一个基于协议实现URLStreamHandler的类。 So in this case the protocol is ftp and it returns FtpURLConnection . 因此,在这种情况下,协议为ftp并返回FtpURLConnection

So there is a check that if the username is null (not specified in the URL), and if it is so it sets it to anonymous and uses a password of the java version with @ 因此,要检查用户名是否为null(在URL中未指定),如果是,则将其设置为匿名并使用带有@的java版本的密码。

eg Java1.8.0@ not sure for what purpose, maybe for login history. 例如Java1.8.0@不确定用于什么目的,可能用于登录历史记录。 But you can use any password to login with anonymous unless it's configured for something else I think. 但是您可以使用任何密码以匿名方式登录,除非我认为已对其进行了配置。

        if(this.user == null) {
            this.user = "anonymous";
            String var11 = (String)AccessController.doPrivileged(new GetPropertyAction("java.version"));
            this.password = (String)AccessController.doPrivileged(new GetPropertyAction("ftp.protocol.user", "Java" + var11 + "@"));
        }

So I basically logged in with Anonymous and no password . 所以我基本上使用Anonymous登录, 没有密码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM