小编sma*_*art的帖子

FindBugs:"可能无法关闭流",如何解决?

以下代码被FindBugs标记为错误.FindBugs说"这种方法可能无法清理(关闭,处理)流,数据库对象或需要显式清理操作的其他资源." 错误标记在该行上output = new FileOutputStream (localFile);

但是我们已经在块中添加了try/finally.

inputStream   input =null;
OutputStream output =null;
try {
    input = zipFile.getInputStream(entry);
    File localFile = new File(unzipFile.getAbsolutePath()
            + File.separator + entryName);
    output = new FileOutputStream (localFile);  // BUG MARKED HERE
    byte[] buffer = new byte[1024 * 8];
    int readLen = 0;
    while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {
        output.write(buffer, 0, readLen);
    }
    output.flush();
    output.close();
    input.close();

} finally {
    if(output!=null) {
        output.flush();
        output.close();
    }
    if(input!=null) {
        input.close();
    }
}
Run Code Online (Sandbox Code Playgroud)

java findbugs stream

5
推荐指数
1
解决办法
6385
查看次数

在Spring mvc控制器上使用Spring Aop

我有一个Spring MVC控制器抛出两种异常:

@RequestMapping(value = "forwardRefundApply",method = RequestMethod.GET)
public ModelAndView forwardRefundApply1(String ticketNbr)throws Exception {
    if(true)
        throw new Exception();
    else
        throw new ApplicationException("test");
}
Run Code Online (Sandbox Code Playgroud)

然后我编写了一个AOP类来处理异常,然后像这样返回Model:

  @Pointcut("execution(public * ..*(..))")
public void getRefundPointCut() {
}

@AfterThrowing(pointcut="getRefundPointCut()", throwing="e")
public ModelAndView throwException(Exception e){
    ModelAndView mav = null;
    if(e instanceof ApplicationException)
    {

        e.printStackTrace();
        mav = new ModelAndView(CommonConstants.ERRORPAGE);
        mav.addObject("errorMsg", "application error");
        return mav;
    }
    else{
        e.printStackTrace();

        mav  = new ModelAndView(CommonConstants.ERRORPAGE);
        mav.addObject("errorMsg", "system error");
        return mav;
    }
}
Run Code Online (Sandbox Code Playgroud)

唉是工作.但结果是错误.系统错误:

org.springframework.web.util.NestedServletException:处理程序处理失败; 嵌套异常是java.lang.NoSuchMethodError

Aspect类是不是可以将ModelAndView返回给Controller?

java aop spring-mvc

0
推荐指数
1
解决办法
2308
查看次数

标签 统计

java ×2

aop ×1

findbugs ×1

spring-mvc ×1

stream ×1