• 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();
                    }
                }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    客户端

    /**
         * 从网页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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
  • 相关阅读:
    #力扣:1920. 基于排列构建数组@FDDLC
    【数据结构】栈与队列
    在DevExpress的GridView的列中,动态创建列的时候,绑定不同的编辑处理控件
    合肥工业大学内容安全实验一:爬虫|爬新闻文本
    七分钟,数据转换器get到了
    02-Java流程控制
    无代码开发平台子管理员入门教程
    【Vue】具名插槽
    年薪百万,一夜归零!程序员,过了 35 岁,你还能做什么?
    基于SpringBoot的网上购物商场管理系统
  • 原文地址:https://blog.csdn.net/oLengYueHun/article/details/126461716