如何使用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(); }
乱码的情况就解决了!
小结一下
IO这块不是很熟悉,尽量不要用原生的方法去写,而应该使用JDK封装好的方法去实现。避免出现一些意料之外的问题。
PS:至于上面那段代码为什么会出现乱码,暂时还未研究出来。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringCloud Netfilx Ribbon负载均衡工具使用方法介绍
Ribbon是Netflix的组件之一,负责注册中心的负载均衡,有助于控制HTTP和TCP客户端行为。Spring Cloud Netflix Ribbon一般配合Ribbon进行使用,利用在Eureka中读取的服务信息,在调用服务节点时合理进行负载2022-12-12servlet之session工作原理简介_动力节点Java学院整理
这篇文章主要介绍了servlet之session工作原理简介,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-07-07IntelliJ IDEA 2021.1 推出语音、视频功能,边写代码边聊天(功能超级强大)
这篇文章主要介绍了IntelliJ IDEA 2021.1 推出语音、视频功能,边写代码边聊天(功能超级强大),本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2021-04-04
最新评论