spring读写excel文件的时候,return语句或者异常信息写到了文件里面

spring读写excel文件的时候,return语句或者异常信息写到了文件里面

return 语句

if(true) {
  System.out.println("准备返回了");
  return "hellowrld";
}
response.addHeader("Content-Disposition",
                   "attachment;fileName=ddd.xls");// 设置文件名

只要是在设置header的Content-Disposition 属性之前, return 语句的内容都会被写到excel中。

异常

 try {
   wwb.write();
   wwb.close();
 if(true) {
   System.out.println("准备抛异常了");
   throw new RuntimeException("hellowrld");
 }
 } catch (IOException e) {
   e.printStackTrace();
 } catch (WriteException e) {
   e.printStackTrace();
 }

WritableWorkbook 实例close之后抛异常,不会影响文件下载。

try {
  wwb.write();
  if(true) {
    System.out.println("准备抛异常了");
    throw new RuntimeException("hellowrld");
  }
  wwb.close();
} catch (IOException e) {
  e.printStackTrace();
} catch (WriteException e) {
  e.printStackTrace();
}

WritableWorkbook 实例close之前抛异常,文件不能下载,服务器直接抛异常。

try-catch-finally

try-catch块中有 return , 或者 抛异常以后 , 如果有finally, 则finally必会执行,可以处理我们自己抛出的异常,以及关闭流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值