1. 五统一商户信息
This commit is contained in:
parent
0e8ada017c
commit
317f6852e0
|
|
@ -1,22 +1,24 @@
|
||||||
package com.chushang.common.excel.utils;
|
package com.chushang.common.excel.utils;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
import com.alibaba.excel.EasyExcel;
|
import com.alibaba.excel.EasyExcel;
|
||||||
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
|
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
|
||||||
import com.alibaba.excel.read.listener.ReadListener;
|
import com.alibaba.excel.read.listener.ReadListener;
|
||||||
|
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
|
||||||
import com.chushang.common.core.exception.ResultException;
|
import com.chushang.common.core.exception.ResultException;
|
||||||
import com.chushang.common.core.util.StringUtils;
|
import com.chushang.common.core.util.StringUtils;
|
||||||
|
import com.chushang.common.excel.convert.ExcelBigNumberConvert;
|
||||||
import com.chushang.common.excel.listener.DataListener;
|
import com.chushang.common.excel.listener.DataListener;
|
||||||
import com.chushang.common.excel.listener.DefaultExcelListener;
|
import com.chushang.common.excel.listener.DefaultExcelListener;
|
||||||
import com.chushang.common.excel.listener.ImportListener;
|
import com.chushang.common.excel.listener.ImportListener;
|
||||||
import com.chushang.common.excel.service.ExcelImporter;
|
import com.chushang.common.excel.service.ExcelImporter;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.BufferedInputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -26,6 +28,7 @@ import java.util.function.Consumer;
|
||||||
* by zhaowenyuan create 2021/12/27 10:49
|
* by zhaowenyuan create 2021/12/27 10:49
|
||||||
* 表格工具
|
* 表格工具
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class ExcelUtils {
|
public class ExcelUtils {
|
||||||
/**
|
/**
|
||||||
* 导出文件时为Writer生成OutputStream.
|
* 导出文件时为Writer生成OutputStream.
|
||||||
|
|
@ -51,13 +54,48 @@ public class ExcelUtils {
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
public static <T> void exportList(HttpServletResponse response, Class<T> clazz, List<T> list, String sheetName){
|
public static <T> void exportList(HttpServletResponse response, Class<T> clazz, List<T> list, String sheetName){
|
||||||
OutputStream outputStream = getOutputStream(response);
|
OutputStream outputStream = getOutputStream(response);
|
||||||
EasyExcel.write(outputStream, clazz)
|
exportList(outputStream, clazz, list, sheetName);
|
||||||
.sheet(sheetName)
|
|
||||||
.doWrite(list);
|
|
||||||
|
|
||||||
outputStream.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public static <T> void exportList(OutputStream outputStream, Class<T> clazz, List<T> list, String sheetName){
|
||||||
|
EasyExcel.write(outputStream, clazz)
|
||||||
|
// 自动适配
|
||||||
|
.registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
|
||||||
|
// 大数值自动转换 防止失真
|
||||||
|
.registerConverter(new ExcelBigNumberConvert())
|
||||||
|
.autoCloseStream(false)
|
||||||
|
.sheet(sheetName)
|
||||||
|
.doWrite(list);
|
||||||
|
}
|
||||||
|
public static <T> File exportExcel(List<T> list, Class<T> clazz, String path) {
|
||||||
|
File file = FileUtil.touch(path);
|
||||||
|
try (OutputStream outputStream = FileUtil.getOutputStream(file)) {
|
||||||
|
exportList(outputStream, clazz, list, "");
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("#MerInfoServiceImpl.exportMer# :", e);
|
||||||
|
Assert.isTrue(Boolean.FALSE, "文件生成失败");
|
||||||
|
}
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 导出excel
|
||||||
|
*
|
||||||
|
* @param response 响应类
|
||||||
|
* @param fileName 文件名
|
||||||
|
* @param sheetName sheet名
|
||||||
|
* @param dataList 数据列表
|
||||||
|
* @param clazz class类
|
||||||
|
* @param <T> 泛型
|
||||||
|
*/
|
||||||
|
@SneakyThrows
|
||||||
|
public static <T> void exportList(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
|
||||||
|
response.setContentType("application/vnd.ms-excel");
|
||||||
|
response.setCharacterEncoding("UTF-8");
|
||||||
|
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
|
||||||
|
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
||||||
|
exportList(response, clazz, dataList, sheetName);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用校验监听器 异步导入 同步返回
|
* 使用校验监听器 异步导入 同步返回
|
||||||
|
|
@ -165,24 +203,7 @@ public class ExcelUtils {
|
||||||
public static <T> void save(List<T> rowList, ExcelImporter<T> importer) {
|
public static <T> void save(List<T> rowList, ExcelImporter<T> importer) {
|
||||||
importer.save(rowList);
|
importer.save(rowList);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* 导出excel
|
|
||||||
*
|
|
||||||
* @param response 响应类
|
|
||||||
* @param fileName 文件名
|
|
||||||
* @param sheetName sheet名
|
|
||||||
* @param dataList 数据列表
|
|
||||||
* @param clazz class类
|
|
||||||
* @param <T> 泛型
|
|
||||||
*/
|
|
||||||
@SneakyThrows
|
|
||||||
public static <T> void exportList(HttpServletResponse response, String fileName, String sheetName, List<T> dataList, Class<T> clazz) {
|
|
||||||
response.setContentType("application/vnd.ms-excel");
|
|
||||||
response.setCharacterEncoding("UTF-8");
|
|
||||||
fileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
|
|
||||||
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
|
|
||||||
exportList(response, clazz, dataList, sheetName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> ExcelReaderBuilder getReaderBuilder(InputStream inputStream, ReadListener<T> readListener, Class<T> clazz) {
|
public static <T> ExcelReaderBuilder getReaderBuilder(InputStream inputStream, ReadListener<T> readListener, Class<T> clazz) {
|
||||||
return EasyExcel.read(inputStream, clazz, readListener);
|
return EasyExcel.read(inputStream, clazz, readListener);
|
||||||
|
|
|
||||||
|
|
@ -32,5 +32,20 @@ public interface RemoteOssService {
|
||||||
@PostMapping(value = "/uploadqccode")
|
@PostMapping(value = "/uploadqccode")
|
||||||
Result<String> uploadQcCode() throws Exception;
|
Result<String> uploadQcCode() throws Exception;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过 文件字节上传,
|
||||||
|
* 需要文件名称, 文件路径,
|
||||||
|
* 不需要 做ocr, 需要做 水印添加
|
||||||
|
* @param bytes 字节
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@PostMapping(value = "/upload/bytes")
|
||||||
|
Result<List<FileSourceVo>> uploadFile(@RequestParam(value = "bytes") byte[] bytes,
|
||||||
|
@RequestParam(value = "fileName") String fileName,
|
||||||
|
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||||
|
@RequestParam(value = "formats", required = false) String formats,
|
||||||
|
@RequestParam(value = "fileType", required = false) String fileType,
|
||||||
|
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,17 @@ public class RemoteFileController implements RemoteOssService {
|
||||||
return Result.ok(fileSourceService.uploadQcCode());
|
return Result.ok(fileSourceService.uploadQcCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件导出, 有可能是 excel, word, zip, 单图片等
|
||||||
|
* @param bytes 字节
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@SysLog(value = "feign文件", businessType = BusinessType.EXPORT)
|
||||||
|
@PostMapping(value = "/upload/bytes")
|
||||||
|
public Result<List<FileSourceVo>> uploadFile(byte[] bytes, String fileName, Boolean sealFlag, String formats, String fileType, String source)
|
||||||
|
{
|
||||||
|
return Result.ok(fileSourceService.uploadFile(bytes, fileName, sealFlag, formats, fileType));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,8 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -253,4 +255,63 @@ public class FileSourceService
|
||||||
save(fileSourceInfo);
|
save(fileSourceInfo);
|
||||||
return upload;
|
return upload;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FileSourceVo addFile(byte[] bytes, String fileName, Boolean sealFlag, String formats, String fileType)
|
||||||
|
{
|
||||||
|
String ip = IPUtils.clientIp(ServletUtils.getRequest());
|
||||||
|
String fid = generateFid();
|
||||||
|
String fName = fileName;
|
||||||
|
Tika t = new Tika();
|
||||||
|
String mimetype = null;
|
||||||
|
try {
|
||||||
|
mimetype = t.detect(bytes);
|
||||||
|
if (fName.indexOf(".") < 0) {//如果文件名字没有后缀
|
||||||
|
String ext = MimeTypes.getDefaultMimeTypes().forName(mimetype).getExtension();
|
||||||
|
fName += ext;
|
||||||
|
}
|
||||||
|
} catch (MimeTypeException e) {
|
||||||
|
log.error("文件后缀识别失败");
|
||||||
|
}
|
||||||
|
FileSourceInfo info = new FileSourceInfo();
|
||||||
|
try (ByteArrayInputStream in = new ByteArrayInputStream(bytes)){
|
||||||
|
info.setFid(fid);
|
||||||
|
info.setName(fName);
|
||||||
|
// mimeType 还不是 后缀 类型
|
||||||
|
info.setMimeType(mimetype);
|
||||||
|
// size 如何获取?
|
||||||
|
info.setSize((long) bytes.length);
|
||||||
|
String md5 = FileUtils.getMd5(in);
|
||||||
|
info.setMd5(md5);
|
||||||
|
String path = ossService.getPrefixPath(md5,fName);
|
||||||
|
// 不带https 的路径
|
||||||
|
info.setPath(path);
|
||||||
|
info.setStorage(uploadConfig.getStorage());
|
||||||
|
info.setUploadIp(ip);
|
||||||
|
// 上传到 oss 或者 minio
|
||||||
|
String realPath = ossService.upload(bytes, info, sealFlag, formats);
|
||||||
|
// 带https 的路径
|
||||||
|
info.setRealPath(realPath);
|
||||||
|
info.setFileType(fileType);
|
||||||
|
info.setBucketName(uploadConfig.getBucketName());
|
||||||
|
RMap<String, FileSourceInfo> map = redissonClient.getMap("OSS-Cache");
|
||||||
|
map.put(fid, info);
|
||||||
|
// 入库
|
||||||
|
save(info);
|
||||||
|
map.remove(info.getFid());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("上传文件失败", e);
|
||||||
|
}
|
||||||
|
FileSourceVo vo = new FileSourceVo();
|
||||||
|
vo.setFid(info.getFid());
|
||||||
|
vo.setFilePath(info.getRealPath());
|
||||||
|
vo.setName(fName);
|
||||||
|
vo.setSize(info.getSize());
|
||||||
|
return vo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public List<FileSourceVo> uploadFile(byte[] bytes, String fileName, Boolean sealFlag, String formats, String fileType) {
|
||||||
|
return Collections.singletonList(addFile(bytes, fileName, sealFlag, formats, fileType));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue