Use try-with-resources or close this "FileOutputStream" in a "finally" clause. 解决办法

Use try-with-resources or close this "FileOutputStream" in a "finally" clause.

image

原来代码:

/**
     * @param contents 二进制数据
     * @param filePath 文件存放目录,包括文件名及其后缀,如D:\file\bike.jpg
     * @Title: byteToFile
     * @Description: 把二进制数据转成指定后缀名的文件,例如PDF,PNG等
     */
    public static void byteToFile(byte[] contents, String filePath) throws IOException {
        FileOutputStream fos = null;
        BufferedOutputStream output = null;
        try (  BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(contents))){
            File file = new File(filePath);
            // 获取文件的父路径字符串
            File path = file.getParentFile();
            if (!path.exists()) {
                log.info("文件夹不存在,创建。path={}", path);
                boolean isCreated = path.mkdirs();
                if (!isCreated) {
                    log.error("创建文件夹失败,path={}", path);
                }
            }
            fos = new FileOutputStream(file);
            // 实例化OutputString 对象
            output = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                output.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            output.flush();
        } catch (Exception e){
            log.error("输出文件流时抛异常,filePath={}", filePath, e);
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

使用sonar扫描出来的bug需要修复。修改之后的结果是:

  /**
     * @param contents 二进制数据
     * @param filePath 文件存放目录,包括文件名及其后缀,如D:\file\bike.jpg
     * @Title: byteToFile
     * @Description: 把二进制数据转成指定后缀名的文件,例如PDF,PNG等
     */
    public static void byteToFile(byte[] contents, String filePath) throws IOException {
        FileOutputStream fos = null;
        BufferedOutputStream output = null;
        try (BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(contents))) {
            File file = new File(filePath);
            // 获取文件的父路径字符串
            File path = file.getParentFile();
            if (!path.exists()) {
                log.info("文件夹不存在,创建。path={}", path);
                boolean isCreated = path.mkdirs();
                if (!isCreated) {
                    log.error("创建文件夹失败,path={}", path);
                }
            }
            fos = new FileOutputStream(file);
            // 实例化OutputString 对象
            output = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                output.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            output.flush();
        } catch (Exception e) {
            log.error("输出文件流时抛异常,filePath={}", filePath, e);
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

扫描之后

image

提示没有问题,结果完美!

审核工具给出的修改意见是:

try (

try(){

}catch (Exception e) {

}

写在try()里面会自动关闭流不需要继续其他操作,至于try里面定义的需要重新finally关闭下

你可能感兴趣的:(Use try-with-resources or close this "FileOutputStream" in a "finally" clause. 解决办法)