base64转文件保存
package com.hcp.tools.file; /** * @Author:LLF * @description: * @Date:2023/5/17 20:15 * @version: V1.0 */ public enum Base64FileTypeEnum { // 文件类型 BASE64_FILETYPE_DOC(".doc", "data:application/msword;base64"), BASE64_FILETYPE_DOCX(".docx", "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64"), BASE64_FILETYPE_XLS(".xls", "data:application/vnd.ms-excel;base64"), BASE64_FILETYPE_XLSX(".xlsx", "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64"), BASE64_FILETYPE_PDF(".pdf", "data:application/pdf;base64"), BASE64_FILETYPE_PPT(".ppt", "data:application/vnd.ms-powerpoint;base64"), BASE64_FILETYPE_PPTX(".pptx", "data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64"), BASE64_FILETYPE_TXT(".txt", "data:text/plain;base64"), // 图片类型 BASE64_FILETYPE_PNG(".png", "data:image/png;base64"), BASE64_FILETYPE_JPG(".jpg", "data:image/jpeg;base64"), BASE64_FILETYPE_JPEG(".jpeg", "data:image/jpeg;base64"), BASE64_FILETYPE_GIF(".gif", "data:image/gif;base64"), BASE64_FILETYPE_SVG(".svg", "data:image/svg+xml;base64"), BASE64_FILETYPE_ICO(".ico", "data:image/x-icon;base64"), BASE64_FILETYPE_BMP(".bmp", "data:image/bmp;base64"), // // 二进制流 // BASE64_FILETYPE_OCTET_STREAM("octet-stream", "data:application/octet-stream;base64,"), ; private Base64FileTypeEnum(String code, String value) { this.code = code; this.value = value; } private String code; private String value; public String getCode() {return code;} public String getValue() {return value;} public static String getFileType(String value) { Base64FileTypeEnum[] types = values(); for (Base64FileTypeEnum x : types) { if (x.getValue().equals(value)) { return x.getCode(); } } return null; } }
public static File base64ToFile(String base64, String fileName,String path) { File file = null; File dir = new File(path); if (!dir.exists() && !dir.isDirectory()) { dir.mkdirs(); } BufferedOutputStream bos = null; FileOutputStream fos = null; try { //截取base64头部,获取文件类型 String fileType = Base64FileTypeEnum.getFileType(base64.substring(0, base64.indexOf(","))); //去掉头部,防止转换文件后打开显示文件损坏 String s = base64.substring(base64.indexOf(",") + 1); byte[] bytes = new BASE64Decoder().decodeBuffer(s); file = new File(path + "/" + fileName + fileType); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); } catch (IOException 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(); } } } return file; }