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必会执行,可以处理我们自己抛出的异常,以及关闭流。