在路上的咸鱼 2016-10-23 12:41 采纳率: 0%
浏览 869

使用对象流与servlet传递对象抛出异常

想要向服务器传一个对象过去,服务器再强转回对象。中间用的是对象流。在服务器用的是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)

单步调试发现是在本地创建输出流没问题,创建输入流时报错。。。
这是什么原因?
求解。图片说明图片说明图片说明图片说明图片说明图片说明

  • 写回答

1条回答 默认 最新

  • threenewbee 2016-10-23 12:50
    关注

    文件没有找到
    temp是什么东西

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记