在JDK 的RMI中我有个方法需要传参数,参数为InputStream,在远程调用的的时候报错,
这是我客户端代码
[code="java"]
try {
ICheckServer rmi = (ICheckServer) Naming.lookup("rmi://127.0.0.1:2012/checkserver");
InputStream in = new Thread().getContextClassLoader().getResourceAsStream("jolt-config.xml");
rmi.initTuxedoConnect(in);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
[/code]
错误信息:
java.rmi.MarshalException: error marshalling arguments; nested exception is:
java.io.NotSerializableException: java.io.BufferedInputStream
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:138)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at $Proxy0.initTuxedoConnect(Unknown Source)
at test.Test.main(Test.java:18)
Caused by: java.io.NotSerializableException: java.io.BufferedInputStream
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at sun.rmi.server.UnicastRef.marshalValue(UnicastRef.java:274)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:133)
楼上哥们说的都对
在实际工作中,由于io stream 不支持序列化功能,所以不能直接传递到客户端
送一个类
[code="java"]
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
@SuppressWarnings("serial")
public class DataSet implements Serializable{
private transient InputStream is;
public DataSet(InputStream is){
this.is = is;
}
public InputStream getInputStream(){
return is;
}
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
int c = -1;
byte[] buff = new byte[1024];
while((c = is.read(buff, 0, 1024)) != -1){
out.write(buff, 0, c);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int c = -1;
byte[] buf = new byte[1024];
while((c = in.read(buf, 0, 1024)) != -1){
bos.write(buf, 0, c);
}
//recreate the input stream here.
is = new ByteArrayInputStream(bos.toByteArray());
}
}
[/code]
未序列化的对象是不能通过RMI传递的,事实上你自己想一下stream也是不能远程传递的,比如你本地打开一个文件,你传到远程接口上,远程接口怎么可能打开你本地的文件呢?
InputStream 没有实现java.io.Serializable 不可通过RMI 传输
可以读取到byte[] 直接传byte[]数组 或者直接传String