为了账号安全,请及时绑定邮箱和手机立即绑定

jdk6 中 如何将 Base64 转为 File 类

jdk6 中 如何将 Base64 转为 File 类

互换的青春 2018-07-06 14:26:19
网上搜到的是将 Base64转为图片 并保存到某一盘符下,要么就是 jdk版本限制;由于下游的数据处理是以 Blob 类型存储到数据库中的,在上游只需将 Base64 转为 File类,但不用输出到某一盘符就可以了;Stream 的转换很头大,希望大神可以指点一下,谢谢
查看完整描述

2 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

    public void base64ToFile(String base64, String pdfName) {
        FileOutputStream fileOutputStream = null;
        try {
            //创建文件目录
            String filePath="D:\\image";
            File file=new File(filePath);
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            byte[] s = Base64.decodeBase64(base64.getBytes());
            fileOutputStream = new FileOutputStream(file+File.separator+pdfName);
            fileOutputStream.write(s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


查看完整回答
反对 回复 2018-07-16
?
回首忆惘然

TA贡献1847条经验 获得超11个赞

BASE64解码成File文件

public static void base64ToFile(String base64, String fileName) {
File file = null;
//创建文件目录
String filePath="D:\image";
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();
}
}
}
}


查看完整回答
反对 回复 2018-07-16
  • 2 回答
  • 0 关注
  • 534 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信