NIO下载超大文件(支持20个G)

服务端

/**
     * nio将文件流写入response
     * @author: zhanghp2017he@foxmail.com
     * @date: 2022/8/22
     * @param: [response]
     * @return: void
     * @exception:
     */
    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
            OutputStream os = null;
            try {
                File file = new File("D:\\bao\\14个g.zip");

                // 取得输出流
                os = response.getOutputStream();
                String contentType = Files.probeContentType(Paths.get(file.getAbsolutePath()));
                response.setHeader("Content-Type", contentType);
                response.setHeader("Content-Disposition", "attachment;filename="+ new String(file.getName().getBytes("utf-8"),"ISO8859-1"));
                FileInputStream fileInputStream = new FileInputStream(file);
                WritableByteChannel writableByteChannel = Channels.newChannel(os);
                FileChannel fileChannel = fileInputStream.getChannel();

                ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 20);
                while (true) {
                    buffer.clear();
                    int flag = fileChannel.read(buffer);
                    if (flag == -1) {
                        break;
                    }
                    buffer.flip();
                    writableByteChannel.write(buffer);
                }

                fileChannel.close();
                os.flush();
                writableByteChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //文件的关闭放在finally中
            finally {
                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    }

客户端

/**
     * 从网页url下载大文件
     * @author: zhanghp2017he@foxmail.com
     * @date: 2022/8/22
     * @param: [urlStr, file]
     * @return: void
     * @exception:
     */
    public static void downloadWithNIO(String urlStr, String file) throws IOException {
        URL url = new URL(urlStr);
        ReadableByteChannel rbc = Channels.newChannel(url.openStream());
        FileOutputStream fos = new FileOutputStream(file);
//        fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

        BufferedOutputStream buffOS = new BufferedOutputStream(fos);
        ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024 * 20);
        while (true) {
            buffer.clear();
            int flag = rbc.read(buffer);
            if (flag == -1) {
                break;
            }
            buffer.flip();
            FileChannel fcout = fos.getChannel();
            fcout.write(buffer);
        }
        buffOS.flush();
        buffOS.close();

        fos.flush();
        fos.close();
        rbc.close();
    }
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值