如何在程序中实现retr和list命令?我在互联网上看过,但我找不到任何帮助我的东西。我还需要添加端口20和21.我的程序是一个FTP客户端,它必须从FTP服务器接收文件和回复。
import it.sauronsoftware.ftp4j.FTPClient;
import java.net.*;
import java.util.Scanner;
import java.io.*;
public class FTPKlient {
static String username;
static String pass;
static class Fil
{
Socket Klient;
static BufferedReader br;
static DataInputStream din;
static DataOutputStream dos;
Fil(Socket soc)
{
try
{
Klient=soc;
din=new DataInputStream(Klient.getInputStream());
dos=new DataOutputStream(Klient.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
}
catch(Exception s)
{
}
}
public static void main(String args[]) throws Exception
{
Scanner scan= new Scanner(System.in);
System.out.println("Hvad er brugernavnet?:");
username=scan.nextLine();
System.out.println("Hvad er koden?:");
pass=scan.nextLine();
FTPClient client = new FTPClient();
client.connect("ftp.host.com");
if(username.equals("geek") && (pass.equals("hello"))){
} else {
System.out.println("Adgang nægtet, du har skrevet de forkerte oplysninger");
}
Socket soc=new Socket("localhost",14147);
Fil t=new Fil(soc);
t.menu();
}
private static void Modtag() throws Exception
{
String filnavn;
System.out.print("Hvilken fil søger du? :");
filnavn=br.readLine();
dos.writeUTF(filnavn);
String msgFromServer=din.readUTF();
if(msgFromServer.compareTo("Filen findes ikke")==0)
{
System.out.println("Den indtastede fil findes ikke i serveren");
return;
}
else if(msgFromServer.compareTo("Færdig")==0)
{
System.out.println("Filen er i gang med at blive hentet, vent venligst");
File f=new File(filnavn);
if(f.exists())
{
String o;
System.out.println("Destinationen har allerede en fil med dette navn. Vil du erstatte filen? (J/N) ?");
o=br.readLine();
if (o.equals("N"))
{
dos.flush();
return;
}
}
FileOutputStream fot=new FileOutputStream(f);
int c;
String temp;
do
{
temp=din.readUTF();
c=Integer.parseInt(temp);
if(c!=-1)
{
fot.write(c);
}
}while(c!=-1);
fot.close();
System.out.println(din.readUTF());
}
}
public static void menu() throws Exception
{
while(true)
{
System.out.println("1. Modtag Fil");
System.out.println("2. Afslut");
System.out.print("\nHvad vil du foretage dig? :");
int vm;
vm=Integer.parseInt(br.readLine());
if(vm==1)
{
dos.writeUTF("GET");
Modtag();
}
else
{
dos.writeUTF("DISCONNECT");
System.exit(1);
}
}
}
}
}
答案 0 :(得分:0)
以下是我的建议:
首先,学习如何使用telnet命令进行FTP。您可以在https://www.webdigi.co.uk/blog/2009/ftp-using-raw-commands-and-telnet/
查看此示例您会看到需要两个连接:一个在端口21上,您向其发送FTP命令,第二个连接在PASV命令响应中返回的端口上。第二个连接将为您提供列表或下载的文件。
然后你尝试映射" telnet"连接到" socket"。它们是相似的,因为它们都打开了与特定端口上主机的TCP连接。