java 사용rmi 전송 큰 파일 예시 공유

왜 RMI를 사용해야 하는지 이번 프로젝트에서 클라이언트와 서버 간의 통신에 대해 많은 방법을 생각했다. 부유한 클라이언트 응용이기 때문에 최종적으로 기술을 RMI와 자바-sockets 두 가지 사이에 선정했다. 그 중에서 RMI의 유연성이 높지 않다. 클라이언트와 서비스 기기는 모두 자바가 작성해야 하지만 사용이 비교적 편리하다. 반면java-sockets는 비교적 유연하지만그러나 서버 측과 클라이언트 간의 통신 프로토콜을 스스로 규정해야 한다.서버 - 클라이언트 통신을 위해 RMI를 선택하는 것이 번거롭습니다.
파일 업로드 문제는java-rmi를 사용하는 과정에서 파일 업로드 문제가 발생할 수 있습니다. rmi에서 파일 흐름을 전송할 수 없기 때문입니다. (예를 들어 rmi의 방법 매개 변수는 File InputStream 같은 것이 될 수 없습니다.) 그러면 우리는 절충된 방법을 선택할 수 밖에 없습니다. 먼저 File InputStream으로 파일을 하나의 Byte 수조에 읽은 다음에 이 Byte 수조를 매개 변수로 RMI에 전송하는 방법입니다.그리고 서버에서 Byte 그룹을 outputStream으로 복원하면 RMI를 통해 파일을 전송할 수 있습니다
이렇게 하는 것도 단점이 있다. 전송된 데이터의 정확성을 검증할 수 없다는 것이다.
다음은 제가 실례를 하나 설명해 드릴게요.
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();
            }
        }
    // , filename byte
        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{

        // filename
        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();
            }

     
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
            new FileServer();

        }

    }
   

좋은 웹페이지 즐겨찾기