In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use java-RMI to transfer large files. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
Why use RMI?
In this project, for the communication between the client and the server, think of a lot of ways, because it is a rich client application, the technology will be selected between RMI and Java-sockets, in which the flexibility of RMI is not high, the client and server must be written by java, but it is more convenient to use, on the contrary, java-sockets, although more flexible, but need to specify the communication protocol between the server and the client. More troublesome, after several tradeoffs, finally choose RMI for server-client communication
File upload problem
In the process of using java-rmi, we will inevitably encounter a problem of file upload. Since the file stream cannot be transferred in rmi (for example, the method parameter in rmi cannot be FileInputStream, etc.), we have to choose a compromise, that is, we first use FileInputStream to read the file into a Byte array, then pass the Byte array as a parameter to the RMI method, and then restore the Byte array to outputStream on the server side. So you can transfer files through RMI.
This also has the disadvantage that it is impossible to verify the accuracy of the transmitted data.
Let me give you an example to explain.
File structure
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 method is important to convert a file named filename into 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 {/ / filename here should be the address public void upload (String filename, byte [] file) throws RemoteException;} where the file is stored on the server side.
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 ();}} after reading the above, do you have any further understanding of how to use java-RMI for large file transfer? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.