|
@@ -0,0 +1,183 @@
|
|
|
+package com.ynfy.app.api.v1.controller;
|
|
|
+
|
|
|
+import com.ynfy.app.api.v1.annoation.IgnoreAuth;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.common.constant.CommonConstant;
|
|
|
+import org.jeecg.common.constant.SymbolConstant;
|
|
|
+import org.jeecg.common.util.MinioUtil;
|
|
|
+import org.jeecg.common.util.oConvertUtils;
|
|
|
+import org.jeecg.modules.system.entity.FileInfo;
|
|
|
+import org.jeecg.modules.system.service.IFileInfoService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.util.AntPathMatcher;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.servlet.HandlerMapping;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 用户表 前端控制器
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @Author scott
|
|
|
+ * @since 2018-12-20
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RestController
|
|
|
+@RequestMapping("/api/v1/common")
|
|
|
+public class ApiCommonController {
|
|
|
+
|
|
|
+ @Value(value = "${jeecg.path.upload}")
|
|
|
+ private String uploadpath;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 本地:local minio:minio 阿里:alioss
|
|
|
+ */
|
|
|
+ @Value(value = "${jeecg.uploadType}")
|
|
|
+ private String uploadType;
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IFileInfoService fileInfoService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 预览图片&下载文件
|
|
|
+ * 请求地址:http://localhost:8080/common/static/{user/20190119/e1fe9925bc315c60addea1b98eb1cb1349547719_1547866868179.jpg}
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ */
|
|
|
+ @IgnoreAuth
|
|
|
+ @GetMapping(value = "/static/**")
|
|
|
+ public void view(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ // ISO-8859-1 ==> UTF-8 进行编码转换
|
|
|
+ String imgPath = extractPathFromPattern(request);
|
|
|
+ if (oConvertUtils.isEmpty(imgPath) || CommonConstant.STRING_NULL.equals(imgPath)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 其余处理略
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ imgPath = imgPath.replace("..", "").replace("../", "");
|
|
|
+ if (imgPath.endsWith(SymbolConstant.COMMA)) {
|
|
|
+ imgPath = imgPath.substring(0, imgPath.length() - 1);
|
|
|
+ }
|
|
|
+ String fileName = null;
|
|
|
+ if (CommonConstant.UPLOAD_TYPE_LOCAL.equals(uploadType)) {
|
|
|
+ String filePath = uploadpath + File.separator + imgPath;
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ response.setStatus(404);
|
|
|
+ throw new RuntimeException("文件[" + imgPath + "]不存在..");
|
|
|
+ }
|
|
|
+ fileName = file.getName();
|
|
|
+ inputStream = new BufferedInputStream(new FileInputStream(filePath));
|
|
|
+ } else if (CommonConstant.UPLOAD_TYPE_MINIO.equals(uploadType)) {
|
|
|
+ inputStream = MinioUtil.getMinioFile(imgPath);
|
|
|
+ FileInfo fileInfo = fileInfoService.listByFileSaveId(imgPath);
|
|
|
+ fileName = !Objects.isNull(fileInfo) ? fileInfo.getFileName() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置强制下载不打开
|
|
|
+ response.setContentType("application/force-download");
|
|
|
+ response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
|
|
|
+
|
|
|
+ //Accept-Ranges字段为bytes时,表示服务器支持按字节范围请求文件
|
|
|
+ response.addHeader("accept-ranges", "bytes");
|
|
|
+
|
|
|
+ // 设置文件大小
|
|
|
+ long fileSize = MinioUtil.getFileSize(imgPath);
|
|
|
+ response.addHeader("content-length", String.valueOf(fileSize));
|
|
|
+
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buf)) > 0) {
|
|
|
+ outputStream.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ response.flushBuffer();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("预览文件失败" + e.getMessage());
|
|
|
+ response.setStatus(404);
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("imgPath:{}", imgPath);
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @功能:pdf预览
|
|
|
+ */
|
|
|
+ @IgnoreAuth
|
|
|
+ @RequestMapping("/pdf/preview/**")
|
|
|
+ public void pdfPreviewIframe(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ String imgPath = extractPathFromPattern(request);
|
|
|
+ // 其余处理略
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ inputStream = MinioUtil.getMinioFile(imgPath);
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ byte[] buf = new byte[1024];
|
|
|
+ int len;
|
|
|
+ while ((len = inputStream.read(buf)) > 0) {
|
|
|
+ outputStream.write(buf, 0, len);
|
|
|
+ }
|
|
|
+ response.flushBuffer();
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("预览文件失败" + e.getMessage());
|
|
|
+ } finally {
|
|
|
+ if (inputStream != null) {
|
|
|
+ try {
|
|
|
+ inputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (outputStream != null) {
|
|
|
+ try {
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.info("imgPath:{}", imgPath);
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 把指定URL后的字符串全部截断当成参数
|
|
|
+ * 这么做是为了防止URL中包含中文或者特殊字符(/等)时,匹配不了的问题
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String extractPathFromPattern(final HttpServletRequest request) {
|
|
|
+ String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
|
|
|
+ String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
|
|
+ return new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|