Browse Source

优化数据读取功能

java110 5 years ago
parent
commit
60d822a4c8

+ 3 - 1
CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java

@@ -7,6 +7,7 @@ import com.java110.core.client.JSchFtpUploadTemplate;
 import com.java110.core.smo.file.IFileInnerServiceSMO;
 import com.java110.dto.file.FileDto;
 import com.java110.core.client.FtpUploadTemplate;
+import com.java110.utils.util.Base64Convert;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
@@ -61,7 +62,8 @@ public class FileInnerServiceSMOImpl extends BaseServiceSMO implements IFileInne
                 java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
                 java110Properties.getFtpUserPassword());
 
-        String context = new BASE64Encoder().encode(fileImg);
+       //String context = new BASE64Encoder().encode(fileImg);
+        String context = Base64Convert.byteToBase64(fileImg);
 
         fileDto.setContext(context);
         fileDtos.add(fileDto);

+ 16 - 3
java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java

@@ -67,10 +67,10 @@ public class FtpUploadTemplate {
             } else if (imageBase64.contains("data:image/webp;base64,")) {
                 imageBase64 = imageBase64.replace("data:image/webp;base64,", "");
                 fileName += ".jpg";
-            } else if(imageBase64.contains("data:application/octet-stream;base64,")){
+            } else if (imageBase64.contains("data:application/octet-stream;base64,")) {
                 imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", "");
                 fileName += ".jpg";
-            }else {
+            } else {
                 fileName += ".jpg";
             }
             FTPFile[] fs = ftpClient.listFiles(fileName);
@@ -175,11 +175,24 @@ public class FtpUploadTemplate {
                 while (ins != null && (bufsize = ins.read(buf, 0, buf.length)) != -1) {
                     byteOut.write(buf, 0, bufsize);
                 }
-                return_arraybyte = byteOut.toByteArray();
+                //return_arraybyte = byteOut.toByteArray();
+                ByteArrayInputStream fis = new ByteArrayInputStream(byteOut.toByteArray());
+                byteOut.flush();
                 byteOut.close();
+                byte[] buffer = new byte[fis.available()];
+                int offset = 0;
+                int numRead = 0;
+                while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
+                    offset += numRead;
+                }
+                if (offset != buffer.length) {
+                    throw new IOException("Could not completely read file ");
+                }
+                fis.close();
                 if (ins != null) {
                     ins.close();
                 }
+                return_arraybyte = buffer;
             }
         } catch (Exception e) {
             e.printStackTrace();

+ 14 - 0
java110-utils/src/main/java/com/java110/utils/util/Base64Convert.java

@@ -35,6 +35,20 @@ public class Base64Convert {
         return strBase64;
     }
 
+    /**
+     * 流转换为字符串
+     *
+     * @param bytes
+     * @return
+     * @throws IOException
+     */
+    public static String byteToBase64(byte[] bytes)  {
+        String strBase64 = null;
+            // in.available()返回文件的字节长度
+            strBase64 = new BASE64Encoder().encode(bytes);      //将字节流数组转换为字符串
+        return strBase64;
+    }
+