想要向服务器传一个对象过去,服务器再强转回对象。中间用的是对象流。在服务器用的是servlet类建立连接。
之前没怎么接触过servlet...问题出在哪里?
下面是客户端关键代码:
public class TempUtil {
public static void upload(Temp temp) throws Exception {
URL url = new URL("http://localhost:8080/Weather_Server/servlet/Servlet_pt");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();
ObjectOutputStream oos = new ObjectOutputStream(connection.getOutputStream());
oos.writeObject(temp);
oos.close();
ObjectInputStream ois=new ObjectInputStream(connection.getIutputStream());
//其他代码
connection.disconnect();
}
下面是服务器端关键代码:
public class Servlet_pt extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response){
try {
ObjectInputStream ois=new ObjectInputStream(request.getInputStream());
Object oj=ois.readObject();
ObjectOutputStream oos=new ObjectOutputStream(response.getOutputStream());
Temp temp=new Temp();
temp=(Temp)oj;
//其他代码....
oos.writeObject(i);// i是整数包装类对象
ois.close();
oos.close();
}catch(Exception e){
}
报错信息如下:
java.io.FileNotFoundException: http://localhost:8080/Weather_Server/servlet/Servlet_pt
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at local.TempUtil.download(TempUtil.java:40)
at local.Test.main(Test.java:13)
单步调试发现是在本地创建输出流没问题,创建输入流时报错。。。
这是什么原因?
求解。