如何使用 HttpURLConnection 将序列化对象从 Java 类发送到 Servlet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12798954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use HttpURLConnection to send serialized object to a Servlet from Java class?
提问by Rajath
I want to send a serialized object from a Java class to a servlet where the servlet should retrieve and save the object as a file. I'm aware that I have to use HttpURLConnection to make a POST request to a servlet, but I don't know whether the below code is correct.
我想将序列化对象从 Java 类发送到 servlet,servlet 应在其中检索对象并将其保存为文件。我知道我必须使用 HttpURLConnection 向 servlet 发出 POST 请求,但我不知道下面的代码是否正确。
private static HttpURLConnection urlCon;
private static ObjectOutputStream out;
public static void main(String[] args) {
Names names = new Names();
names.setName("ABC");
names.setPlace("Bangalore");
URL url;
try {
url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet");
try {
out = (ObjectOutputStream) urlCon.getOutputStream();
out.writeObject(names);
urlCon = (HttpURLConnection) url.openConnection();
urlCon.setRequestMethod("POST");
urlCon.setDoOutput(true);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
And in the servlet, I have the following code:
在 servlet 中,我有以下代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
try {
names = (Names) in.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in.close();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names"));
out.writeObject(names);
out.close();
}
What should I do to make it work? Also, I want the servlet to send back the object it receives as response.
我该怎么做才能让它发挥作用?此外,我希望 servlet 将它接收到的对象作为响应发回。
Any help would be appreciated. Thanks!
任何帮助,将不胜感激。谢谢!
采纳答案by Vikdor
You would need to
你需要
- Make sure the
Names
class implementsjava.io.Serializable
marker interface. Create an ObjectOutputStream from the servlet's outputstream as follows:
out = new ObjectOutputStream(urlCon.getOutputStream());
On the receiver side, once you read the object from the servlet's inputstream and persist in the file, write it back to the response's output stream as follows:
out = new ObjectOutputStream(response.getOutputStream()); out.writeObject(names); out.close();
- 确保
Names
该类实现了java.io.Serializable
标记接口。 从 servlet 的输出流创建一个 ObjectOutputStream,如下所示:
out = new ObjectOutputStream(urlCon.getOutputStream());
在接收方,一旦您从 servlet 的输入流中读取对象并将其保存在文件中,就将其写回响应的输出流,如下所示:
out = new ObjectOutputStream(response.getOutputStream()); out.writeObject(names); out.close();
On the sender's side:
在发送方:
Names names = new Names();
names.setName("ABC");
names.setPlace("Bangalore");
URL url;
try {
url = new URL("http://localhost:6080/HttpClientSerializable/HttpPostServlet");
urlCon = (HttpURLConnection) url.openConnection();
urlCon.setDoOutput(true); // to be able to write.
urlCon.setDoInput(true); // to be able to read.
out = new ObjectOutputStream(urlCon.getOutputStream());
out.writeObject(names);
out.close();
ObjectInputStream ois = new ObjectInputStream(urlCon.getInputStream());
names = (Names) ois.readObject();
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (MalformedURLException e1) {
e1.printStackTrace();
}
On the receiver's side:
在接收方:
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
try {
names = (Names) in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
in.close();
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream("C:/Documents and Settings/RAGASTH/Desktop/Names"));
out.writeObject(names);
out.close();
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream());
oos.writeObject(names);
oos.close();