IT源码网

JAVA将Base64转为文件并保存

lxf 2020年09月10日 编程语言 642 0
 
public static void base64ToFile(String base64, String fileName, String savePath) { 
        File file = null; 
        //创建文件目录 
        String filePath = savePath; 
        File dir = new File(filePath); 
        if (!dir.exists() && !dir.isDirectory()) { 
                dir.mkdirs(); 
        } 
        BufferedOutputStream bos = null; 
        java.io.FileOutputStream fos = null; 
        try { 
            byte[] bytes = Base64.getDecoder().decode(base64); 
            file=new File(filePath + fileName); 
            fos = new java.io.FileOutputStream(file); 
            bos = new BufferedOutputStream(fos); 
            bos.write(bytes); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        } finally { 
            if (bos != null) { 
                try { 
                    bos.close(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
            if (fos != null) { 
                try { 
                    fos.close(); 
                } catch (IOException e) { 
                    e.printStackTrace(); 
                } 
            } 
        } 
}

 

 
评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!