LocalSysFileServiceImpl.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package com.boman.file.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.boman.common.core.utils.obj.ObjectUtils;
  4. import com.boman.common.core.utils.poi.ExcelUtil;
  5. import com.boman.common.redis.RedisKey;
  6. import com.boman.common.redis.service.RedisService;
  7. import com.boman.domain.GenTable;
  8. import com.boman.domain.GenTableColumn;
  9. import com.boman.domain.constant.MaskConstant;
  10. import com.boman.domain.dto.*;
  11. import com.boman.file.utils.FileUploadUtils;
  12. import com.boman.web.core.api.RemoteObjService;
  13. import com.google.common.collect.Lists;
  14. import org.apache.commons.io.IOUtils;
  15. import org.apache.commons.lang3.BooleanUtils;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.beans.factory.annotation.Value;
  19. import org.springframework.cloud.context.config.annotation.RefreshScope;
  20. import org.springframework.context.annotation.Primary;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.web.multipart.MultipartFile;
  23. import javax.annotation.Resource;
  24. import javax.servlet.http.HttpServletResponse;
  25. import java.io.*;
  26. import java.net.URL;
  27. import java.net.URLEncoder;
  28. import java.nio.charset.StandardCharsets;
  29. import java.util.*;
  30. import static com.boman.common.core.utils.obj.ObjectUtils.map;
  31. /**
  32. * 本地文件存储
  33. *
  34. * @author ruoyi
  35. */
  36. @Primary
  37. @Service
  38. @RefreshScope
  39. public class LocalSysFileServiceImpl implements ISysFileService
  40. {
  41. private static final Logger LOGGER = LoggerFactory.getLogger(LocalSysFileServiceImpl.class);
  42. @Resource
  43. private RemoteObjService remoteObjService;
  44. @Resource
  45. private RedisService redisService;
  46. /**
  47. * 资源映射路径 前缀
  48. */
  49. @Value("${file.prefix}")
  50. public String localFilePrefix;
  51. /**
  52. * 域名或本机访问地址
  53. */
  54. @Value("${file.domain}")
  55. public String domain;
  56. /**
  57. * 上传文件存储在本地的根路径
  58. */
  59. @Value("${file.path}")
  60. private String localFilePath;
  61. /**
  62. * 上传文件大小设置
  63. */
  64. @Value("${file.maxSize}")
  65. public long maxSize;
  66. /**
  67. * 本地文件上传接口
  68. *
  69. * @param file 上传的文件
  70. * @return 访问地址
  71. * @throws Exception
  72. */
  73. @Override
  74. public List<String> uploadFile(MultipartFile file) throws Exception {
  75. String name = FileUploadUtils.upload(localFilePath, file, maxSize);
  76. String absolutePath = localFilePath + name;
  77. String staticUrl = domain + localFilePrefix + name;
  78. LOGGER.info("上传静态路径路径为: {}, 绝对路径为: {}, 原文件名称: {}", staticUrl, absolutePath, file.getOriginalFilename());
  79. return Lists.newArrayList(staticUrl, absolutePath, file.getOriginalFilename());
  80. }
  81. /**
  82. * 功能描述: 上传base64
  83. *
  84. * @param base64 base64
  85. * @return java.lang.String
  86. */
  87. @Override
  88. public List<String> uploadFileBase64(String base64) throws IOException {
  89. MultipartFile multipartFile = FileUploadUtils.base64ToMultipart(base64);
  90. String name = FileUploadUtils.upload(localFilePath, multipartFile, maxSize);
  91. String absolutePath = localFilePath + "\\" + name;
  92. String staticUrl = domain + localFilePrefix + name;
  93. LOGGER.info("上传静态路径路径为: {}, 绝对路径为: {}", staticUrl, absolutePath);
  94. return Lists.newArrayList(staticUrl, absolutePath);
  95. }
  96. /**
  97. * 功能描述: 通用的导入接口
  98. *
  99. * @param multipartFile multipartFile
  100. * @param tableName tableName
  101. * @return java.util.List<com.alibaba.fastjson.JSONObject>
  102. */
  103. @Override
  104. public AjaxResult importExcelCommon(MultipartFile multipartFile, String tableName) throws Exception {
  105. Objects.requireNonNull(multipartFile, "multipartFile is empty");
  106. ObjectUtils.requireNonNull(tableName, "tableName is empty");
  107. GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  108. List<GenTableColumn> columns = genTable.getColumns();
  109. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  110. List<JSONObject> list = util.importCommonExcel("", multipartFile.getInputStream(), columns);
  111. ImportExcelDto dto = new ImportExcelDto();
  112. dto.setDataList(list);
  113. dto.setTableName(tableName);
  114. return remoteObjService.importCommonData(dto);
  115. }
  116. /**
  117. * 功能描述: 通用的导出接口
  118. *
  119. * @param response response
  120. * @param dto tableName
  121. * empty(true=>只带表头的excel,不含数据, false=>带数据的excel)
  122. * condition 查询条件
  123. * @return com.boman.domain.dto.AjaxResult
  124. */
  125. @Override
  126. public AjaxResult exportExcelCommon(HttpServletResponse response, ExportExcelDto dto) {
  127. String tableName = dto.getTableName();
  128. Boolean empty = dto.getEmpty();
  129. ObjectUtils.requireNonNull(tableName, "exportExcelCommon tableName is empty");
  130. ObjectUtils.requireNonNull(empty, "exportExcelCommon empty is empty");
  131. GenTable genTable = redisService.getCacheObject(RedisKey.TABLE_INFO + tableName);
  132. List<GenTableColumn> columns = genTable.getColumns();
  133. ExcelUtil<JSONObject> util = new ExcelUtil<>(JSONObject.class);
  134. List<Map<String, Object>> list = null;
  135. if (BooleanUtils.isTrue(empty)) {
  136. list = new ArrayList<>(0);
  137. } else {
  138. FormDataDto condition = new FormDataDto();
  139. condition.setTable(tableName);
  140. condition.setFixedData(new JSONObject(dto.getCondition()));
  141. AjaxResult ajaxResult = remoteObjService.getByMap(condition);
  142. if (AjaxResult.checkSuccess(ajaxResult)) {
  143. list = ((List<Map<String, Object>>) ajaxResult.get(AjaxResult.DATA_TAG));
  144. columns = ExcelUtil.filterData(columns, 4, MaskConstant.LIST_VISIBLE::equals);
  145. handleNullColumnValue(list, map(columns, GenTableColumn::getColumnName));
  146. }
  147. }
  148. try {
  149. util.exportExcelCommon(response, list, "sheet1", columns, empty);
  150. } catch (IOException e) {
  151. e.printStackTrace();
  152. }
  153. return null;
  154. }
  155. /**f
  156. * 功能描述: 通用的导入接口
  157. *
  158. * @param dto@return java.util.List<com.alibaba.fastjson.JSONObject>
  159. */
  160. @Override
  161. public List<JSONObject> importExcelCommon(ImportExcelDto dto) throws Exception {
  162. return null;
  163. }
  164. /**
  165. * 功能描述: 查看BooleanUtils.isTrue(dto.getPreview())
  166. * 下载BooleanUtils.isFalse(dto.getPreview())
  167. *
  168. * @param dto absolutePath preview
  169. * @param response response
  170. */
  171. @Override
  172. public void previewAndDownload(FileDto dto, HttpServletResponse response) throws RuntimeException, IOException {
  173. String filePath = dto.getAbsolutePath();
  174. File file = new File(filePath);
  175. if (!file.exists()) {
  176. response.sendError(404, "file is not exist, filePath = " + filePath);
  177. return;
  178. }
  179. response.reset();
  180. response.setCharacterEncoding("utf-8");
  181. String filename = URLEncoder.encode(dto.getFilename(), "UTF-8");
  182. if (BooleanUtils.isTrue(dto.getPreview())) {
  183. // 查看
  184. URL url = new URL("file:///" + filePath);
  185. response.setContentType(url.openConnection().getContentType() + ";charset=utf-8");
  186. response.setHeader("content-disposition", "attachment;filename=" + filename);
  187. } else {
  188. // 下载
  189. response.setContentType("application/x-msdownload");
  190. response.setHeader("Content-Disposition", "attachment;filename=" + filename);
  191. }
  192. int len;
  193. byte[] bs = new byte[1024];
  194. BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
  195. OutputStream out = response.getOutputStream();
  196. while ((len = br.read(bs)) > 0) {
  197. out.write(bs, 0, len);
  198. }
  199. out.flush();
  200. IOUtils.closeQuietly(out);
  201. IOUtils.closeQuietly(br);
  202. }
  203. public void handleNullColumnValue(List<Map<String, Object>> result, List<String> showData) {
  204. for (Map<String, Object> map : result) {
  205. Set<String> resultKeySet = map.keySet();
  206. for (String columnName : showData) {
  207. if (!resultKeySet.contains(columnName)) {
  208. map.put(columnName, "");
  209. }
  210. }
  211. }
  212. }
  213. }