java 사용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();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.