Przeglądaj źródła

新增了Base64编码文件及字节流上传oss

zhangchenm 2 tygodni temu
rodzic
commit
cf6703c95f

+ 2 - 3
trade-admin/src/main/java/com/trade/admin/job/PurchaseInvoiceSchedule.java

@@ -232,9 +232,8 @@ public class PurchaseInvoiceSchedule {
         businessCommonRequest.setCustomerId(invoiceVo.getCustomerUserId());
         InvoiceFileResponse invoiceFileResponse = xiaotuRequestService.invoiceFileQuery(invoice.getPurchaseVoucherNo(),
                 businessCommonRequest);
-        MultipartFileExtend mockMultipartFile = XiaoTuUtil.getMultipartFileExtend(invoiceFileResponse.getFileName(),
-                invoiceFileResponse.getFileName(), invoiceFileResponse.getBlob());
-        TFileStorage fileStorage = ossFileService.addStorage(mockMultipartFile, FileModuleEnum.PURCHASE_ORDER);
+        TFileStorage fileStorage = ossFileService.addStorageWithBase64Str(invoiceFileResponse.getFileName(),
+                invoiceFileResponse.getBlob(), FileModuleEnum.PURCHASE_ORDER);
         BusinessRelateFile businessRelateFile = Builder.of(BusinessRelateFile.class)
                 .with(BusinessRelateFile::setBelongId, invoice.getId())
                 .with(BusinessRelateFile::setBelongType, ConstantUtil.PURCHASE_ORDER)

+ 29 - 2
trade-service/src/main/java/com/trade/service/filestorage/service/OSSFileService.java

@@ -42,7 +42,6 @@ public interface OSSFileService {
      */
     TFileStorage uploadAndSaveDBRecord(File file, FileModuleEnum fileModuleEnum);
 
-
     /**
      * 新增文件保存 With MultipartFile
      *
@@ -100,7 +99,7 @@ public interface OSSFileService {
      * @return String
      */
     String previewFile(Long fileId, HttpServletRequest request, HttpServletResponse response, Model model,
-                       String watermarkText);
+            String watermarkText);
 
     /**
      * 获取文件输入流
@@ -110,5 +109,33 @@ public interface OSSFileService {
      */
     InputStream getFileInputStream(String fileKey);
 
+    /**
+     * 根据文件url下载文件到OSS,并保存数据库记录
+     * 
+     * @param originalName 文件名
+     * @param fileUrl 文件url
+     * @param moduleType 文件模块
+     * @return TFileStorage
+     */
     TFileStorage downloadFileFromUrl(String originalName, String fileUrl, FileModuleEnum moduleType);
+
+    /**
+     * 根据字节流上传文件到OSS,并保存数据库记录
+     * 
+     * @param originalName 文件名
+     * @param bytes 字节流
+     * @param fileModule 文件模块
+     * @return TFileStorage
+     */
+    TFileStorage addStorageWithBytes(String originalName, byte[] bytes, FileModuleEnum fileModule);
+
+    /**
+     * 根据base64Str上传文件到OSS,并保存数据库记录
+     *
+     * @param originalName 文件名
+     * @param base64Str base64字符串
+     * @param fileModule 文件模块
+     * @return TFileStorage
+     */
+    TFileStorage addStorageWithBase64Str(String originalName, String base64Str, FileModuleEnum fileModule);
 }

+ 26 - 1
trade-service/src/main/java/com/trade/service/filestorage/service/impl/OSSFileServiceImpl.java

@@ -13,7 +13,6 @@ import com.aspose.words.SaveFormat;
 import com.github.pagehelper.util.StringUtil;
 import com.trade.common.utils.StringUtils;
 import com.trade.common.utils.file.ASopUtil;
-import com.trade.common.utils.file.FileUtils;
 import com.trade.common.utils.http.HttpClientUtils;
 import com.trade.common.utils.http.HttpConfig;
 import com.trade.service.filestorage.dto.FileAbstractDto;
@@ -22,6 +21,7 @@ import com.trade.service.filestorage.util.TextConverterPdfUtil;
 import com.trade.service.filestorage.util.watermark.MakeWaterMark;
 import com.trade.service.filestorage.util.watermark.PreviewUtil;
 import com.trade.service.filestorage.util.watermark.TextMark;
+import org.apache.tomcat.util.codec.binary.Base64;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.Model;
 import org.springframework.util.MultiValueMap;
@@ -233,6 +233,31 @@ public class OSSFileServiceImpl implements OSSFileService {
         return tFileStorage;
     }
 
+    @Override
+    public TFileStorage addStorageWithBytes(String originalName, byte[] bytes, FileModuleEnum fileModule) {
+        try (InputStream inputStream = new ByteArrayInputStream(bytes)) {
+            String saveName = FileUtil.randomFileName() + FileUtil.getSuffix(originalName);
+            String objName = fileModule.getCode() + OSSUtil.fileSep + saveName;
+            // 上传oss
+            String eTag = ossUtil.uploadFile(objName, inputStream);
+            if (StringUtils.isEmpty(eTag)) {
+                throw new ServiceException("oss upload file failed, eTag is null");
+            }
+            // 保存文件记录
+            TFileStorage tFileStorage = new TFileStorage(eTag, originalName, saveName, objName, bytes.length);
+            tFileStorageService.save(tFileStorage);
+            return tFileStorage;
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new ServiceException("文件保存失败" + e.getMessage(), HttpStatus.ERROR);
+        }
+    }
+
+    @Override
+    public TFileStorage addStorageWithBase64Str(String originalName, String base64Str, FileModuleEnum fileModule) {
+        return addStorageWithBytes(originalName, Base64.decodeBase64(base64Str), fileModule);
+    }
+
     private void uploadAndSaveFile(HttpServletRequest request, FileModuleEnum moduleDict) {
         if (!(request instanceof MultipartHttpServletRequest)) {
             return;

+ 6 - 15
trade-service/src/main/java/com/trade/service/trade/salesorder/service/impl/CustomsDeclarationFileServiceImpl.java

@@ -8,8 +8,6 @@ import com.trade.service.filestorage.util.FileUtil;
 import com.trade.service.thirdparty.xiaotu.dto.BusinessCommonRequest;
 import com.trade.service.thirdparty.xiaotu.dto.customsDeclarationFileResponse;
 import com.trade.service.thirdparty.xiaotu.service.XiaotuRequestService;
-import com.trade.service.thirdparty.xiaotu.utils.MultipartFileExtend;
-import com.trade.service.thirdparty.xiaotu.utils.XiaoTuUtil;
 import com.trade.service.trade.salesorder.domain.CustomsDeclarationFile;
 import com.trade.service.trade.salesorder.mapper.CustomsDeclarationFileMapper;
 import com.trade.service.trade.salesorder.service.ICustomsDeclarationFileService;
@@ -17,7 +15,6 @@ import com.trade.service.trade.filingdoc.vo.SalesOrder4CustomsFileQueryVO;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
-import java.io.IOException;
 
 /**
  * <p>
@@ -55,27 +52,21 @@ public class CustomsDeclarationFileServiceImpl extends ServiceImpl<CustomsDeclar
                 .customsDeclarationFileQuery(fileQueryVO.getQueryNo(), fileQueryVO.getType(), businessCommonRequest);
         try {
             if (null != fileResponse.getBgdBlob()) {
-                MultipartFileExtend bgdFile = XiaoTuUtil.getMultipartFileExtend(
-                        fileQueryVO.getQueryNo() + FileUtil.getSuffix(fileResponse.getBgdWjm()),
-                        fileResponse.getBgdWjm(), fileResponse.getBgdBlob());
-                TFileStorage fileStorage = ossFileService.addStorage(bgdFile, FileModuleEnum.CUSTOMS_DECLARATION);
+                TFileStorage fileStorage = ossFileService.addStorageWithBase64Str(fileResponse.getBgdWjm(),
+                        fileResponse.getBgdBlob(), FileModuleEnum.CUSTOMS_DECLARATION);
                 customsDeclarationFile.setCustomsFileId(fileStorage.getId());
             }
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
         try {
             if (null != fileResponse.getWtbgxyBlob()) {
-                MultipartFileExtend wtbgxyFile = XiaoTuUtil.getMultipartFileExtend(
-                        fileQueryVO.getQueryNo() + "_" + fileResponse.getXybh()
-                                + FileUtil.getSuffix(fileResponse.getWtbgxyWjm()),
-                        fileResponse.getWtbgxyWjm(), fileResponse.getWtbgxyBlob());
-                TFileStorage fileStorage = ossFileService.addStorage(wtbgxyFile,
-                        FileModuleEnum.ENTRUSTED_CUSTOMS_AGREEMENT);
+                TFileStorage fileStorage = ossFileService.addStorageWithBase64Str(fileResponse.getWtbgxyWjm(),
+                        fileResponse.getWtbgxyBlob(), FileModuleEnum.ENTRUSTED_CUSTOMS_AGREEMENT);
                 customsDeclarationFile.setEntrustFileId(fileStorage.getId());
                 customsDeclarationFile.setEntrustNo(fileResponse.getXybh());
             }
-        } catch (IOException e) {
+        } catch (Exception e) {
             e.printStackTrace();
         }
         if (null != customsDeclarationFile.getId()) {