我有个大问题,我不明白为什么会这样。我的问题是,当我从服务器获取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();
}
1条答案
按热度按时间bt1cpqcv1#
你能检查一下你是否添加了keep-alive标题吗?