public static Object dispatch(Object dispatchObject)
    {
        String appletURLParameter = "http://192.168.1.1:8080/TestApplet/ServletDispatcher";        Object returnObject = null;
        try
        {
            URL url = new URL(appletURLParameter);
            URLConnection connection = url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);            //本次连接不用Cache
            connection.setUseCaches(false);
            //下次连接不用Cache
            connection.setDefaultUseCaches(false);
            connection.setRequestProperty("Content-Type", "application/octet-stream");            ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
            oos.writeObject(dispatchObject);
            oos.flush();
            oos.close();            ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
            returnObject = ois.readObject();
        }
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
        catch (MalformedURLException ex)
        {
            ex.printStackTrace();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return returnObject;
    }
    我的做法是把一个序列化的对象传递给一个SERVLET,它的名字叫ServletDispatcher,然后在SERVLET里做一系列操作,返回对象给applet,但            ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
            returnObject = ois.readObject();    connection.getInputStream()为什么为unknow resource呢?            ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
            oos.writeObject(dispatchObject);
            oos.flush();
            oos.close();    把上述四行代码注掉就正常了?
  

解决方案 »

  1.   

    你看看不要调用oos.close看看,这个时候不太应该关闭这个流吧
      

  2.   

    是不是代码写的有问题?oos.close注释掉了,还是有问题。
      

  3.   

    应该是你的服务器代码存在问题吧,我试过是可以的            URL url = new URL(appletURLParameter);
                URLConnection connection = url.openConnection();
                connection.setDoInput(true);
                connection.setDoOutput(true);            //本次连接不用Cache
                connection.setUseCaches(false);
                //下次连接不用Cache
                connection.setDefaultUseCaches(false);
                connection.setRequestProperty("Content-Type", "application/octet-stream");            ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
                oos.writeObject(new Integer(3));
                oos.flush();            ObjectInputStream ois = new ObjectInputStream(connection.getInputStream());
                System.out.println(ois.readObject());--------------------------
    ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
    System.out.println(ois.readObject());
    ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
    oos.writeObject(new Integer(333));
    oos.flush();------------
    服务器可以打印3,客户端会打印333
      

  4.   

    我在服务端DEBUG了,断点根本不能到达那里。错误是从客户端那里抛出来的。ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());只要这段代码存在,就有错误;这段代码注释掉,可以到达服务端的SERVLET。