0


【Java流式下载大文件,避免OOM内存溢出】

Java下载大文件,如何避免OOM内存溢出

Java下载文件时,如果是小文件的下载,我们一般直接使用工具类的方法,比如cn.hutool.http.HttpUtil.downloadFile()。但是如果是大文件的下载,使用这些工具类的方法,可能会出现Out of Memory内存溢出,它是指需要的内存空间大于系统分配的内存空间,oom后果就是项目程序crash,Hprof Heap Profile 内存快照文件。因此,我们需要自己写下载文件方法,下面提供两种下载方法:

1.BufferedInputStream 缓存流的方式来获取下载文件

使用HttpURLConnection和bufferedInputStream 缓存流的方式来获取下载文件,读取InputStream输入流时,每次读取的大小为5M,不一次性读取完,就可避免内存溢出的情况。

/**
 * BufferedInputStream 缓存流下载文件
 * @param downloadUrl
 * @param path
 */
public static void downloadFile(String downloadUrl, String path){
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        URL url = new URL(downloadUrl);
        //这里没有使用 封装后的ResponseEntity 就是也是因为这里不适合一次性的拿到结果,放不下content,会造成内存溢出
        HttpURLConnection connection =(HttpURLConnection) url.openConnection();

        //使用bufferedInputStream 缓存流的方式来获取下载文件,不然大文件会出现内存溢出的情况
        inputStream = new BufferedInputStream(connection.getInputStream());
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        outputStream = new FileOutputStream(file);
        //这里也很关键每次读取的大小为5M 不一次性读取完
        byte[] buffer = new byte[1024 * 1024 * 5];// 5MB
        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        connection.disconnect();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
    }
}

2.RestTemplate流式处理下载大文件

使用RestTemplate流式处理下载大文件,需要先用RequestCallback定义请求头的接收类型application/octet-stream,然后restTemplate进行请求下载时,对响应进行流式处理而不是将其全部加载到内存中。

/**
 * RestTemplate流式处理下载大文件
 * @param url
 * @param productZipName
 */
public static void download(String url, String productZipName) {
    SSL factory = new SSL();
    RestTemplateConfig config = new RestTemplateConfig();
    RestTemplate restTemplate = config.restTemplate(factory);

    File file = new File(productZipName);
    try {
        //定义请求头的接收类型
        RequestCallback requestCallback = request -> request.getHeaders()
                .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));
        //对响应进行流式处理而不是将其全部加载到内存中
        restTemplate.execute(url, HttpMethod.GET, requestCallback, clientHttpResponse -> {
            Files.copy(clientHttpResponse.getBody(), Paths.get(productZipName));
            return null;
        });
    }catch (Exception e){
        e.printStackTrace();
    }
}
标签: java http spring

本文转载自: https://blog.csdn.net/weixin_39825906/article/details/129355848
版权归原作者 Taltoo 所有, 如有侵权,请联系我们删除。

“【Java流式下载大文件,避免OOM内存溢出】”的评论:

还没有评论