Java byte转文件

public static Map<String, Object> byteToPdf(String fileName, String pdfPath, byte[] bytes) {
        Map<String, Object> resultMap = new HashMap<>();

        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(pdfPath);
            if(!dir.exists()){//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(pdfPath + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);

            resultMap.put("result", "success");
            resultMap.put("pdfFilePath", pdfPath + fileName);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            resultMap.put("result", "fail");
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    logger.error(e1.getMessage(), e1);
                    resultMap.put("result", "fail");
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    logger.error(e1.getMessage(), e1);
                    resultMap.put("result", "fail");
                }
            }
        }

        return resultMap;
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注