iteye_17867 2012-09-10 09:10
浏览 299
已采纳

RMI中参数为Input出现的问题

在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)

  • 写回答

3条回答 默认 最新

  • iteye_5246 2012-09-10 11:28
    关注

    楼上哥们说的都对
    在实际工作中,由于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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题