From c2bb89da722d5b5db5f7151546369eb84b775a05 Mon Sep 17 00:00:00 2001 From: chenjiahe <763432473@qq.com> Date: 星期一, 15 四月 2024 16:26:35 +0800 Subject: [PATCH] 线程池工具新增方法 --- src/main/java/com/hx/util/FileConvertTool.java | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/hx/util/FileConvertTool.java b/src/main/java/com/hx/util/FileConvertTool.java index a57a0fd..da21ef8 100644 --- a/src/main/java/com/hx/util/FileConvertTool.java +++ b/src/main/java/com/hx/util/FileConvertTool.java @@ -1,5 +1,6 @@ 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; @@ -7,6 +8,7 @@ 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; @@ -42,8 +44,7 @@ /** * 鑾峰彇鏂囦欢base64瀛楃涓� * @param urlPath 鏂囦欢璺緞 - * @return - * @throws IOException + * @return 杩斿洖base64缂栫爜 */ public static String getFileBaseStrByUrl(String urlPath) throws IOException { InputStream is = getUrlFile(urlPath); @@ -108,10 +109,10 @@ File file = null; try { File dir = new File(filePath); - if(!dir.exists()&&dir.isDirectory()){//鍒ゆ柇鏂囦欢鐩綍鏄惁瀛樺湪 + if(!dir.exists()){//鍒ゆ柇鏂囦欢鐩綍鏄惁瀛樺湪 dir.mkdirs(); } - file = new File(filePath+"\\"+fileName); + file = new File(filePath+"/"+fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); @@ -262,6 +263,23 @@ } /** + * 鍒犻櫎鍗曚釜鏂囦欢 + * @param file 琚垹闄ゆ枃浠� + * @return 鍗曚釜鏂囦欢鍒犻櫎鎴愬姛杩斿洖true锛屽惁鍒欒繑鍥瀎alse + */ + public static boolean deleteFile(File file) { + if(file == null){ + return true; + } + // 璺緞涓烘枃浠朵笖涓嶄负绌哄垯杩涜鍒犻櫎 + if (file.isFile() && file.exists()) { + file.delete(); + return true; + } + return false; + } + + /** * 鍒犻櫎鐩綍锛堟枃浠跺す锛変互鍙婄洰褰曚笅鐨勬枃浠� * @param sPath 琚垹闄ょ洰褰曠殑鏂囦欢璺緞 * @return 鐩綍鍒犻櫎鎴愬姛杩斿洖true锛屽惁鍒欒繑鍥瀎alse @@ -299,5 +317,35 @@ } } + 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