Просмотр исходного кода

添加读取文件转换成Base64方法

Lucien 1 год назад
Родитель
Сommit
77cbf13b2a

+ 2 - 0
src/main/java/com/keystar/plane/inspection/service/IReportService.java

@@ -17,4 +17,6 @@ public interface IReportService {
 
     String pic2Base64FromMinio(String minioAddr);
 
+    String pic2Base64FromPath(String filePath);
+
 }

+ 43 - 0
src/main/java/com/keystar/plane/inspection/service/impl/ReportServiceImpl.java

@@ -162,4 +162,47 @@ public class ReportServiceImpl implements IReportService {
 
         return "";
     }
+
+    @Override
+    public String pic2Base64FromPath(String filePath) {
+
+        FileInputStream fileInputStream = null;
+        ByteArrayOutputStream byteArrayOutputStream = null;
+
+        try {
+
+            fileInputStream = new FileInputStream(filePath);
+
+            byteArrayOutputStream = new ByteArrayOutputStream();
+
+            byte[] bytes = new byte[10240];
+            int mReadLength = 0;
+
+            while ((mReadLength = fileInputStream.read(bytes)) != -1) {
+                byteArrayOutputStream.write(bytes, 0, mReadLength);
+            }
+
+            return Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray());
+        }catch (Exception e) {
+
+        }finally {
+            if (fileInputStream != null) {
+                try {
+                    fileInputStream.close();
+                }catch (Exception e) {
+
+                }
+            }
+            if (byteArrayOutputStream != null) {
+                try {
+                    byteArrayOutputStream.close();
+                }catch (Exception e) {
+
+                }
+            }
+        }
+
+        return "";
+
+    }
 }