支持文件保存扩展

This commit is contained in:
mazhicheng 2020-03-12 19:32:46 +08:00
parent 206ce5dc12
commit b3b050479c
11 changed files with 82 additions and 43 deletions

View File

@ -191,6 +191,9 @@ public class SqlHandler {
if(jdbcUrl == null){
String master = environment.getProperty("spring.datasource.dynamic.primary");
jdbcUrl = environment.getProperty("spring.datasource.dynamic.datasource."+master+".url");
if(jdbcUrl == null){
log.warn("无法获取 datasource url 配置,请检查!");
}
}
return jdbcUrl;
}

View File

@ -1,5 +1,6 @@
package com.diboot.file.controller;
import com.diboot.file.entity.UploadFile;
import com.diboot.file.excel.listener.FixedHeadExcelListener;
import com.diboot.file.util.ExcelHelper;
import com.diboot.file.util.FileHelper;
@ -45,7 +46,7 @@ public abstract class BaseExcelFileController extends BaseFileController {
public JsonResult excelPreview(MultipartFile file, HttpServletRequest request) throws Exception {
Map<String, Object> dataMap = new HashMap();
savePreviewExcelFile(file, request, dataMap);
return new JsonResult(Status.OK, dataMap);
return JsonResult.OK(dataMap);
}
/***
@ -62,9 +63,14 @@ public abstract class BaseExcelFileController extends BaseFileController {
String fileUid = S.substringBefore(previewFileName, ".");
String fullPath = FileHelper.getFullPath(previewFileName);
String ext = FileHelper.getFileExtByName(originFileName);
// 描述
String description = getString(request, "description");
// 保存文件上传记录
createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, request);
return new JsonResult(Status.OK);
UploadFile uploadFile = new UploadFile().setUuid(fileUid)
.setFileName(originFileName).setStoragePath(fullPath).setFileType(ext)
.setRelObjType(entityClass.getSimpleName()).setDescription(description);
super.createUploadFile(uploadFile, request);
return JsonResult.OK();
}
/***
@ -76,7 +82,7 @@ public abstract class BaseExcelFileController extends BaseFileController {
*/
public <T> JsonResult uploadExcelFile(MultipartFile file, Class<T> entityClass, HttpServletRequest request) throws Exception {
checkIsExcel(file);
return uploadFile(file, entityClass, request);
return super.uploadFile(file, entityClass, request);
}
/**
@ -87,29 +93,25 @@ public abstract class BaseExcelFileController extends BaseFileController {
*/
private void savePreviewExcelFile(MultipartFile file, HttpServletRequest request, Map<String, Object> dataMap) throws Exception{
checkIsExcel(file);
// 文件后缀
String fileName = file.getOriginalFilename();
String ext = FileHelper.getFileExtByName(fileName);
// 先保存文件
String newFileName = S.newUuid() + "." + ext;
String fullPath = FileHelper.saveFile(file, newFileName);
// 保存文件到本地
UploadFile uploadFile = super.saveFile(file, getExcelDataListener().getExcelModelClass(), request);
// 预览
FixedHeadExcelListener listener = getExcelDataListener();
listener.setRequestParams(super.getParamsMap(request));
try {
ExcelHelper.previewReadExcel(fullPath, listener);
ExcelHelper.previewReadExcel(uploadFile.getStoragePath(), listener);
}
catch (Exception e) {
log.warn("解析并校验excel文件失败", e);
if(V.notEmpty(e.getMessage())){
throw new Exception(e.getMessage()); //.replaceAll("; ", "<br/>")
throw new Exception(e.getMessage());
}
throw e;
}
// 绑定属性到model
dataMap.put("header", listener.getFieldHeaders());
dataMap.put(ORIGIN_FILE_NAME, fileName);
dataMap.put(PREVIEW_FILE_NAME, newFileName);
dataMap.put(ORIGIN_FILE_NAME, file.getOriginalFilename());
dataMap.put(PREVIEW_FILE_NAME, FileHelper.getFileName(uploadFile.getStoragePath()));
List dataList = listener.getDataList();
if(V.notEmpty(dataList) && dataList.size() > BaseConfig.getPageSize()){
dataList = dataList.subList(0, BaseConfig.getPageSize());

View File

@ -43,7 +43,7 @@ public abstract class BaseFileController extends BaseController {
// 查询当前页的数据
List entityList = uploadFileService.getEntityList(queryWrapper, pagination);
// 返回结果
return new JsonResult(Status.OK, entityList).bindPagination(pagination);
return JsonResult.OK(entityList).bindPagination(pagination);
}
/***
@ -62,39 +62,55 @@ public abstract class BaseFileController extends BaseController {
log.debug("非法的文件类型: " + originFileName);
throw new BusinessException(Status.FAIL_VALIDATION, "请上传合法的文件格式!");
}
// 保存文件
UploadFile uploadFile = saveFile(file, entityClass, request);
// 保存上传记录
createUploadFile(uploadFile, request);
// 返回结果
Map<String, String> dataMap = new HashMap<>();
dataMap.put("uuid", uploadFile.getUuid());
dataMap.put("accessUrl", uploadFile.getAccessUrl());
return JsonResult.OK(dataMap);
}
/**
* 保存文件
* @param file
* @param entityClass
* @param request
* @param <T>
* @return
* @throws Exception
*/
protected <T> UploadFile saveFile(MultipartFile file, Class<T> entityClass, HttpServletRequest request) throws Exception{
// 文件后缀
String ext = FileHelper.getFileExtByName(originFileName);
String originFileName = file.getOriginalFilename();
String ext = FileHelper.getFileExtByName(file.getOriginalFilename());
// 先保存文件
String fileUid = S.newUuid();
String newFileName = fileUid + "." + ext;
String fullPath = FileHelper.saveFile(file, newFileName);
// 保存文件上传记录
createUploadFile(entityClass, fileUid, originFileName, fullPath, ext, request);
Map<String, String> dataMap = new HashMap<>();
dataMap.put("uuid", fileUid);
return new JsonResult(Status.OK).data(dataMap);
String storageFullPath = FileHelper.saveFile(file, newFileName);
UploadFile uploadFile = new UploadFile();
uploadFile.setUuid(fileUid).setFileName(originFileName).setFileType(ext);
uploadFile.setRelObjType(entityClass.getSimpleName()).setStoragePath(storageFullPath);
String description = getString(request, "description");
uploadFile.setDescription(description);
// 返回uploadFile对象
return uploadFile;
}
/**
* 保存上传文件信息
* @param entityClass
* @param fileUid
* @param originFileName
* @param storagePath
* @param fileType
* @param uploadFile
* @param request
* @param <T>
* @throws Exception
*/
protected <T> void createUploadFile(Class<T> entityClass, String fileUid, String originFileName, String storagePath, String fileType, HttpServletRequest request) throws Exception{
protected void createUploadFile(UploadFile uploadFile, HttpServletRequest request) throws Exception{
// 保存文件之后的处理逻辑
int dataCount = extractDataCount(fileUid, storagePath, request);
UploadFile uploadFile = new UploadFile();
uploadFile.setUuid(fileUid).setFileName(originFileName).setFileType(fileType);
uploadFile.setRelObjType(entityClass.getSimpleName()).setStoragePath(storagePath);
// 初始设置为0批量保存数据后更新
String description = getString(request, "description");
uploadFile.setDataCount(dataCount).setDescription(description);
int dataCount = extractDataCount(uploadFile.getUuid(), uploadFile.getStoragePath(), request);
uploadFile.setDataCount(dataCount);
// 保存文件上传记录
uploadFileService.createEntity(uploadFile);
}

View File

@ -45,6 +45,12 @@ public class UploadFile extends BaseEntity {
@JSONField(serialize = false)
private String storagePath;
/**
* 访问URL
*/
@TableField
private String accessUrl;
@TableField
private String fileType;

View File

@ -26,17 +26,17 @@ public abstract class FixedHeadExcelListener<T extends BaseExcelModel> extends A
//解析出的excel表头
protected Map<Integer, String> headMap;
// 字段名-列名的映射
protected LinkedHashMap<String, String> fieldHeadMap;
private LinkedHashMap<String, String> fieldHeadMap;
//解析后的数据实体list
protected List<T> dataList = new ArrayList<>();
private List<T> dataList = new ArrayList<>();
//错误信息
private List<String> validateErrorMsgs = new ArrayList<>();
// 导入文件的uuid
protected String uploadFileUuid;
// 是否为预览模式
private boolean preview = false;
// 注入request
private Map<String, Object> requestParams;
// 是否为预览模式
private boolean preview = false;
// 导入文件的uuid
protected String uploadFileUuid;
public FixedHeadExcelListener(){
}
@ -186,6 +186,10 @@ public abstract class FixedHeadExcelListener<T extends BaseExcelModel> extends A
return this.fieldHeadMap;
}
/**
* 获取FieldHeadMap顺序转换的list列表
* @return
*/
public List<Map<String, String>> getFieldHeaders(){
if (V.isEmpty(this.fieldHeadMap)) {
return Collections.emptyList();

View File

@ -6,6 +6,7 @@ CREATE TABLE upload_file (
file_name varchar(100) NOT NULL COMMENT '文件名',
storage_path varchar(200) NOT NULL COMMENT '存储路径',
file_type varchar(20) DEFAULT NULL COMMENT '文件类型',
access_url varchar(200) NULL COMMENT '访问地址',
data_count int DEFAULT 0 COMMENT '数据量',
description varchar(100) DEFAULT NULL COMMENT '备注',
is_deleted tinyint(1) default 0 not null comment '是否删除',

View File

@ -5,6 +5,7 @@ CREATE TABLE upload_file (
rel_obj_id bigint DEFAULT NULL COMMENT '关联对象ID',
file_name varchar(100) NOT NULL COMMENT '文件名',
storage_path varchar(200) NOT NULL COMMENT '存储路径',
access_url varchar(200) NULL COMMENT '访问地址',
file_type varchar(20) DEFAULT NULL COMMENT '文件类型',
data_count int DEFAULT 0 COMMENT '数据量',
description varchar(100) DEFAULT NULL COMMENT '备注',

View File

@ -5,6 +5,7 @@ CREATE TABLE ${SCHEMA}.upload_file (
rel_obj_id NUMBER(20),
file_name VARCHAR2(100) NOT NULL,
storage_path VARCHAR2(200) NOT NULL,
access_url VARCHAR2(200),
file_type VARCHAR2(20),
data_count NUMBER(9) DEFAULT 0 not null,
description VARCHAR2(100),
@ -18,6 +19,7 @@ comment on column ${SCHEMA}.upload_file.rel_obj_type is '关联对象类';
comment on column ${SCHEMA}.upload_file.rel_obj_id is '关联对象ID';
comment on column ${SCHEMA}.upload_file.file_name is '文件名';
comment on column ${SCHEMA}.upload_file.storage_path is '存储路径';
comment on column ${SCHEMA}.upload_file.access_url is '访问地址';
comment on column ${SCHEMA}.upload_file.file_type is '文件类型';
comment on column ${SCHEMA}.upload_file.data_count is '数据量';
comment on column ${SCHEMA}.upload_file.description is '备注';

View File

@ -5,6 +5,7 @@ CREATE TABLE upload_file (
rel_obj_id bigint,
file_name varchar(100) NOT NULL,
storage_path varchar(200) NOT NULL,
access_url varchar(200),
file_type varchar(20),
data_count int not null DEFAULT 0,
description varchar(100),
@ -18,6 +19,7 @@ comment on column upload_file.rel_obj_type is '关联对象类';
comment on column upload_file.rel_obj_id is '关联对象ID';
comment on column upload_file.file_name is '文件名';
comment on column upload_file.storage_path is '存储路径';
comment on column upload_file.access_url is '访问地址';
comment on column upload_file.file_type is '文件类型';
comment on column upload_file.data_count is '数据量';
comment on column upload_file.description is '备注';

View File

@ -5,6 +5,7 @@ CREATE TABLE ${SCHEMA}.upload_file (
rel_obj_id bigint,
file_name varchar(100) NOT NULL,
storage_path varchar(200) NOT NULL,
access_url varchar(200) NULL,
file_type varchar(20),
data_count int not null DEFAULT 0,
description varchar(100),
@ -18,6 +19,7 @@ execute sp_addextendedproperty 'MS_Description', N'关联对象类', 'SCHEMA', '
execute sp_addextendedproperty 'MS_Description', N'关联对象ID', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'rel_obj_id';
execute sp_addextendedproperty 'MS_Description', N'文件名', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'file_name';
execute sp_addextendedproperty 'MS_Description', N'存储路径', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'storage_path';
execute sp_addextendedproperty 'MS_Description', N'访问地址', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'access_url';
execute sp_addextendedproperty 'MS_Description', N'文件类型', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'file_type';
execute sp_addextendedproperty 'MS_Description', N'数据量', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'data_count';
execute sp_addextendedproperty 'MS_Description', N'备注', 'SCHEMA', '${SCHEMA}', 'table', upload_file, 'column', 'description';

View File

@ -11,7 +11,7 @@
</parent>
<artifactId>diboot-iam-base-spring-boot-starter</artifactId>
<version>2.0.4-RC2</version>
<version>2.0.4</version>
<packaging>jar</packaging>
<description>diboot IAM base project</description>