在httpclient中使用Serializable接口报错

public static void main(String[] args) throws IOException, ClassNotFoundException
{
String url = "http://localhost:8080/RmiJetty/test";

    Scanner sc1 = new Scanner(System.in);

    String queryString1 = "num1 = 1";
    String queryString2 = "num2 = 1";

    String inputObj = sc1.next();

    Serializable s = getObjFromServer(url, queryString1,queryString2, inputObj);

    System.out.println(s.toString());
}

/**
 * @param url
 * @param queryString 类似a=b&c=d 形式的参数
 * 
 * @param inputObj   发送到服务器的对象。
 *     
 * @return 服务器返回到客户端的对象。
 * @throws IOException
 */
public static Serializable getObjFromServer(String url, String queryString1,String queryString2,Serializable inputObj) throws IOException
{
    HttpClient client = new HttpClient();

    PostMethod post = new PostMethod(url);

    post.setQueryString(queryString1);
    post.setQueryString(queryString2);

    post.setRequestHeader("Content-Type", "application/octet-stream");

    java.io.ByteArrayOutputStream bOut = new java.io.ByteArrayOutputStream(1024);
    java.io.ByteArrayInputStream bInput = null;
    java.io.ObjectOutputStream out = null;
    Serializable returnObj = null;
    try
    {
        out = new java.io.ObjectOutputStream(bOut);

        out.writeObject(inputObj);

        out.flush();
        out.close();

        out = null;

        bInput = new java.io.ByteArrayInputStream(bOut.toByteArray());

        RequestEntity re = new InputStreamRequestEntity(bInput);
        post.setRequestEntity(re);

        client.executeMethod(post);

        java.io.InputStream in = post.getResponseBodyAsStream();
        java.io.ObjectInputStream oInput = new java.io.ObjectInputStream(in);
        returnObj = (Serializable) oInput.readObject();
        oInput.close();
        oInput = null;
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {
        if (out != null)
        {
            out.close();
            out = null;

        }

        if (bInput != null)
        {
            bInput.close();
            bInput = null;

        }
        //释放连接
        post.releaseConnection();
    }

    return returnObj;

}

下面是servlet:
public class servlet1 extends HttpServlet{

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
     String strnum1 = request.getParameter("num1");
     String strnum2 = request.getParameter("num2");
     //String strnum3 = request.getParameter("inputObj");
     java.io.ObjectInputStream oi = null;
     java.io.ObjectOutputStream ot = null;


     String url="rmi://127.0.0.1/Hello";
      //hello表示为一个token,类似秘钥
    try {
      NumServer  server = (NumServer)Naming.lookup(url);
      oi = new java.io.ObjectInputStream(request.getInputStream());

      Object o = oi.readObject();

      oi.close();
      oi = null;
      //int result = Integer.parseInt(strnum1) + Integer.parseInt(o.toString());
      int a = Integer.parseInt(strnum1);
      int b = Integer.parseInt(strnum2);
      int result = server.num(a,b,o.toString());

      ot = new java.io.ObjectOutputStream(response.getOutputStream());

      ot.writeObject(result);
      ot.flush();

      ot.close();
      ot = null;


      PrintWriter pw = response.getWriter();
      pw.println(result);
    }catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (ClassNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NotBoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally
    {
        try
        {
            if (oi != null)
            {

                oi.close();
                oi = null;
            }
            if (ot != null)
            {
                ot.close();
                ot = null;
            }

        }

        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

}

}
}
这是报的错:
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2281)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2750)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:780)
at java.io.ObjectInputStream.(ObjectInputStream.java:280)
at RmiClient.HttpClientTest1.getObjFromServer(HttpClientTest1.java:79)
at RmiClient.HttpClientTest1.main(HttpClientTest1.java:31)
Exception in thread "main" java.lang.NullPointerException
at RmiClient.HttpClientTest1.main(HttpClientTest1.java:33)
求大神帮忙啊

1个回答

main函数里有空值。你看看这几行代码有没有空值。
String inputObj = sc1.next();

Serializable s = getObjFromServer(url, queryString1,queryString2, inputObj);

System.out.println(s.toString());
bulusli3
lhl_lqc 回复爱吃烧麦的管: 不要感觉,你调试一下试试。主要是inputObj, s这两个值
5 年多之前 回复
u010011940
爱吃烧麦的管 感觉没有啊
5 年多之前 回复
u010011940
爱吃烧麦的管 感觉没有啊
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐