无法连接com.sun.proxy。$ Proxy0无法转发到client.Client.main上的client.FTPservice(Client.java:36)

时间:2016-06-24 08:52:45

标签: java ftp rmi

我正在创建我的第一个RMI APP,我遇到了客户端问题。

线程“main”中的异常java.lang.ClassCastException:com.sun.proxy。$ Proxy0无法强制转换为client.FTPservice     在client.Client.main(Client.java:36) 我无法连接服务器

public class Client {
static FTPprotocol protocol = null;
static FTPservice ftpSrv;

public static void main(String[] args) throws IOException {

        protocol = new FTPprotocol();
        protocol.localPath = new java.io.File( "." ).getCanonicalPath();
        Scanner stdin = new Scanner(System.in);
        String cmd = null;
        String output = null;
        String serverOutput = null;

        try { 

            System.out.print(protocol.localPath +"> ");

            while(stdin.hasNextLine()){
                serverOutput = null;
                cmd = stdin.nextLine();

                output = protocol.processInput(cmd, ftpSrv);
                System.out.print(output);

                if(protocol.state == FTPprotocol.ESTABLISHING_CONNECTION){
                    ftpSrv = (FTPservice)Naming.lookup("rmi://"+ protocol.url +"/FTPservice");

                     protocol.serverPath = ftpSrv.open();
                     if(!protocol.serverPath.equals("0")){
                        serverOutput = "You are now connected to the FTP server\n";
                        protocol.state = FTPprotocol.CONNECTION_ESTABLISHED;
                     }else{
                        serverOutput = "You were unhable to connect to the FTP server\n";
                     }
                }
                if(serverOutput != null)
                    System.out.print(serverOutput);
                System.out.print(protocol.localPath +" | " + protocol.serverPath + " > ");

            }

        } 


public class FTPprotocol {
public static final int WAITING = 0;
public static final int ESTABLISHING_CONNECTION = 1;
public static final int CONNECTION_ESTABLISHED = 2;
public static final int SENDINGFILE = 3;

public int state = WAITING;
public String url = null;

public String localPath = null; 
public String serverPath = null; 

private String[] commands = { "open", "lcd", "cd", "ls", "put", "get", "exit", "help" };
private String help =  "1- open <machine> : abre conexão com o servidor myFTP na máquina desejada\n"
                        + "2- lcd <directory> : muda o directório corrente do cliente para directory (o directório tem de existir)\n"
                        + "3- cd <directory> : muda o directório corrente do servidor para directory (o directório tem de existir)\n"
                        + "4- ls : faz listagem do conteudo do directório corrente no servidor\n"
                        + "5- put <filename> : transfere o ficheiro filename para o directório corrente do servidor mantendo o nome\n"
                        + "6- get <filename> : transfere o ficheiro filename para o directório corrente do cliente mantendo o nome\n"
                        + "7- exit : termina a sessão entre o cliente e o servidor\n";
private Scanner stdin;


public String processInput(String input, FTPservice service) throws IOException {
    String cmdTemp = null;
    String output = null;
    String urlT= null;

    if(input == null || input.equals(""))
        return "";

    stdin = new Scanner(input);
            System.out.println("Podaj ip:");
    cmdTemp = stdin.next();
    if(stdin.hasNext())
        urlT = stdin.next();

    boolean isCommand = isCommand(cmdTemp);
    if(!isCommand){
        output= "Type \"help\" to view the list of commands\n";
        return output;
    }
    if(input.equals(commands[commands.length-1])){
        output= help;
        return output;
    }
    if(cmdTemp.equals(commands[6])){
        System.exit(0);
    }
    //lcd
    if(cmdTemp.equals(commands[1])){
        if(urlT.equals("..")){
          String [] fields = localPath.split("/");
        localPath = "";
          for(int i =0; i < fields.length - 1; i++){
              if(!fields[i].equals(""))
                localPath = localPath +"/" + fields[i];
              }
        }
        else{
            File folder = new File(localPath);
            File[] listOfFiles = folder.listFiles(); 

            for (int i = 0; i < listOfFiles.length; i++) {
                 if(listOfFiles[i].isDirectory()){                   
                     if(urlT.equals(listOfFiles[i].getName())){
                        localPath = localPath + "/" + urlT;
                        return "\n";
                     }
                 }
             }
             return "No such file or directory\n";
        }
    }
    //ends lcd
    if (state == WAITING) {
        if(cmdTemp.equals(commands[0])){
            output = "Connecting to "+ urlT + "..\n";   
            url = urlT;
            state = ESTABLISHING_CONNECTION;
        }
        else{
            output = "You need to connect to the server first.. see --help\n";      
            return output;
        }
    } 
    else if( state == CONNECTION_ESTABLISHED){
        // listing in server command
        if(cmdTemp.equals(commands[3])){
                return service.ls(serverPath);
        }
        // muda o directório corrente do servidor para urlT
        else if(cmdTemp.equals(commands[2])){
            String tempServerPath = service.cd(urlT, serverPath);
            if(tempServerPath.equals("NA"))
                 return "No such file or directory\n";
            serverPath = tempServerPath;
        }
        else if(cmdTemp.equals(commands[5])){
            output = get(service, urlT);
        }
        else if(cmdTemp.equals(commands[4])){
            output = put(service, urlT);
        }
    }

    return output;
}

1 个答案:

答案 0 :(得分:-1)

public interface FTPservice  extends Remote {

    public String ls(String serverPath) 
        throws RemoteException; 

    public String cd(String dir, String serverPath) 
        throws java.rmi.RemoteException; 

    public String open() 
        throws RemoteException;

    public boolean put(byte[] buffer, String path) throws IOException;

    byte[] get(String fileName, int offset, int fileSize)
        throws RemoteException;
    public int getFileSize(String string) throws RemoteException;
}