简体   繁体   English

使用Java FTP连接到Localhost

[英]Connect to Localhost with Java FTP

I'm developing FTP program on JAVA. 我正在JAVA上开发FTP程序。 I'm using Apache Commons Net Library. 我正在使用Apache Commons Net Library。 My Codes are below. 我的代码如下。

 import java.io.IOException; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class ServerClass { private static void showServerReply(FTPClient ftpClient) { String[] replies = ftpClient.getReplyStrings(); if (replies != null && replies.length > 0) { for (String aReply : replies) { System.out.println("SERVER: " + aReply); } } } public static void main(String[] args) { String server = "127.0.0.1"; int port = 80; String user = "root"; String pass = "root"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); showServerReply(ftpClient); int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { System.out.println("Operation failed. Server reply code: " + replyCode); return; } boolean success = ftpClient.login(user, pass); showServerReply(ftpClient); if (!success) { System.out.println("Could not login to the server"); return; } else { System.out.println("LOGGED IN SERVER"); } } catch (IOException ex) { System.out.println("Oops! Something wrong happened"); ex.printStackTrace(); } } } 

But I can't connect my localhost. 但是我无法连接本地主机。 I want to login my localhost and see my file. 我想登录我的本地主机并查看我的文件。 My errors are below. 我的错误如下。

 Oops! Something wrong happened java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) at java.net.AbstractPlainSocketImpl.connect(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at org.apache.commons.net.SocketClient.connect(SocketClient.java:188) at org.apache.commons.net.SocketClient.connect(SocketClient.java:209) at com.emrecanoztas.ftp.ServerClass.main(ServerClass.java:22) 

can you help me anybody? 你能帮助我吗? Thanks!.. 谢谢!..

Questions: 问题:

  • Is there an FTP server running? 是否正在运行FTP服务器?
  • Is there a message in the FTP server log about the connect request? FTP服务器日志中是否有关于连接请求的消息?
  • Does the FTP server allow connections from localhost? FTP服务器是否允许来自本地主机的连接?
  • Is the FTP server listening on localhost or should you use the public IP/name of the computer? FTP服务器是在本地主机上侦听还是应该使用计算机的公共IP /名称? (check with netstat) (与netstat检查)

Try the settings below. 请尝试以下设置。

  String server = "ftp.icm.edu.pl"; int port = 21; String user = "anonymous"; String pass = "me@nowhere.com"; 

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

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