怎么使用ByteArrayOutputStream下载文件

发布时间:2021-12-10 16:45:53 作者:iii
来源:亿速云 阅读:325

本篇内容主要讲解“怎么使用ByteArrayOutputStream下载文件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用ByteArrayOutputStream下载文件”吧!

使用ByteArrayOutputStream下载文件

//文件名称
String filepath = ServletActionContext.getServletContext()
        .getRealPath(farmerQrCode.getQrCodeUrl());
        File file = new File(filepath);
        String fileName = new Date().getTime()+".png";
//设置请求信息
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType(response.getContentType());
        response.setHeader("Content-disposition",
                "attachment; filename="+fileName);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int len = 0;
        FileInputStream inputStream = new FileInputStream(file);
        byte [] buffer  = new byte[3];
        while((len = inputStream.read(buffer)) != -1)
        {
            baos.write(buffer, 0,  len);
        }
        byte[] bytes = baos.toByteArray();
        response.setHeader("Content-Length", String.valueOf(bytes.length));
        BufferedOutputStream bos = null;
        bos = new BufferedOutputStream(response.getOutputStream());
        bos.write(bytes);
        bos.close();
        baos.close();

使用POI导出数据,然后将其下载

//此处将HSSFWorkbook wb处理好,然后最后要导出文件时加上此代码。
ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.setContentType(response.getContentType());
        response.setHeader("Content-disposition",
                "attachment; filename=monthPayment.xls");
        wb.write(baos);
        byte[] bytes = baos.toByteArray();
        response.setHeader("Content-Length", String.valueOf(bytes.length));
        BufferedOutputStream bos = null;
        bos = new BufferedOutputStream(response.getOutputStream());
        bos.write(bytes);
        bos.close();
        baos.close();

1、使用inputStream.read(buffer)方法分段的把txt文本中的内容写入buffer数组。

这里为buffer数组指定了长度为3,所以“hello world!”这组长度为11的数据会被分成4次写入到buffer数组中。

当inputStream.read(buffer)把数据都写入到buffer数组之后,它最后还会返回一次len为-1的值,代表数据完全读完。

2、使用outStream.write(buffer, 0, len)方法,在while循环体内把每次写入到buffer数组的值再次叠加写入到内存缓冲区中。

3、使用outStream.toByteArray()方法把内存缓冲区中的数据流转换成字节数组。

4、最后把字符数组转换成字符串进行返回return new String(data)。

使用ByteArrayOutputStream解决IO乱码问题

说下经过

今天在用s3接口做ceph储存的时候,要实现一个io下载的接口。

需要把InputStream转成byte[],一开始,是的写法是这样的:

byte[] buf = new byte[(int) fileSize];
        InputStream in = ossObject.getObjectContent();
        try {
            for (int n = 0; n != -1; ) {
                n = in.read(buf, 0, buf.length);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        }

可是下载的文件稍大一些,就会出现乱码。

于是换了网上推荐的,使用byte缓存的方法,来实现InputStream转成byte[]:

private static byte[] inputToByte(InputStream inStream, int fileSize) throws IOException {
        ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        byte[] buff = new byte[fileSize];
        int rc;
        while ((rc = inStream.read(buff, 0, fileSize)) > 0) {
            swapStream.write(buff, 0, rc);
        }
        return swapStream.toByteArray();
    }

乱码的情况就解决了!

到此,相信大家对“怎么使用ByteArrayOutputStream下载文件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. 如何使用layui下载文件
  2. javascript如何使用Blob对象下载文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

bytearrayoutputstream

上一篇:Redis数据库入门知识点有哪些

下一篇:如何搭建基于Nexus 3的npm私服

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》