下载文件,解决oom问题 springboot

背景:一次性将几十兆几百兆的文件读到内存里,然后再传给用户,服务器就爆了。

解决原则:读一点传一点。

解决方法:利用流,循环读写。

使用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);
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

globalcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值