FileInputStream
即使我SimpleRemoteInputStream
从RMIIO 库中包装它,我也无法发送到 RMI 服务器。这是我尝试运行客户端应用程序时得到的:
Client started
SENDING FILE: file.pdf
paź 29, 2013 1:13:24 PM com.healthmarketscience.rmiio.exporter.RemoteStreamExporter getInstance
INFO: Using stream exporter com.healthmarketscience.rmiio.exporter.DefaultRemoteStreamExporter
这是客户端应用程序:
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.Remote;
import com.healthmarketscience.rmiio.SimpleRemoteInputStream;
import pl.opa.backuper_common.FileServer;
public class TestClient {
public static void main(String[] args) {
System.setSecurityManager(new RMISecurityManager());
String fileName = "file.pdf";
System.out.println("Client started");
try {
Remote remote = Naming.lookup("FileServer");
FileServer server = null;
if(remote instanceof FileServer)
server = (FileServer) remote;
System.out.println("SENDING FILE: " + fileName);
SimpleRemoteInputStream istream = new SimpleRemoteInputStream(new FileInputStream(fileName));
server.sendFile(istream.export());
} catch(Exception e) {
e.printStackTrace();
}
}
}
服务器方法实现:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import com.healthmarketscience.rmiio.RemoteInputStream;
import com.healthmarketscience.rmiio.RemoteInputStreamClient;
import pl.opa.backuper_common.FileServer;
public class FileServerImpl extends UnicastRemoteObject implements FileServer {
private static final long serialVersionUID = 1L;
protected FileServerImpl() throws RemoteException {
super();
}
@Override
public void sendFile(RemoteInputStream data) throws IOException, RemoteException {
InputStream input = null;
try {
input = RemoteInputStreamClient.wrap(data);
writeToFile(input);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (input != null) {
input.close();
}
}
}
private void writeToFile(InputStream stream) throws IOException {
FileOutputStream output = null;
try {
File file = File.createTempFile("data", ".dat");
output = new FileOutputStream(file);
int chunk = 4096;
byte [] result = new byte[chunk];
int readBytes = 0;
do {
readBytes = stream.read(result);
if (readBytes >= 0)
output.write(result, 0, readBytes);
} while(readBytes != -1);
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
if(output != null)
output.close();
}
}
}