使用ByteArrayOutputStream下载文件
来源:互联网 发布:淘宝客服周总结计划 编辑:程序博客网 时间:2024/06/04 23:18
//文件名称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)。
0 0
- 使用ByteArrayOutputStream下载文件
- ByteArrayOutputStream 大文件内存溢出
- ByteArrayOutputStream
- ByteArrayOutputStream
- ByteArrayOutputStream
- ByteArrayOutputStream
- 内存虚拟文件(ByteArrayOutputStreamとByteArrayInputStream)
- 文件与二进制数据互转-ByteArrayOutputStream
- 将文件转为byte[],通过ByteArrayOutputStream 实现
- ByteArrayInputStream和ByteArrayOutputStream使用-黑马程序员
- ByteArrayOutputStream以及DataOutputStream的组合使用
- java类ByteArrayOutputStream的toString()方法使用
- 【第二十一颗】流---ByteArrayOutputStream的使用
- 使用vbs下载文件
- 使用数据流下载文件
- 使用数据流下载文件
- 使用vbs下载文件
- 使用FTP下载文件
- java-抽象类和接口的区别
- getcwd()、__DIR__、__FILE__
- 封装Redis部分基本操作(键值都封装为对象类型)
- 王朝 第十二周 连续乘法问题 1*3*5.......
- 文章标题
- 使用ByteArrayOutputStream下载文件
- [Wondgirl] 微信小程序(三)模板的使用
- Python实现Tab自动补全和历史命令管理的方法
- 矩阵的求导运算
- HashSet剖析
- 清理keyChain
- ECHART基本使用,折线图,柱状图,散点图,饼图,盒形图5分钟上手
- 使用fetch遇到过的坑
- 使用正则表达式匹配字符串中特定的xml内容