最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何使用ByteArrayOutputStream下載文件

 更新時(shí)間:2021年12月10日 14:56:03   作者:該起什么名字好呢  
這篇文章主要介紹了如何使用ByteArrayOutputStream下載文件方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用ByteArrayOutputStream下載文件

//文件名稱
String filepath = ServletActionContext.getServletContext()
        .getRealPath(farmerQrCode.getQrCodeUrl());
        File file = new File(filepath);
        String fileName = new Date().getTime()+".png";
//設(shè)置請求信息
        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導(dǎo)出數(shù)據(jù),然后將其下載

//此處將HSSFWorkbook wb處理好,然后最后要導(dǎo)出文件時(shí)加上此代碼。
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文本中的內(nèi)容寫入buffer數(shù)組。

這里為buffer數(shù)組指定了長度為3,所以“hello world!”這組長度為11的數(shù)據(jù)會(huì)被分成4次寫入到buffer數(shù)組中。

當(dāng)inputStream.read(buffer)把數(shù)據(jù)都寫入到buffer數(shù)組之后,它最后還會(huì)返回一次len為-1的值,代表數(shù)據(jù)完全讀完。

2、使用outStream.write(buffer, 0, len)方法,在while循環(huán)體內(nèi)把每次寫入到buffer數(shù)組的值再次疊加寫入到內(nèi)存緩沖區(qū)中。

3、使用outStream.toByteArray()方法把內(nèi)存緩沖區(qū)中的數(shù)據(jù)流轉(zhuǎn)換成字節(jié)數(shù)組。

4、最后把字符數(shù)組轉(zhuǎn)換成字符串進(jìn)行返回return new String(data)。

使用ByteArrayOutputStream解決IO亂碼

說下經(jīng)過

今天在用s3接口做ceph儲(chǔ)存的時(shí)候,要實(shí)現(xiàn)一個(gè)io下載的接口。

需要把InputStream轉(zhuǎn)成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());
            }
        }

可是下載的文件稍大一些,就會(huì)出現(xiàn)亂碼。

于是換了網(wǎng)上推薦的,使用byte緩存的方法,來實(shí)現(xiàn)InputStream轉(zhuǎn)成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();
    }

亂碼的情況就解決了!

小結(jié)一下

IO這塊不是很熟悉,盡量不要用原生的方法去寫,而應(yīng)該使用JDK封裝好的方法去實(shí)現(xiàn)。避免出現(xiàn)一些意料之外的問題。

PS:至于上面那段代碼為什么會(huì)出現(xiàn)亂碼,暫時(shí)還未研究出來。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

上蔡县| 濮阳县| 怀安县| 论坛| 武鸣县| 敦化市| 漾濞| 肇庆市| 西丰县| 华蓥市| 根河市| 柳江县| 武定县| 博野县| 高密市| 龙门县| 宜兰县| 安平县| 武宁县| 蓬莱市| 耒阳市| 二手房| 额尔古纳市| 朝阳县| 建湖县| 涟水县| 岳池县| 上思县| 桂平市| 鹤峰县| 丹棱县| 平果县| 孝昌县| 长岛县| 抚远县| 邢台市| 巨鹿县| 察隅县| 拜城县| 宁明县| 莲花县|