From 0b6e556313e5626f335a5b4b590a1c495238170b Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期一, 25 三月 2024 15:29:35 +0800 Subject: [PATCH] 1.修改判断 --- src/main/java/com/hx/util/FileConvertTool.java | 93 ++++++++++++++++++---------------------------- 1 files changed, 37 insertions(+), 56 deletions(-) diff --git a/src/main/java/com/hx/util/FileConvertTool.java b/src/main/java/com/hx/util/FileConvertTool.java index 55c93c2..95359f6 100644 --- a/src/main/java/com/hx/util/FileConvertTool.java +++ b/src/main/java/com/hx/util/FileConvertTool.java @@ -1,11 +1,14 @@ package com.hx.util; +import com.hx.exception.TipsException; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.io.IOUtils; import org.springframework.http.MediaType; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.commons.CommonsMultipartFile; +import sun.misc.BASE64Decoder; +import sun.misc.BASE64Encoder; import java.io.*; import java.net.URL; @@ -41,8 +44,7 @@ /** * 鑾峰彇鏂囦欢base64瀛楃涓� * @param urlPath 鏂囦欢璺緞 - * @return - * @throws IOException + * @return 杩斿洖base64缂栫爜 */ public static String getFileBaseStrByUrl(String urlPath) throws IOException { InputStream is = getUrlFile(urlPath); @@ -107,7 +109,7 @@ File file = null; try { File dir = new File(filePath); - if(!dir.exists()&&dir.isDirectory()){//鍒ゆ柇鏂囦欢鐩綍鏄惁瀛樺湪 + if(!dir.exists()){//鍒ゆ柇鏂囦欢鐩綍鏄惁瀛樺湪 dir.mkdirs(); } file = new File(filePath+"\\"+fileName); @@ -146,59 +148,6 @@ FileOutputStream fos = null; File file = null; try { - 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鏁扮粍锛岀敓鎴愭枃浠� - */ - 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()){ @@ -350,4 +299,36 @@ return false; } } + + public static String encodeBase64File(File file) throws Exception { + FileInputStream fileInputStream = new FileInputStream(file); + byte[] bytes = new byte[fileInputStream.available()]; + // 璇诲彇鍒� byte 閲岄潰 + fileInputStream.read(bytes); + fileInputStream.close(); + BASE64Encoder base64Encoder = new BASE64Encoder(); + // 寰楀埌鏂囦欢 涔嬪悗杞垚beye 鐒跺悗浣跨敤base64杞爜 + // 杞爜 + String encode = base64Encoder.encode(bytes); + return encode; + } + + public static String encodeBase64File(MultipartFile multipartFile) throws Exception { + if (multipartFile == null) { + throw new TipsException("鏈鏌ュ埌涓婁紶鐨勬枃浠讹紒"); + } + String imageBaseStr = null; + try { + String contentType = multipartFile.getContentType(); + byte[] imageBytes = multipartFile.getBytes(); + BASE64Encoder base64Encoder = new BASE64Encoder(); +// imageBaseStr = "data:" + contentType + ";base64," + base64Encoder.encode(imageBytes); + imageBaseStr = base64Encoder.encode(imageBytes); + imageBaseStr = imageBaseStr.replaceAll("[\\s*\t\n\r]", ""); + } catch (IOException e) { + throw new TipsException("鏂囦欢杞崲base64寮傚父"); + } + //杩斿洖鐢熸垚鐨勭紪鐮� + return imageBaseStr; + } } -- Gitblit v1.8.0