package {{ package.Common }}.unit; import org.apache.commons.io.FileUtils; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Encoder; import java.io.*; import java.util.Base64; public class FilesUtil { /** * 根据文件路径获取文件字节流 * @return * filePath 文件路径 * @throws IOException */ public static byte[] toByteArray(String filePath) throws IOException { File f = new File(filePath); if (!f.exists()) { throw new FileNotFoundException("文件不存在"); } ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(f)); int buf_size = 1024; byte[] buffer = new byte[buf_size]; int len = 0; while (-1 != (len = in.read(buffer, 0, buf_size))) { bos.write(buffer, 0, len); } return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } bos.close(); } } public static File multipartFileToFile(MultipartFile file) throws Exception { File toFile = null; if (file.equals("") || file.getSize() <= 0) { file = null; } else { InputStream ins = null; ins = file.getInputStream(); toFile = new File(file.getOriginalFilename()); inputStreamToFile(ins, toFile); ins.close(); } return toFile; } private static void inputStreamToFile(InputStream ins, File file) { try { OutputStream os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); ins.close(); } catch (Exception e) { e.printStackTrace(); } } public static String TransformPhotoToBase64Data(String path){ Base64.Encoder encoder= Base64.getEncoder(); //获取Base64编码器 byte [] ImgContainer = null ; //数据集缓存器 FileInputStream fileInputStream = null; //文件输入流 try { System.out.println(path); File file=new File(path); fileInputStream = new FileInputStream(file); //到指定路径寻找文件 ImgContainer = new byte[fileInputStream.available()]; //设置图片字节数据缓冲区大小 fileInputStream.read(ImgContainer); //将数据流中的图片数据读进缓冲区 String Base64ImgData =encoder.encodeToString(ImgContainer); //将图片编码转换成Base64格式的数据集 fileInputStream.close(); //关闭数据流 return Base64ImgData; //将缓冲区数据转换成字符数据返回 } catch (FileNotFoundException e) { return "找不到指定文件!"; } catch (IOException e) { e.printStackTrace(); } return "null"; } public static String getBase64String(MultipartFile multiPartFile) throws IOException { String baseStr = null; //把MultipartFile转化为File File file = new File(multiPartFile.getOriginalFilename()); FileUtils.copyInputStreamToFile(multiPartFile.getInputStream(), file); try {//file转base64 FileInputStream inputStream = new FileInputStream(file); byte[] buffer = new byte[(int) file.length()]; inputStream.read(buffer); inputStream.close(); baseStr = new BASE64Encoder().encode(buffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //删除临时文件 if (file.exists()) { file.delete(); } baseStr = baseStr.replaceAll("\r\n", ""); return baseStr; } /** * 返回文件扩展名,带。 * @param file * @return */ public static String getFileExtension(MultipartFile file) { String fileName = file.getOriginalFilename(); int dotIndex = fileName.lastIndexOf("."); if (dotIndex == -1) { return ""; } else { return "."+fileName.substring(dotIndex + 1); } } }