This commit is contained in:
udf_admin@163.com
2020-05-05 13:49:48 +08:00
parent 07d9cfa878
commit ad184fccff
15 changed files with 117 additions and 40 deletions
+1
View File
@@ -25,6 +25,7 @@
<mybatis-plus-boot-starter.version>3.3.1.tmp</mybatis-plus-boot-starter.version>
<guava.version>28.2-jre</guava.version>
<poi.version>4.1.2</poi.version>
<thumbnailator.version>0.4.11</thumbnailator.version>
</properties>
<!--子模块 -->
<modules>
+6
View File
@@ -20,5 +20,11 @@
<groupId>org.itkk.udf</groupId>
<artifactId>udf-starter-core</artifactId>
</dependency>
<!-- thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>${thumbnailator.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,25 @@
package org.itkk.udf.starter.file.db;
import lombok.extern.slf4j.Slf4j;
/**
* 常量
*/
@Slf4j
public class DbFileConstant {
/**
* JPEG上下文类型
*/
public static final String CONTENT_TYPE_JPEG = "image/jpeg";
/**
* PNG上下文类型
*/
public static final String CONTENT_TYPE_PNG = "image/png";
/**
* 私有化构造函数
*/
private DbFileConstant() {
}
}
@@ -1,19 +1,22 @@
package org.itkk.udf.starter.file.db.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.itkk.udf.starter.core.CoreConstant;
import org.itkk.udf.starter.core.CoreProperties;
import org.itkk.udf.starter.core.CoreUtil;
import org.itkk.udf.starter.core.exception.ParameterValidException;
import org.itkk.udf.starter.core.exception.SystemRuntimeException;
import org.itkk.udf.starter.file.db.DbFileConstant;
import org.itkk.udf.starter.file.db.DbFileProperties;
import org.itkk.udf.starter.file.db.IGenPreviewPdf;
import org.itkk.udf.starter.file.db.dto.DbFileInfoDto;
import org.itkk.udf.starter.file.db.entity.DbFileInfoEntity;
import org.itkk.udf.starter.file.db.repository.IDbFileInfoRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -27,6 +30,7 @@ import java.text.SimpleDateFormat;
import java.util.*;
@Service
@Slf4j
public class DbFileService {
/**
@@ -191,11 +195,13 @@ public class DbFileService {
/**
* 下载
*
* @param id 文件ID
* @param os 输出流
* @param id 文件ID
* @param width 宽度
* @param height 高度
* @param os 输出流
* @throws IOException IOException
*/
public void download(String id, OutputStream os) throws IOException {
public void download(String id, Integer width, Integer height, OutputStream os) throws IOException {
//获得文件信息
DbFileInfoEntity fileInfo = this.getEntity(id);
//判空
@@ -204,6 +210,22 @@ public class DbFileService {
}
//获得文件路径
Path path = Paths.get(dbFileProperties.getMapping().get(fileInfo.getRootPathCode()) + fileInfo.getPhysicalRelativePath() + fileInfo.getPhysicalFileName());
//缩略图处理
if (DbFileConstant.CONTENT_TYPE_JPEG.equals(fileInfo.getContentType()) || DbFileConstant.CONTENT_TYPE_PNG.equals(fileInfo.getContentType())) {
if (width != null && height != null) {
Path thumbnailsPath = Paths.get(path.getParent().toString() + "/" + width + "x" + height + "/" + fileInfo.getPhysicalFileName());
if (!Files.exists(thumbnailsPath)) {
try {
Files.createDirectories(thumbnailsPath.getParent());
} catch (Exception e) {
log.warn("{} --> {} 创建文件目录失败,错误类型为 : {}", CoreUtil.getTraceId(), thumbnailsPath.getParent(), e.getClass().getName());
}
Files.createFile(thumbnailsPath);
Thumbnails.of(path.toFile()).size(width, height).keepAspectRatio(false).toFile(thumbnailsPath.toFile());
}
path = thumbnailsPath;
}
}
//输出文件
try (
OutputStream osi = new BufferedOutputStream(os);
@@ -1,5 +1,7 @@
package org.itkk.udf.starter.file.db.web;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.itkk.udf.starter.core.CoreConstant;
import org.itkk.udf.starter.core.CoreProperties;
import org.itkk.udf.starter.core.RestResponse;
@@ -7,8 +9,6 @@ import org.itkk.udf.starter.core.exception.ParameterValidException;
import org.itkk.udf.starter.file.db.DbFileProperties;
import org.itkk.udf.starter.file.db.dto.DbFileInfoDto;
import org.itkk.udf.starter.file.db.service.DbFileService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -105,15 +105,17 @@ public abstract class DbFileBaseController {
/**
* 文件预览
*
* @param id id
* @param id id
* @param width width
* @param height height
* @throws IOException IOException
*/
@GetMapping("preview/{id}")
public void preview(@PathVariable String id) throws IOException {
public void preview(@PathVariable String id, @RequestParam(name = "width", required = false) Integer width, @RequestParam(name = "height", required = false) Integer height) throws IOException {
//设置头信息
this.setResponseHeader(id, true);
//下载
dbFileService.download(id, response.getOutputStream());
dbFileService.download(id, width, height, response.getOutputStream());
}
/**
@@ -127,7 +129,7 @@ public abstract class DbFileBaseController {
//设置头信息
this.setResponseHeader(id, false);
//下载
dbFileService.download(id, response.getOutputStream());
dbFileService.download(id, null, null, response.getOutputStream());
}
/**