java socketexception:套接字已关闭

svujldwt  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(405)

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

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();
    }
bt1cpqcv

bt1cpqcv1#

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

相关问题