使用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)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
public static String loadAFileToStringDE1(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new BufferedInputStream( new FileInputStream(f) ); long contentLength = f.length(); ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024); byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) > 0) { outstream.write(buffer, 0, len); } outstream.close(); ret = outstream.toString(); //byte[] ba = outstream.toByteArray(); //ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); InputStream is = null; String ret = null; try { is = new FileInputStream(f) ; long contentLength = f.length(); byte[] ba = new byte[(int)contentLength]; is.read(ba); ret = new String(ba); } finally { if(is!=null) {try{is.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法2用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE3(File f) throws IOException { long beginTime = System.currentTimeMillis(); BufferedReader br = null; String ret = null; try { br = new BufferedReader(new FileReader(f)); String line = null; StringBuffer sb = new StringBuffer((int)f.length()); while( (line = br.readLine() ) != null ) { sb.append(line).append(LINE_BREAK); } ret = sb.toString(); } finally { if(br!=null) {try{br.close();} catch(Exception e){} } } long endTime = System.currentTimeMillis(); System.out.println("方法3用时"+ (endTime-beginTime) + "ms"); return ret; } 3个方法去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方法3 可以通过,设置到-Xmx256M时也只有方法3可以通过,干脆设置512M,都可以了,运行时间如果正常的话一般都在4~5S

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值