Java Transfer large file sample with RMI sharing _java

Source: Internet
Author: User
Tags file upload

Why should I use RMI?
In this project, for the communication between the client and the server, think a lot of ways, because do is rich client application, the final technology selected in the RMI and Java-sockets two, which RMI flexibility is not high, both client and server must be written Java, but the use of more convenient , Java-sockets, although more flexible, but need to specify their own server-side and client communication protocol. More trouble, after weighing, and ultimately choose RMI to do server-client communication

File Upload problem
In the process of using Java-rmi, you will inevitably encounter a file upload problem, because in the RMI can not transfer file flow (such as the RMI method parameters can not be fileinputstream and so on), then we have to choose a compromise approach, is to read the file in a byte array first, then pass the byte array as a parameter into the RMI method, and then restore the byte array to outputstream on the server side, so that the file can be transferred via RMI FileInputStream

This is also a disadvantage, that is, can not test the accuracy of the data transmitted over.

Here's an example to explain

Fileclient

Copy Code code as follows:

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 more important by using this method to convert 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 of the file stored on the server side
public void upload (String filename, byte[] file) throws RemoteException;

}

Filedataservice_imp

Copy Code code as follows:

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

Copy Code code as follows:

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 ();

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.