Browse Source

增加文件预览

humuyu 3 years ago
parent
commit
ecde083a7f

+ 16 - 74
eladmin-system/src/main/java/me/zhengjie/application/bank/controller/BankNotarizeController.java

@@ -1,26 +1,9 @@
 package me.zhengjie.application.bank.controller;
 
-import java.io.IOException;
-import java.io.InputStream;
-
-import javax.servlet.ServletOutputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-
-import cn.hutool.core.io.IoUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
-import me.zhengjie.annotation.rest.AnonymousGetMapping;
 import me.zhengjie.annotation.rest.AnonymousPostMapping;
 import me.zhengjie.application.bank.service.BankNotarizeService;
 import me.zhengjie.application.bank.service.UserAXQInfoService;
@@ -28,10 +11,15 @@ import me.zhengjie.base.ResponseDTO;
 import me.zhengjie.base.ResultCode;
 import me.zhengjie.base.config.TencentHumanFaceVerify;
 import me.zhengjie.base.util.FileUploadUtil;
+import me.zhengjie.base.util.UUIDGenerator;
 import me.zhengjie.base.util.tencent.h5face.SdkTest;
 import me.zhengjie.dao.mybatis.OrderFileRepository;
 import me.zhengjie.dao.mybatis.entity.OrderFileEntity;
-import me.zhengjie.exception.BadRequestException;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
 
 @Validated
 @RestController
@@ -49,41 +37,20 @@ public class BankNotarizeController {
 	 *
 	 * @param json
 	 * @return
-	 * @throws IOException
-	 * @throws ServletException
+	 * @throws Exception 
 	 */
-//    @RequestMapping("/getNotarization")
-	@RequestMapping(value = "/getNotarization/{businessNo}")
-	public void getNotarization(@PathVariable("businessNo") String businessNo, HttpServletRequest request,
-			HttpServletResponse response) throws Exception {
-
+	@RequestMapping("/getNotarization")
+	public ResponseDTO<?> getNotarization(@RequestBody String json) throws Exception {
+		JSONObject jsonObj = JSONObject.parseObject(json);
+		String businessNo = jsonObj.getString("businessNo");
 		if (StringUtils.isEmpty(businessNo)) {
-			throw new BadRequestException("参数错误");
+			return ResponseDTO.error(ResultCode.PARAM_IS_BLANK);
 		}
 		OrderFileEntity orderFile = orderFileRepository.getOrderNotarization(businessNo);
-		ServletOutputStream outputStream = null;
-		InputStream inputStream = null;
-
-		try {
-			inputStream = FileUploadUtil.getInputStream(orderFile.getSignedPdfUrl());
-			response.setContentType("application/pdf");
-			outputStream = response.getOutputStream();
-//
-//			int len = 0;
-//			byte[] buffer = new byte[4096];
-//			while ((len = inputStream.read(buffer)) != -1) {
-//				outputStream.write(buffer, 0, len);
-//			}
-			byte[] b = IoUtil.readBytes(inputStream);
-			outputStream.write(b);
-			outputStream.flush();
-		} catch (Exception e) {
-			e.printStackTrace();
-		} finally {
-			outputStream.close();
-			inputStream.close();
-		}
-//		return ResponseDTO.success();
+		// 下载到指定的目录
+		String filePath = "tmp/" + UUIDGenerator.uuid() + ".pdf";
+		
+		return ResponseDTO.success(FileUploadUtil.getCustomUrl(orderFile.getSignedPdfUrl(),filePath));
 	}
 
 	@RequestMapping("/axqRegister")
@@ -118,31 +85,6 @@ public class BankNotarizeController {
 		return bankNotarizeService.videoCall(json);
 	}
 
-	@AnonymousGetMapping("/preview/{path}")
-	public void preview(@PathVariable("path") String path, HttpServletRequest request, HttpServletResponse response)
-			throws IOException {
-		ServletOutputStream outputStream = null;
-		InputStream inputStream = null;
-		try {
-			inputStream = FileUploadUtil.getInputStream(path);
-			response.setContentType("application/pdf");
-			outputStream = response.getOutputStream();
-
-			int len = 0;
-			byte[] buffer = new byte[4096];
-			while ((len = inputStream.read(buffer)) != -1) {
-				outputStream.write(buffer, 0, len);
-			}
-			outputStream.flush();
-		} catch (Exception e) {
-			e.printStackTrace();
-		} finally {
-			outputStream.close();
-			inputStream.close();
-		}
-
-	}
-
 	@RequestMapping("/order/pdf")
 	public ResponseDTO<?> getOrderPDF(@RequestBody String json) {
 

+ 8 - 3
eladmin-system/src/main/java/me/zhengjie/base/file/FileHandle.java

@@ -2,7 +2,6 @@ package me.zhengjie.base.file;
 
 import java.io.InputStream;
 
-
 public interface FileHandle {
 
 	/**
@@ -42,19 +41,25 @@ public interface FileHandle {
 	 * @Description: (获取 ⽂ 件外链) [bucketName 桶名, objectName 文件名, expires 时间<=1小时]
 	 */
 
-	public String getPreviewUrl(String fileKey) throws Exception ;
+	public String getPreviewUrl(String fileKey) throws Exception;
+
 	/**
 	 * 通过文件id得到文件流
+	 * 
 	 * @param fileId
 	 * @return
 	 * @throws Exception
 	 */
 	public byte[] getByte(String fileId) throws Exception;
+
 	/**
 	 * 得到文件的大小
+	 * 
 	 * @param keyName
 	 * @return
 	 * @throws Exception
 	 */
-	public long getSize(String keyName)throws Exception ;
+	public long getSize(String keyName) throws Exception;
+
+	public String getCustomUrl(String fileKey, String filePath) throws Exception;
 }

+ 6 - 0
eladmin-system/src/main/java/me/zhengjie/base/file/LocalFileHandle.java

@@ -48,4 +48,10 @@ public class LocalFileHandle extends AbstractFileHandle {
 		return 0;
 	}
 
+	@Override
+	public String getCustomUrl(String fileKey, String filePath) throws Exception {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
 }

+ 39 - 10
eladmin-system/src/main/java/me/zhengjie/base/file/MinioFileHandle.java

@@ -1,11 +1,14 @@
 package me.zhengjie.base.file;
 
+import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 
 import javax.annotation.PostConstruct;
 
 import me.zhengjie.dao.mybatis.mapper.FileInfoMapper;
+
+import org.apache.commons.io.FileUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Primary;
@@ -18,6 +21,7 @@ import io.minio.PutObjectOptions;
 import lombok.extern.slf4j.Slf4j;
 import me.zhengjie.dao.mybatis.entity.FileInfoEntity;
 import me.zhengjie.application.bank.service.FileInfoService;
+import me.zhengjie.base.config.AppConfigInfo;
 
 @Slf4j
 @Primary
@@ -35,10 +39,10 @@ public class MinioFileHandle extends AbstractFileHandle {
 	// minio的连接secretkey
 	@Value("${app.minio.secretkey:minioadmin}")
 	public String appMinioSecretkey;
-	
+
 	@Autowired
 	public FileInfoMapper fileInfoMapper;
-	
+
 	private MinioClient minioClient;
 
 	@PostConstruct
@@ -114,17 +118,42 @@ public class MinioFileHandle extends AbstractFileHandle {
 	public String getObjectUrl(String keyName) throws Exception {
 		return minioClient.presignedGetObject(bucketName, keyName, 3600);
 	}
-	 
+
+	/**
+	 * 自定义url
+	 * 
+	 * @param fileKey 文件的
+	 * @return
+	 */
+	public String getCustomUrl(String fileKey, String filePath) {
+		try {
+			// return fileHandle.getPreviewUrl(fileKey);
+			InputStream input = minioClient.getObject(bucketName, fileKey);
+			byte[] b = IoUtil.readBytes(input);
+			String uploadPath = AppConfigInfo.APP_UPLOAD_PATH;
+			if (!uploadPath.endsWith("/")) {
+				uploadPath = uploadPath + "/";
+			}
+			filePath = uploadPath + filePath;
+			FileUtils.writeByteArrayToFile(new File(filePath), b);
+			return appMinioPreview + filePath;
+		} catch (Exception e) {
+			log.error("图片预览失败:" + e.getLocalizedMessage());
+			return null;
+		}
+	}
+
 	public byte[] getByte(String fileId) throws Exception {
 		FileInfoEntity fileInfo = fileInfoMapper.selectById(fileId);
-		String path=fileInfo.getPath();
-		InputStream inputStream=this.getObject(path);
-		//这里已经关闭流,不需要手动关闭
+		String path = fileInfo.getPath();
+		InputStream inputStream = this.getObject(path);
+		// 这里已经关闭流,不需要手动关闭
 		return IoUtil.readBytes(inputStream);
 	}
-	public long getSize(String keyName)throws Exception {
-		  ObjectStat stat = minioClient.statObject(bucketName, keyName);
-		  long length =  stat.length();
-		  return length;
+
+	public long getSize(String keyName) throws Exception {
+		ObjectStat stat = minioClient.statObject(bucketName, keyName);
+		long length = stat.length();
+		return length;
 	}
 }

File diff suppressed because it is too large
+ 13 - 10
eladmin-system/src/main/java/me/zhengjie/base/util/FileUploadUtil.java