【问题标题】:Java SocketException: socket is closedJava SocketException:套接字已关闭
【发布时间】:2023-03-03 09:40:02
【问题描述】:

我有一个大问题,我不明白为什么会这样。 我的问题是,当我从服务器(.mp4 文件)获取 MediaFiles 并尝试将其保存在本地存储中时,出现异常:套接字已关闭。请帮帮我。

    try {
        RequestHelper requestHelper = new RequestHelper(activity);
        HttpURLConnection conn = requestHelper.connection(url + file, "GET");

        int statuscode = requestHelper.getStatuscode(conn);
        if (statuscode == 200) {
            InputStream is = conn.getInputStream();
            System.out.println("inputStream: " + is);
            OutputStream os = new FileOutputStream(new File(saveDir, file.getName()));
            System.out.println("outputStream: " + os);



            byte[] b = new byte[1024];
            int length;
            
            //HERE IS THE EXCEPTION TRIGGER
            System.out.println("inputstreamRead: " + is.read(b));

            while ((length = is.read(b)) != -1) {
                os.write(b, 0, length);
            }

            os.flush();
            os.close();
            is.close();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

【问题讨论】:

  • 好像对方关闭了socket。

标签: java android-studio inputstream httpurlconnection socketexception


【解决方案1】:

你能检查一下你是否添加了keep-alive标头吗?

【讨论】:

    猜你喜欢
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2014-03-01
    • 1970-01-01
    相关资源
    最近更新 更多