Описание: Это класс FTPClient.java для FTP-программы на основе сокета TCP. Мой профессор сказал, что моя программа была хорошей, кроме метода run() в классе FTPClient.java. Он сказал, что мой метод run() читает все, просто прочитав конкретную команду.Проблемы с методом run()
Вопрос: Должен ли я использовать String [] output = something.split ("") в этом методе? Не могли бы вы помочь мне выяснить правильный код для этого метода?
Спасибо!
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();
}
}