LocalSysFileServiceImpl.java 8.2 KB

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