Ftp run()方法的问题
描述:这是基于TCP套接字的FTP程序的FTPClient.java类。我的教授说,除了FTPClient.java类中的run()方法外,我的程序还不错。他说我的run()方法是读取所有内容,而不是只读取特定命令 问题:我应该在这个方法中使用String[]output=something.split(“”)吗?你能帮我找出这个方法的正确代码吗 谢谢大家!Ftp run()方法的问题,ftp,ftp-client,Ftp,Ftp Client,描述:这是基于TCP套接字的FTP程序的FTPClient.java类。我的教授说,除了FTPClient.java类中的run()方法外,我的程序还不错。他说我的run()方法是读取所有内容,而不是只读取特定命令 问题:我应该在这个方法中使用String[]output=something.split(“”)吗?你能帮我找出这个方法的正确代码吗 谢谢大家! package ftp.server; import java.net.*; import java.io.*; public clas
package ftp.server;
import java.net.*;
import java.io.*;
public class FTPClient
{
private Socket socket = null;
private DataInputStream input = null;
private DataOutputStream output = null;
private BufferedReader keyboard = null;
final private String EXIT_CMD = "exit";
final private String PUT_CMD = "put";
final private String GET_CMD = "get";
public FTPClient(Socket socket) throws IOException
{
this.socket = socket;
input = new DataInputStream(socket.getInputStream());
output = new DataOutputStream(socket.getOutputStream());
keyboard = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
}
public void sendFile(String fileName) throws IOException
{
File file = new File(fileName);
if(!file.exists())
{
output.writeUTF("File " + fileName + " does not exist");
}
else
{
output.writeUTF(fileName);
FileInputStream inFile =
new FileInputStream(file);
int ch;
do
{
ch = inFile.read();
output.writeUTF(String.valueOf(ch));
}while(ch != -1);
inFile.close();
}
}
public void receiveFile(String fileName) throws IOException
{
output.writeUTF(fileName);
String msg = input.readUTF();
if(msg.equals("NOT_FOUND"))
{
output.writeUTF("No file in server");
}
else if(msg.equals("READY"))
{
File file = new File(fileName);
FileOutputStream outFile =
new FileOutputStream(file);
int ch;
String line;
do
{
line = input.readUTF();
ch = Integer.parseInt(line);
if(ch != -1)
{
outFile.write(ch);
}
}while(ch != -1);
outFile.close();
}
}
public void run() throws IOException
{
while(true)
{
System.out.println("ftp>");
String cmd = keyboard.readLine();
if(cmd.equals(PUT_CMD))
{
output.writeUTF(PUT_CMD);
sendFile("novel.dat");
}
if(cmd.equals(GET_CMD))
{
output.writeUTF(GET_CMD);
receiveFile("novel.dat");
}
if(cmd.equals(EXIT_CMD))
{
output.writeUTF(EXIT_CMD);
}
}
}
public static void main(String[] args) throws Exception
{
//check parameters
if(args.length != 2)
{
System.out.println("Usage: java FTPClient <host> <port>");
return;
}
//create socket
String host = args[0];
int port = Integer.parseInt(args[1]);
Socket socket = new Socket(host, port);
//run ftp client
FTPClient client = new FTPClient(socket);
client.run();
}
}
包ftp.server;
导入java.net。*;
导入java.io.*;
公共类FTP客户端
{
私有套接字=空;
私有数据输入流输入=null;
私有DataOutputStream输出=null;
专用BufferedReader键盘=空;
最终私有字符串EXIT_CMD=“EXIT”;
最终私有字符串PUT_CMD=“PUT”;
最后一个私有字符串GET_CMD=“GET”;
公共FTPClient(套接字)引发IOException
{
this.socket=socket;
输入=新的DataInputStream(socket.getInputStream());
output=新的DataOutputStream(socket.getOutputStream());
键盘=新的BufferedReader(
新的InputStreamReader(socket.getInputStream());
}
public void sendFile(字符串文件名)引发IOException
{
文件=新文件(文件名);
如果(!file.exists())
{
output.writeUTF(“文件“+文件名+”不存在”);
}
其他的
{
output.writeUTF(文件名);
文件输入流填充=
新文件输入流(文件);
int-ch;
做
{
ch=infle.read();
output.writeUTF(String.valueOf(ch));
}而(ch!=-1);
infle.close();
}
}
public void receiveFile(字符串文件名)引发IOException
{
output.writeUTF(文件名);
字符串msg=input.readUTF();
如果(消息等于(“未找到”))
{
writeUTF(“服务器中没有文件”);
}
else if(消息等于(“就绪”))
{
文件=新文件(文件名);
FileOutputStream输出文件=
新文件输出流(文件);
int-ch;
弦线;
做
{
line=input.readUTF();
ch=整数.parseInt(行);
如果(ch!=-1)
{
输出文件写入(ch);
}
}而(ch!=-1);
outFile.close();
}
}
public void run()引发IOException
{
while(true)
{
System.out.println(“ftp>”);
String cmd=keyboard.readLine();
if(cmd.equals(PUT_cmd))
{
output.writeUTF(PUT_CMD);
sendFile(“novel.dat”);
}
if(cmd.equals(GET_cmd))
{
output.writeUTF(GET_CMD);
接收文件(“novel.dat”);
}
如果(指令等于(退出指令))
{
output.writeUTF(EXIT_CMD);
}
}
}
公共静态void main(字符串[]args)引发异常
{
//检查参数
如果(参数长度!=2)
{
System.out.println(“用法:JavaFTPClient”);
返回;
}
//创建套接字
字符串host=args[0];
int port=Integer.parseInt(args[1]);
套接字=新的套接字(主机、端口);
//运行ftp客户端
FTP客户端=新的FTP客户端(套接字);
client.run();
}
}
这就是我需要做的来修复我的run方法:
public void run() throws IOException
{
while(true)
{
System.out.print("ftp>");
String cmd = keyboard.readLine();
if(cmd.equals(EXIT_CMD))
{
output.writeUTF(EXIT_CMD);
System.exit(0);
}
String[] tokens = cmd.split(" ");
cmd = tokens[0];
String fileName = tokens[1];
if(cmd.equals(PUT_CMD))
{
output.writeUTF(PUT_CMD);
sendFile(fileName);
}
else if(cmd.equals(GET_CMD))
{
output.writeUTF(GET_CMD);
receiveFile(fileName);
}
}
}