本文主要是介绍文件下载出现 IOExpcetion: closed,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
异常原因截图 :
异常代码位置 :
出现的原因是 使用 try-with-resources, downloadFile 方法执行完毕, 文件流被关闭了...导致前面读取文件字节, 异常提示已经关闭...
try (Response response = OkHttpUtils.getInstance().client.newCall(new Request.Builder().url(fileUrl).build()).execute()) {
正确的写法是 :
不使用 try-with-resources, 使用常规写法, 所以说 try-with-resources 这个语法虽然方便, 但使用起来也要注意代码上下文
/*** 根据资源网络url下载文件** @param fileUrl* @return*/public static InputStream downloadFile(String fileUrl) {try {Response response = OkHttpUtils.getInstance().client.newCall(new Request.Builder().url(fileUrl).build()).execute();Assert.notNull(response, "http request error");Assert.isTrue(response.isSuccessful(), "http request not successful");ResponseBody body = response.body();Assert.notNull(body, "http request error");return body.byteStream();} catch (Exception e) {log.error("http download file error, ", e);throw new RuntimeException("http error.");}}
附上 OkHttpUtils 工具类, 单例实现的
package com.ckx.pricing.third.common.util;import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.util.Assert;import java.io.InputStream;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;/*** @author yukun.yan* @description OkHttpUtils* @date 2023/5/29 11:38*/
@Slf4j
public class OkHttpUtils {private final OkHttpClient client;private static final ObjectMapper objectMapper = new ObjectMapper();public OkHttpUtils() {client = new OkHttpClient.Builder().protocols(Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1)).connectTimeout(60, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).connectionPool(new ConnectionPool(10, 20, TimeUnit.MINUTES)).build();}/*** 执行一个http请求, 返回 响应数据** @param request* @return*/public static String newCall(Request request) {if (request == null) {throw new IllegalArgumentException("Request cannot be null");}try (Response response = OkHttpUtils.getInstance().client.newCall(request).execute()) {Assert.notNull(response, "http request error");Assert.isTrue(response.isSuccessful(), "http request not successful");ResponseBody body = response.body();Assert.notNull(body, "http request error");return body.string();} catch (Exception e) {throw new RuntimeException("http error.");}}/*** 入参解析为请求入参** @param object* @param <T>* @return*/public static <T> RequestBody toPostRequestBody(T object) {ObjectMapper objectMapper = new ObjectMapper();String jsonRequestBody = null;try {jsonRequestBody = objectMapper.writeValueAsString(object);} catch (JsonProcessingException e) {log.error("request json error, ", e);}Assert.notNull(jsonRequestBody, "request not be null");MediaType JSON = MediaType.get("application/json; charset=utf-8");return RequestBody.create(JSON, jsonRequestBody);}/*** 对网络请求返回值解析, 解密标椎json格式** @param jsonResp* @param tClass* @param <T>* @return*/public static <T> T readResponseValue(String jsonResp, Class<T> tClass) {try {return objectMapper.readValue(jsonResp, tClass);} catch (JsonProcessingException e) {throw new RuntimeException("response to json error");}}/*** 根据资源网络url下载文件** @param fileUrl* @return*/public static InputStream downloadFile(String fileUrl) {try {Response response = OkHttpUtils.getInstance().client.newCall(new Request.Builder().url(fileUrl).build()).execute();Assert.notNull(response, "http request error");Assert.isTrue(response.isSuccessful(), "http request not successful");ResponseBody body = response.body();Assert.notNull(body, "http request error");return body.byteStream();} catch (Exception e) {log.error("http download file error, ", e);throw new RuntimeException("http error.");}}private static class OkHttpUtilsInstanceOwner {private static final OkHttpUtils INSTANCE = new OkHttpUtils();}/*** 返回该类的单例对象, 并自动初始化构造器** @return*/public static OkHttpUtils getInstance() {return OkHttpUtilsInstanceOwner.INSTANCE;}}
这篇关于文件下载出现 IOExpcetion: closed的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!