src/main/java/com/hx/util/FileConvertTool.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/hx/util/ImageBase64Util.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/hx/util/FileConvertTool.java
@@ -6,6 +6,7 @@ import org.springframework.http.MediaType; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; import sun.misc.BASE64Decoder; import java.io.*; import java.net.URL; @@ -184,59 +185,6 @@ } /** * 根据byte数组,生成文件 */ public static File getFile(String baseIn, String filePath, String fileName) { if(baseIn == null){ return null; } BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { InputStream in = parse_inputStream(baseIn); if(in == null){ return null; } File dir = new File(filePath); //判断文件目录是否存在 if(!dir.exists()){ dir.mkdirs(); } file = new File(filePath+"/"+fileName); fos = new FileOutputStream(file); byte[] b = new byte[BUFFER_SIZE]; // while ((in.read(b)) != -1) { // fos.write(b); // 写入数据 // } int len; while ((len = in.read(b)) != -1){ fos.write(b, 0, len); } in.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return file; } /** * 获得指定文件的byte数组 */ private static byte[] getBytes(String filePath){ @@ -350,4 +298,6 @@ return false; } } } src/main/java/com/hx/util/ImageBase64Util.java
New file @@ -0,0 +1,79 @@ package com.hx.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; /** * 图片和base64字符转换工具 * @USER: fhx * @DATE: 2023/4/6 **/ public class ImageBase64Util { /** * base64字符串转化成图片 * @param base64 * @param fileName * @param filePath * @return * @throws Exception */ public static File base64ToFile(String base64, String fileName, String filePath) throws Exception { if(base64.contains("data:image")){ base64 = base64.substring(base64.indexOf(",")+1); } base64 = base64.replace("\r\n", ""); //创建文件目录 File file = File.createTempFile(base64, fileName, new File(filePath)); BufferedOutputStream bos = null; FileOutputStream fos = null; try { BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(base64); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bytes); }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; } /** * 图片转化成base64字符串 * @param imgFile * @return */ public static String GetImageStr(File imgFile) {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理 InputStream in = null; byte[] data = null; //读取图片字节数组 try { in = new FileInputStream(imgFile); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);//返回Base64编码过的字节数组字符串 } }