package com.adc.da.common; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sun.misc.BASE64Encoder; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class HandleFileIOMsg { private static final Logger logger = LoggerFactory.getLogger(HandleFileIOMsg.class); /** * @Description: 根据文件地址转换为base64编码字符串 */ public String getFileBase64Str(String filePath) throws Exception { byte[] data = null; try (InputStream inputStream = new FileInputStream(filePath)){ File file = new File(filePath); data = new byte[(int) file.length()]; inputStream.read(data); inputStream.close(); } catch (IOException e) { logger.info(e.getMessage(),e); } // 加密 BASE64Encoder encoder = new BASE64Encoder(); String codeStr = ""; if(data != null){ codeStr = encoder.encode(data); codeStr = codeStr.replaceAll("\r|\n", ""); } return codeStr; } }