你是在使用http连接读写串行化对象(http隧道)。
但我不明白,你为什么要在发送的时候
“发送到一个字节数组,去字节数组的长,设置发送字节的长。
将字节数组发送出去。servlet端以对象流接收。”
??
在接受端既然要用ObjectInputStream.readObject(),为什么
不再发送的时候用ObjectOutputStream.writeObject()?
我想问题就出在这里,对象串行化后是有特定格式的,直接发送字符串
是不行的。试试上面说的吧

解决方案 »

  1.   

    唉,
    可能是你没有在Servlet端把输入输出流设成同一个
    所以不能读出数据,
    也可能是你使用了缓冲区,而在发送的时候没有
    connection.setUseCashse(false)//禁止使用缓冲区
    调用其flush()方法
      

  2.   

    我是同过一个String对象,从客户端发送,
    servlet将其颠倒顺序,返回,客户端接受例如如果客户端发送:“women”
    发送后接受到:“nemow”,则表示通道没有问题。
    在applet和servlet:都使用了writeobject();readobject()
    同时我也使用了ggyy_csdn的方法flush()
    不知是为什么?
      

  3.   

    我将程序贴出吧,他们都是基础类。可以调用检测通道是否工作正常
    同时希望朋友们查错
    clientbase:
    package servlets;
    import  java.io. *;
    import  java.net.*;
    /**
     * <p>Title: servlets</p>
     * <p>Description: datasecurity</p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: 2-519</p>
     * @author kworld
     * @version 1.0
     */
    public abstract class clientbase {  public clientbase() {
      }
      private  static String servlet=null;
    public static URLConnection getconnection() throws IOException
    {
    URL u=new URL(servlet);
    URLConnection con=u.openConnection();
    con.setDoInput(true);
    con.setDoOutput(true);
    con.setUseCaches(false);
    con.setRequestProperty("Content-type","application/octet-stream");
    con.setAllowUserInteraction(false);
    return con;
    }
    public static void sendobject(URLConnection con,Object obj)throws IOException
    {
    try{
            ObjectOutputStream out=new ObjectOutputStream(con.getOutputStream());
    if(obj!=null)
    {
    out.writeObject(obj);out.close();
    }}catch(Exception ex)
            {System.out .println("error happen in sendobject "+ex.toString() );
            }}
    public static Object receiveobject(URLConnection con)throws Exception
    {
    ObjectInputStream in=new ObjectInputStream(con.getInputStream());
    Object obj=in.readObject();
            in.close();
    return obj;}
    public static void setservlet(String ur)
    {
        servlet=ur;
    }
    public static Object send(Object sobj)
    {
    try{
    URLConnection con=getconnection();
         sendobject(con,sobj);
            sobj=receiveobject(con);
     }
    catch(Exception ex)
    {System.out.println("error happened: "+ex.toString());
    return null;
    }
    return sobj;
    }
    }serverbase
    package servlets;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    /**
     * <p>Title: servlets</p>
     * <p>Description: datasecurity</p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: 2-519</p>
     * @author kworld
     * @version 1.0
     */public abstract class servbase extends HttpServlet implements SingleThreadModel {
    public void servbase() {
      }
      private static final String CONTENT_TYPE = "text/html; charset=GBK";
      //Initialize global variables
       //Process the HTTP Post request
         public static Object getobjectfromclient(HttpServletRequest req)throws Exception
    {
    ObjectInputStream in=new ObjectInputStream(req.getInputStream());
    Object obj=in.readObject();
    in.close();
    return obj;
    }
            public static void sendobjecttoclient(HttpServletResponse resp,Object obj)
    throws IOException
          {
             ObjectOutputStream out=new ObjectOutputStream(resp.getOutputStream());
              out.writeObject(obj);
              out.flush() ;
                    out.close();
            }
            public static Object doobject(Object nobj)
            {
            return null;
             }
           public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>runserver</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a GET. This is the reply.</p>");
        out.println("</body></html>");
      }
            public void doPost(HttpServletRequest req,HttpServletResponse  resp) throws ServletException, IOException
     {
          resp.setContentType(CONTENT_TYPE);
            PrintWriter out = resp.getWriter();
     out.println("<html>");
           out.println("<head><title>Serv</title></head>");
        out.println("<body>");
        out.println("<p>The servlet has received a POST. This is the reply.</p>");
        out.println("</body></html>");
            try
             {
             Object obj=getobjectfromclient(req);
             obj=doobject(obj);
             sendobjecttoclient(resp,obj);
             }
             catch(Exception e)
             {
             System.out.println("error: "+e.toString());
             }
            }
      //Clean up resources
     /* public void destroy() {
      }*/
    }