123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- package com.boman.file.service;
- import com.alibaba.fastjson.JSONObject;
- import com.boman.common.core.utils.obj.ObjectUtils;
- import com.boman.common.core.utils.poi.ExcelUtil;
- import com.boman.domain.dto.*;
- import com.boman.common.redis.RedisKey;
- import com.boman.common.redis.service.RedisService;
- import com.boman.domain.GenTable;
- import com.boman.domain.GenTableColumn;
- import com.boman.domain.constant.MaskConstant;
- import com.boman.file.utils.FileUploadUtils;
- import com.boman.web.core.api.RemoteObjService;
- import com.google.common.collect.Lists;
- import org.apache.commons.io.IOUtils;
- import org.apache.commons.lang3.BooleanUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.cloud.context.config.annotation.RefreshScope;
- import org.springframework.context.annotation.Primary;
- import org.springframework.stereotype.Service;
- import org.springframework.web.multipart.MultipartFile;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.net.URL;
- import java.util.*;
- import static com.boman.common.core.utils.obj.ObjectUtils.map;
- /**
- * 本地文件存储
- *
- * @author ruoyi
- */
- @Primary
- @Service
- @RefreshScope
- public class LocalSysFileServiceImpl implements ISysFileService
- {
- private static final Logger LOGGER = LoggerFactory.getLogger(LocalSysFileServiceImpl.class);
- @Resource
- private RemoteObjService remoteObjService;
- @Resource
- private RedisService redisService;
- /**
- * 资源映射路径 前缀
- */
- @Value("${file.prefix}")
- public String localFilePrefix;
- /**
- * 域名或本机访问地址
- */
- @Value("${file.domain}")
- public String domain;
-
- /**
- * 上传文件存储在本地的根路径
- */
- @Value("${file.path}")
- private String localFilePath;
- /**
- * 上传文件大小设置
- */
- @Value("${file.maxSize}")
- public long maxSize;
- /**
- * 本地文件上传接口
- *
- * @param file 上传的文件
- * @return 访问地址
- * @throws Exception
- */
- @Override
- public List<String> uploadFile(MultipartFile file) throws Exception {
- String name = FileUploadUtils.upload(localFilePath, file, maxSize);
- String absolutePath = localFilePath + "\\" + name;
- String staticUrl = domain + localFilePrefix + name;
- LOGGER.info("上传静态路径路径为: {}, 绝对路径为: {}, 原文件名称: {}", staticUrl, absolutePath, file.getOriginalFilename());
- return Lists.newArrayList(staticUrl, absolutePath, file.getOriginalFilename());
- }
- /**
- * 功能描述: 上传base64
- *
- * @param base64 base64
- * @return java.lang.String
- */
- @Override
- public List<String> uploadFileBase64(String base64) throws IOException {
- MultipartFile multipartFile = FileUploadUtils.base64ToMultipart(base64);
- String name = FileUploadUtils.upload(localFilePath, multipartFile, maxSize);
- String absolutePath = localFilePath + "\\" + name;
- String staticUrl = domain + localFilePrefix + name;
- LOGGER.info("上传静态路径路径为: {}, 绝对路径为: {}", staticUrl, absolutePath);
- return Lists.newArrayList(staticUrl, absolutePath);
- }
- /**
- * 功能描述: 通用的导入接口
- *
- * @param multipartFile multipartFile
- * @param tableName tableName
- * @return java.util.List<com.alibaba.fastjson.JSONObject>
- */
- @Override
- public AjaxResult importExcelCommon(MultipartFile multipartFile, String tableName) throws Exception {
- Objects.requireNonNull(multipartFile, "multipartFile is empty");
- ObjectUtils.requireNonNull(tableName, "tableName is empty");
- GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
- List<GenTableColumn> columns = genTable.getColumns();
- ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
- List<JSONObject> list = util.importCommonExcel("", multipartFile.getInputStream(), columns);
- ImportExcelDto dto = new ImportExcelDto();
- dto.setDataList(list);
- dto.setTableName(tableName);
- return remoteObjService.importCommonData(dto);
- }
- /**
- * 功能描述: 通用的导出接口
- *
- * @param response response
- * @param dto tableName
- * empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
- * condition 查询条件
- * @return com.boman.domain.dto.AjaxResult
- */
- @Override
- public AjaxResult exportExcelCommon(HttpServletResponse response, ExportExcelDto dto) {
- String tableName = dto.getTableName();
- Boolean empty = dto.getEmpty();
- ObjectUtils.requireNonNull(tableName, "exportExcelCommon tableName is empty");
- ObjectUtils.requireNonNull(empty, "exportExcelCommon empty is empty");
- GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
- List<GenTableColumn> columns = genTable.getColumns();
- ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
- List<Map<String, Object>> list = null;
- if (BooleanUtils.isTrue(empty)) {
- list = new ArrayList<>(0);
- } else {
- FormDataDto condition = new FormDataDto();
- condition.setTable(tableName);
- condition.setFixedData(new JSONObject(dto.getCondition()));
- AjaxResult ajaxResult = remoteObjService.getByMap(condition);
- if (AjaxResult.checkSuccess(ajaxResult)) {
- list = ((List<Map<String, Object>>) ajaxResult.get(AjaxResult.DATA_TAG));
- columns = ExcelUtil.filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
- handleNullColumnValue(list, map(columns, GenTableColumn::getColumnName));
- }
- }
- try {
- util.exportExcelCommon(response, list, "sheet1", columns, empty);
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**f
- * 功能描述: 通用的导入接口
- *
- * @param dto@return java.util.List<com.alibaba.fastjson.JSONObject>
- */
- @Override
- public List<JSONObject> importExcelCommon(ImportExcelDto dto) throws Exception {
- return null;
- }
- /**
- * 功能描述: 查看BooleanUtils.isTrue(dto.getPreview())
- * 下载BooleanUtils.isFalse(dto.getPreview())
- *
- * @param dto absolutePath preview
- * @param response response
- */
- @Override
- public void previewAndDownload(FileDto dto, HttpServletResponse response) throws RuntimeException, IOException {
- String filePath = dto.getAbsolutePath();
- File file = new File(filePath);
- if (!file.exists()) {
- response.sendError(404, "file is not exist, filePath = " + filePath);
- return;
- }
- response.reset();
- if (BooleanUtils.isTrue(dto.getPreview())) {
- // 查看
- URL url = new URL("file:///" + filePath);
- String contentType = url.openConnection().getContentType();
- response.setContentType(contentType);
- response.setHeader("Content-Disposition", "inline;filename=test.pdf");
- } else {
- // 下载
- response.setContentType("application/x-msdownload");
- response.setHeader("Content-Disposition", "attachment;filename=test.pdf");
- }
- int len;
- byte[] bs = new byte[1024];
- BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
- OutputStream out = response.getOutputStream();
- while ((len = br.read(bs)) > 0) {
- out.write(bs, 0, len);
- }
- out.flush();
- IOUtils.closeQuietly(out);
- IOUtils.closeQuietly(br);
- }
- public void handleNullColumnValue(List<Map<String, Object>> result, List<String> showData) {
- for (Map<String, Object> map : result) {
- Set<String> resultKeySet = map.keySet();
- for (String columnName : showData) {
- if (!resultKeySet.contains(columnName)) {
- map.put(columnName, "");
- }
- }
- }
- }
- }
|