2016-06-24 22 views
-1

Ich erstelle meine erste RMI-APP und habe Probleme mit dem Client.

Exception in thread "main" java.lang.ClassCastException. Com.sun.proxy $ Proxy0 kann nicht auf client.FTPservice bei client.Client.main (Client.java:36) gegossen werden Ich kann keine Verbindung mit trennen

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; 
} 
+0

Was ist 'FTPService'? – EJP

+0

Es ist Schnittstelle – labamba63

+0

Und welche Remote-Schnittstelle implementiert das Remote-Objekt? Einschließlich seines Paketnamens? – EJP

Antwort

-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; 
} 
+0

Was ist das? Sicher keine Antwort. – EJP

+0

Bitte bearbeiten Sie mit mehr Informationen. Code-only und "try this" Antworten werden abgeraten, da sie keine durchsuchbaren Inhalte enthalten und nicht erklären, warum jemand "das versuchen sollte". Wir bemühen uns, eine Ressource für Wissen zu sein. – abarisone