Java USES rmi to transfer large file example shares

  • 2020-04-01 02:42:15
  • OfStack

Why use RMI​
In this project, for communications between the client and the server, want to the many ways, as do the rich client application, will eventually be selected in the RMI technology and Java - sockets, between two kinds of the flexibility of the RMI is not high, the client and server must be Java to write, but the use of more convenient, in contrast, Java - sockets, proved to be more flexible, but the need to rule of communication protocols between client and server. More troublesome, after several tradeoffs, the final choice is RMI for server-client communication

File upload problem
In the process of using Java rmi -, will inevitably encounter the problem of a file upload, because cannot transfer files in the rmi flow (such as a method parameter of rmi cannot FileInputStream), so we had to choose a compromise, is to use FileInputStream first file read into a Byte array, and then put the Byte array as a parameter to pass into the rmi method, and then on the server side will revert to Byte array outputStream, This allows you to transfer files over RMI

This has the disadvantage of not being able to verify the accuracy of the data being transmitted.

Let me give you an example

FileClient


package rmiupload; 

    import java.io.BufferedInputStream; 
    import java.io.File; 
    import java.io.FileInputStream; 
    import java.io.FileNotFoundException; 
    import java.io.IOException; 
    import java.net.MalformedURLException; 
    import java.rmi.Naming; 
    import java.rmi.NotBoundException; 
    import java.rmi.RemoteException; 

    public class FileClient { 

        public FileClient() { 
            // TODO Auto-generated constructor stub 
        } 

        public static void main(String[] args) { 
            try { 
                FileDataService fileDataService = (FileDataService) Naming.lookup("rmi://localhost:9001/FileDataService"); 
                fileDataService.upload("/Users/NeverDie/Documents/test.mp4", new FileClient().fileToByte("/Users/NeverDie/Music/test.mp4")); 
            } catch (MalformedURLException | RemoteException | NotBoundException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
        } 
    //This is an important method that converts a file named filename to a byte array
        private byte[] fileToByte(String filename){ 
            byte[] b = null; 
            try { 
                File file = new File(filename); 
                b = new byte[(int) file.length()]; 
                BufferedInputStream is = new BufferedInputStream(new FileInputStream(file)); 
                is.read(b); 
            } catch (FileNotFoundException e) { 
            // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
            return b; 
        } 
    } 
FileDataService
package rmiupload; 

    import java.net.URL; 
    import java.rmi.Remote; 
    import java.rmi.RemoteException; 

    public interface FileDataService extends Remote{ 

        //The filename here should be the address where the file is stored on the server side
        public void upload(String filename, byte[] file) throws RemoteException; 

    } 

FileDataService_imp


package rmiupload; 

    import java.io.BufferedOutputStream; 
    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileOutputStream; 
    import java.io.IOException; 
    import java.net.URL; 
    import java.rmi.RemoteException; 
    import java.rmi.server.RMIClientSocketFactory; 
    import java.rmi.server.RMIServerSocketFactory; 
    import java.rmi.server.UnicastRemoteObject; 

    public class FileDataService_imp extends UnicastRemoteObject implements FileDataService{ 

        public FileDataService_imp() throws RemoteException { 

        } 

        @Override 
        public void upload(String filename, byte[] fileContent) throws RemoteException{ 
            File file = new File(filename); 
            try { 
                if (!file.exists()) 
                    file.createNewFile(); 
                BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file)); 
                os.write(fileContent); 
            } catch (FileNotFoundException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (IOException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 

    ;   } 

    } 

FileServer


package rmiupload; 

    import java.net.MalformedURLException; 
    import java.rmi.Naming; 
    import java.rmi.RemoteException; 
    import java.rmi.registry.LocateRegistry; 

    public class FileServer { 

        FileDataService fileDataService; 

        public FileServer() { 
            try { 
                fileDataService = new FileDataService_imp(); 
                LocateRegistry.createRegistry(9001); 
                Naming.rebind("rmi://localhost:9001/FileDataService", fileDataService); 
            } catch (RemoteException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } catch (MalformedURLException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 

      
        } 

         
        public static void main(String[] args) { 
            new FileServer(); 

        } 

    } 
    


Related articles: