将序列化对象作为参数传递给Java中的HTTPServer

时间:2013-06-03 03:56:38

标签: java web-services httpserver

如何将序列化对象作为GET请求中的参数传递给HTTP服务器?请让我知道,因为我一直无法找到相同的方法。

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(bos);
    oos.writeObject(obj);
    oos.close();
    String prm = URLEncoder.encode(new String(bos.toByteArray(), "ISO-8859-1"), "ISO-8859-1");

在服务器端:

servletRequest.setCharacterEncoding("ISO-8859-1");
String s = servletRequeset.getParameter("obj");
byte[] bytes = s.getBytes("ISO-8859-1");
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
Object obj = ois.readObject();

也可以使用Base64代替URL编码,主要思想是在URL中传递序列化字节

相关问题