我试图制作仅使用套接字的ftpclient。我正在与服务器建立联系,但我不知道如何显示文件。这是我的代码:
package pl.edu.ftp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.*;
import pl.edu.config.ConfigData;
public class FTPProtocol {
private Socket socket;
private BufferedReader reader;
private OutputStreamWriter writer;
private Socket socket2;
private BufferedReader reader2;
private OutputStreamWriter writer2;
private String serverImportantOutput;
private boolean firstSocket = true;
public void makeConnection(ConfigData configData) throws UnknownHostException, IOException, InterruptedException {
socket = new Socket(configData.getServerAddress(), configData.getPort());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new OutputStreamWriter(socket.getOutputStream());
(new Thread(new Runnable() {
public void run() {
try {
String line;
if (firstSocket == true) {
while ((line = reader.readLine()) != null) {
System.out.println("SERVER: " + line);
if (line.startsWith("227 Entering Passive Mode")) {
serverImportantOutput = line;
}
}
} else {
while ((line = reader.readLine()) != null) {
System.out.println("SERVER: " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
})).start();
Thread.sleep(2000);
sendCommand("USER " + configData.getUsername());
sendCommand("PASS " + configData.getPassword());
sendCommand("SYST");
sendCommand("FEAT");
sendCommand("PWD");
sendCommand("TYPE I");
sendCommand("PWD");
List<String> newData = sendCommandPasv("PASV");
socket2 = new Socket(newData.get(0), Integer.parseInt(newData.get(1)));
reader2 = new BufferedReader(new InputStreamReader(socket2.getInputStream()));
writer2 = new OutputStreamWriter(socket2.getOutputStream());
sendCommand2("LIST");
}
private void sendCommand(String command) throws IOException, InterruptedException {
firstSocket = true;
writer.write(command + "\r\n");
writer.flush();
System.out.println("Client:" + command);
Thread.sleep(2000);
}
private void sendCommand2(String command) throws IOException, InterruptedException {
firstSocket = true;
writer2.write(command + "\r\n");
writer2.flush();
System.out.println("Client:" + command);
Thread.sleep(2000);
firstSocket = false;
Thread.sleep(2000);
}
private List<String> sendCommandPasv(String command) throws IOException, InterruptedException {
List<String> listOfStrings = new ArrayList<String>();
writer.write(command + "\r\n");
writer.flush();
System.out.println("Client:" + command);
Thread.sleep(2000);
String fullAdress = serverImportantOutput.substring(serverImportantOutput.indexOf("(") + 1,
serverImportantOutput.indexOf(")"));
List<String> partsOfAdress = Arrays.asList(fullAdress.replaceAll("\\s+", "").split(","));
String host = partsOfAdress.get(0) + "." + partsOfAdress.get(1) + "." + partsOfAdress.get(2) + "."
+ partsOfAdress.get(3);
String port = Integer
.toString(Integer.parseInt(partsOfAdress.get(4)) * 256 + Integer.parseInt(partsOfAdress.get(5)));
System.out.println("Host: " + host);
System.out.println("Port: " + port);
listOfStrings.add(host);
listOfStrings.add(port);
return listOfStrings;
}
}
我尝试使用LIST,NLST,MLSD,但仍然无效。在控制台我有:
SERVER: 227 Entering Passive Mode (127,0,0,1,197,183)
Host: 127.0.0.1
Port: 50615
Client:LIST
和&#34;客户:LIST&#34;我得等很长时间。之后我得到了:
SERVER: 421 Connection timed out.
有人可能知道我做错了什么?
我现在编辑了这个方法:
private void sendCommand2(String command) throws IOException, InterruptedException {
writer.write(command + "\r\n");
writer.flush();
System.out.println("Client:" + command);
firstSocket = false;
Thread.sleep(2000);
}
现在我的结果是:
SERVER: 227 Entering Passive Mode (127,0,0,1,196,72)
Host: 127.0.0.1
Port: 50248
Client:LIST
SERVER: 150 Opening data channel for directory listing of "/"
SERVER: 226 Successfully transferred "/"
但它仍然没有显示任何文件。
现在我编辑了从阅读器2读取&#34; bool firstSocket&#34;是假的。所以它应该从&#34; reader2&#34;发送&#34; LIST&#34;命令,但它仍然没有显示任何内容。
(new Thread(new Runnable() {
public void run() {
try {
String line;
if (firstSocket == true) {
while ((line = reader.readLine()) != null) {
System.out.println("SERVER: " + line);
if (line.startsWith("227 Entering Passive Mode")) {
serverImportantOutput = line;
}
}
} else {
while ((line = reader2.readLine()) != null) {
System.out.println("SERVER: " + line);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
})).start();
答案 0 :(得分:0)