我已经建立了一个具有RETR
和LIST
命令的FTP客户端,但是遇到了问题。在我实现RETR
命令后,我不断收到此错误:
1:220-FileZilla Server version 0.9.50 beta
1:220-written by Tim Kosse (tim.kosse@filezilla-project.org)
1:220 Please visit https://filezilla-project.org/
2331 Password required for geek
3230 Logged on
5227 Entering Passive Mode (127,0,0,1,217,137)
6200 Type set to A
7150 Opening data channel for directory listing of "/"
5227 Entering Passive Mode (127,0,0,1,217,139)
150 Opening data channel for directory listing of "/"
8501 Syntax error
Exception in thread "main" 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 java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ftp.ftp.connect(ftp.java:135)
at ftp.test.main(test.java:12)
此外,在我实现RETR
命令后,客户端停止提供LIST
命令;它不会告诉文件夹中有哪些文件。
public class ftp{
public synchronized void connect(String host, int port, String user,
String pass) throws IOException {
Socket socket = new Socket(host, port);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
String response = reader.readLine();
if (!response.startsWith("220-")) {
throw new IOException(
"FTP modtog et ukendt svar, da den blev forbundet til FTP server: "
+ response);
} else {System.out.println("1:"+response);
response = reader.readLine();
System.out.println("1:"+response);
response = reader.readLine();
System.out.println("1:"+response);
}
writer.write("user geek"+"\r\n");
writer.flush();
response= reader.readLine();
System.out.println("2"+response);
writer.write("PASS hello" +"\r\n");
writer.flush();
response= reader.readLine();
System.out.println("3"+response);
writer.write("pasv"+"\r\n");
writer.flush();
response= reader.readLine();
System.out.println("5"+response );
String [] r=response.trim().split(",");
String reg=r[4];
String add= r[5].replace(")", "");
int m = Integer.parseInt(reg);
int a = Integer.parseInt(add);
int port1 = m*256+a;
Socket datasocket = new Socket("127.0.0.1", port1);
writer.write("type A\r\n");
writer.flush();
response= reader.readLine();
System.out.println("6"+response);
BufferedReader reader2 = new BufferedReader(new InputStreamReader(datasocket.getInputStream()));
writer.write("LIST\r\n");
writer.flush();
response = reader.readLine();
System.out.println("7" + response);
List<String> files = new ArrayList<>();
String text;
while ((text = reader2.readLine()) != null) {
String filename = parseFilename(text);
files.add(filename);
}
reader2.close();
datasocket.close();
reader.readLine();
for (String file : files) {
int passivePort = getPort(reader, writer);
writer.write("RETR\r\n");
writer.flush();
System.out.println(response);
byte[] mybytearray = new byte[1024];
try (Socket filesocket = new Socket("localhost", passivePort);
InputStream is = filesocket.getInputStream();
FileOutputStream fos = new FileOutputStream("c:/Users/Username/Desktop/FTP/" + file)) {
int count;
while ((count = is.read(mybytearray)) >= 0) {
fos.write(mybytearray, 0, count);
}
}
}
writer.write("RETR\r\n");
writer.flush();
response= reader.readLine();
System.out.println("8"+response);
Socket filsocket = new Socket ("localhost", port1);
InputStream is = null;
FileOutputStream fos = null;
byte[] mybytearray = new byte[1024];
try {
is = filsocket.getInputStream();
fos = new FileOutputStream("myfile.txt");
int count;
while ((count = is.read(mybytearray)) >= 0) {
fos.write(mybytearray, 0, count);
}
} finally {
fos.close();
is.close();
filsocket.close();
}
}
private String parseFilename(String text) {
return "abc.txt";
}
private int getPort(BufferedReader reader, BufferedWriter writer) throws IOException { //this is line 135
String response;
writer.write("PASV" + "\r\n");
writer.flush();
response = reader.readLine();
System.out.println("5" + response);
String[] r = response.trim().split(",");
String reg = r[4];
String add = r[5].replace(")", "");
int m = Integer.parseInt(reg);
int a = Integer.parseInt(add);
return m * 256 + a;
}
}
提前致谢:)
答案 0 :(得分:0)
RETR
命令有一个必需参数,用于指定要下载的文件的名称。服务器如何知道,你想要下载哪个文件?
writer.write("RETR " + file + "\r\n");
请参阅RFC 959:
RETR <SP> <pathname> <CRLF>
无论如何,为什么要自己实现FTP协议?使用一些现有的实现,例如FTPClient
from Apache Commons。