FileUploadUtils.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. package jnpf.util;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import cn.hutool.core.io.file.FileNameUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import jnpf.constant.GenerateConstant;
  6. import jnpf.constant.MsgCode;
  7. import jnpf.entity.FileDetail;
  8. import jnpf.entity.FileParameter;
  9. import jnpf.exception.DataException;
  10. import jnpf.model.FileListVO;
  11. import jnpf.util.context.SpringContext;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.dromara.x.file.storage.core.FileInfo;
  14. import org.dromara.x.file.storage.core.FileStorageProperties;
  15. import org.dromara.x.file.storage.core.FileStorageService;
  16. import org.dromara.x.file.storage.core.get.*;
  17. import org.dromara.x.file.storage.core.platform.FileStorage;
  18. import org.dromara.x.file.storage.core.upload.UploadPretreatment;
  19. import org.dromara.x.file.storage.core.util.Tools;
  20. import org.dromara.x.file.storage.spring.SpringFileStorageProperties;
  21. import org.springframework.util.Assert;
  22. import org.springframework.util.ReflectionUtils;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.imageio.ImageIO;
  25. import java.io.File;
  26. import java.io.InputStream;
  27. import java.io.OutputStream;
  28. import java.lang.reflect.Field;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.List;
  32. import java.util.Objects;
  33. import java.util.function.Consumer;
  34. @Slf4j
  35. public class FileUploadUtils {
  36. private static final String PLATFORM_LOCAL = "local-plus-1";
  37. private static FileStorageService fileStorageService;
  38. // private static FileDetailService fileDetailService;
  39. private static SpringFileStorageProperties fileStorageProperties;
  40. private static List<String> imagesReaderSuffix;
  41. private static List<String> imagesWriterSuffix;
  42. static {
  43. fileStorageService = SpringContext.getBean(FileStorageService.class);
  44. // fileDetailService = SpringContext.getBean(FileDetailService.class);
  45. fileStorageProperties = SpringContext.getBean(SpringFileStorageProperties.class);
  46. //获取支持读取的图片后缀
  47. imagesReaderSuffix = Arrays.asList(ImageIO.getReaderFormatNames());
  48. //获取支持写入的图片后缀
  49. imagesWriterSuffix = Arrays.asList(ImageIO.getWriterFormatNames());
  50. }
  51. public static FileStorageProperties.LocalPlusConfig getLocalStorageConfig() {
  52. List<? extends FileStorageProperties.LocalPlusConfig> localPlus = fileStorageProperties.getLocalPlus();
  53. Assert.noNullElements(localPlus, "未配置本地存储");
  54. return localPlus.get(0);
  55. }
  56. /**
  57. * 获取文件信息
  58. *
  59. * @return
  60. */
  61. public static FileDetail getFileDetail(FileParameter parameter) {
  62. FileDetail fileDetail = new FileDetail();
  63. fileDetail.setPath(parameter.getRemotePath());
  64. fileDetail.setFilename(parameter.getRemoteFileName());
  65. String basePath;
  66. String platform;
  67. FileStorage fileStorage = fileStorageService.getFileStorage();
  68. if (parameter.isOrgin()) {
  69. basePath = getLocalBasePath();
  70. platform = PLATFORM_LOCAL;
  71. } else {
  72. basePath = getBasePath(fileStorage);
  73. platform = getDefaultPlatform();
  74. }
  75. fileDetail.setBasePath(basePath);
  76. fileDetail.setPlatform(platform);
  77. return fileDetail;
  78. }
  79. // /**
  80. // * 获取文件url
  81. // *
  82. // * @param path
  83. // * @param fileName
  84. // * @return
  85. // */
  86. // public static String getFileUrl(String path, String fileName) {
  87. // FileDetail fileDetail = getFileDetail(path, fileName, );
  88. // String url = null;
  89. // if (fileDetail == null) {
  90. // url = fileStorageService.getFileStorage().getBasePath() + path + fileName;
  91. // } else {
  92. // url = fileDetail.getUrl();
  93. // }
  94. // return url;
  95. // }
  96. /**
  97. * 返回本地地址且固定为local-plus-1
  98. *
  99. * @return
  100. */
  101. public static String getLocalBasePath() {
  102. FileStorageProperties.LocalPlusConfig localStorageConfig = getLocalStorageConfig();
  103. return Tools.getNotNull(localStorageConfig.getStoragePath(), StrUtil.EMPTY)
  104. + Tools.getNotNull(localStorageConfig.getBasePath(), StrUtil.EMPTY);
  105. }
  106. /**
  107. * 获取指定存储平台基础路径
  108. *
  109. * @return
  110. */
  111. public static String getBasePath(FileStorage fileStorage) {
  112. try {
  113. if (fileStorage != null) {
  114. Field field = ReflectionUtils.findField(fileStorage.getClass(), "basePath");
  115. if (field != null) {
  116. ReflectionUtils.makeAccessible(field);
  117. String basePath = (String) ReflectionUtils.getField(field, fileStorage);
  118. if (StringUtil.isNotEmpty(basePath)) {
  119. return basePath;
  120. }
  121. }
  122. }
  123. } catch (Exception e) {
  124. }
  125. return "";
  126. }
  127. /**
  128. * 获取当前存储平台基础路径
  129. *
  130. * @return
  131. */
  132. public static String getBasePath() {
  133. return getBasePath(fileStorageService.getFileStorage());
  134. }
  135. /**
  136. * 获取文件信息
  137. *
  138. * @return
  139. */
  140. public static FileInfo getFileInfo(FileParameter parameter) {
  141. FileDetail fileDetail = getFileDetail(parameter);
  142. return BeanUtil.copyProperties(fileDetail, FileInfo.class, "attr");
  143. }
  144. public static FileInfo getRemoteFileInfo(FileParameter parameter) {
  145. process(parameter);
  146. GetFilePretreatment getFilePretreatment = fileStorageService.getFile();
  147. getFilePretreatment.setPath(parameter.getRemotePath());
  148. getFilePretreatment.setFilename(parameter.getRemoteFileName());
  149. RemoteFileInfo remoteFileInfo = getFilePretreatment.getFile();
  150. return remoteFileInfo.toFileInfo();
  151. }
  152. /**
  153. * 上传文件,通过字节数组
  154. * 减少使用, 无法上传大于2G的文件
  155. *
  156. * @param parameter 文件参数
  157. * @param bytes 内容
  158. */
  159. public static FileInfo uploadFile(FileParameter parameter, byte[] bytes) {
  160. process(parameter);
  161. UploadPretreatment uploadPretreatment = fileStorageService.of(bytes);
  162. FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
  163. Assert.notNull(fileInfo, MsgCode.FA033.get());
  164. return fileInfo;
  165. }
  166. /**
  167. * 上传文件,MultipartFile
  168. *
  169. * @param parameter 文件参数
  170. * @param multipartFile 文件
  171. */
  172. public static FileInfo uploadFile(FileParameter parameter, MultipartFile multipartFile) {
  173. process(parameter);
  174. UploadPretreatment uploadPretreatment = fileStorageService.of(multipartFile);
  175. FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
  176. Assert.notNull(fileInfo, MsgCode.FA033.get());
  177. return fileInfo;
  178. }
  179. /**
  180. * 上传文件,MultipartFile
  181. *
  182. * @param parameter 文件参数
  183. * @param inputStream 文件
  184. */
  185. public static FileInfo uploadFile(FileParameter parameter, InputStream inputStream) {
  186. process(parameter);
  187. UploadPretreatment uploadPretreatment = fileStorageService.of(inputStream);
  188. FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
  189. Assert.notNull(fileInfo, MsgCode.FA033.get());
  190. return fileInfo;
  191. }
  192. /**
  193. * 上传文件,MultipartFile
  194. *
  195. * @param parameter 文件参数
  196. * @param file 文件
  197. */
  198. public static FileInfo uploadFile(FileParameter parameter, File file) {
  199. process(parameter);
  200. UploadPretreatment uploadPretreatment = fileStorageService.of(file);
  201. FileInfo fileInfo = uploadInner(parameter, uploadPretreatment);
  202. Assert.notNull(fileInfo, MsgCode.FA033.get());
  203. return fileInfo;
  204. }
  205. private static FileInfo uploadInner(FileParameter parameter, UploadPretreatment uploadPretreatment) {
  206. process(parameter);
  207. uploadPretreatment.setPath(parameter.getRemotePath())
  208. //设置上传文件名
  209. //.setSaveFilename(StringUtil.isNotEmpty(parameter.getRemoteFileName()), parameter.getRemoteFileName())
  210. .setOriginalFilename(StringUtil.isNotEmpty(parameter.getRemoteFileName()), parameter.getRemoteFileName());
  211. if (parameter.isThumbnail()) {
  212. String ext = FileNameUtil.getSuffix(StringUtil.isNotEmpty(parameter.getLocalFileName()) ? parameter.getLocalFileName() : parameter.getRemoteFileName());
  213. if (imagesReaderSuffix.contains(ext.toLowerCase())) {
  214. if (!imagesWriterSuffix.contains(ext.toLowerCase())) {
  215. if (log.isDebugEnabled()) {
  216. log.debug("不支持当前配置的缩略图格式:{}", fileStorageProperties.getThumbnailSuffix());
  217. }
  218. } else {
  219. try {
  220. uploadPretreatment.thumbnail(120, 120);
  221. } catch (Exception e) {
  222. if (log.isDebugEnabled()) {
  223. log.debug("生成缩略图失败: {}", e.getMessage());
  224. }
  225. }
  226. }
  227. }
  228. }
  229. return uploadPretreatment.upload();
  230. }
  231. /**
  232. * 获取文件列表
  233. */
  234. public static List<FileListVO> getFileList(FileParameter parameter) {
  235. FileStorage fileStorage = fileStorageService.getFileStorage();
  236. if (!fileStorage.isSupportListFiles().getIsSupport()) {
  237. throw new DataException(MsgCode.FA056.get());
  238. }
  239. process(parameter);
  240. List<FileListVO> listVOS = new ArrayList<>();
  241. getFileListInner(parameter, fileStorage, listVOS);
  242. return listVOS;
  243. }
  244. private static void getFileListInner(FileParameter parameter, FileStorage fileStorage, List<FileListVO> resultFiles) {
  245. process(parameter);
  246. ListFilesPretreatment listFilesPretreatment = new ListFilesPretreatment();
  247. listFilesPretreatment.setPath(parameter.getRemotePath());
  248. listFilesPretreatment.setMaxFiles(fileStorage.isSupportListFiles().getSupportMaxFiles());
  249. ListFilesResult listFilesResult = fileStorage.listFiles(listFilesPretreatment);
  250. List<RemoteFileInfo> fileList = listFilesResult.getFileList();
  251. for (int i = 0; i < fileList.size(); i++) {
  252. RemoteFileInfo remoteFileInfo = fileList.get(i);
  253. FileListVO fileListVO = new FileListVO();
  254. fileListVO.setFileId(i + "");
  255. fileListVO.setFileName(remoteFileInfo.getFilename());
  256. fileListVO.setFilePath(remoteFileInfo.getPath());
  257. fileListVO.setFileType(remoteFileInfo.getExt());
  258. fileListVO.setFileSize(String.valueOf(remoteFileInfo.getSize()));
  259. fileListVO.setFileTime(DateUtil.dateFormat(remoteFileInfo.getLastModified()));
  260. resultFiles.add(fileListVO);
  261. }
  262. if (parameter.isRecursive()) {
  263. List<RemoteDirInfo> dirList = listFilesResult.getDirList();
  264. for (RemoteDirInfo remoteDirInfo : dirList) {
  265. parameter.setRemotePath(remoteDirInfo.getPath() + remoteDirInfo.getName() + "/");
  266. getFileListInner(parameter, fileStorage, resultFiles);
  267. }
  268. }
  269. }
  270. /**
  271. * 获取命名空间
  272. */
  273. // public static String getBucketName() {
  274. // String bucketName = fileStorageService.getFileStorage().getBucketName();
  275. // if (StringUtil.isNotEmpty(bucketName)) {
  276. // return bucketName + "/";
  277. // }
  278. // return bucketName;
  279. // return "";
  280. // }
  281. /**
  282. * 获取命名空间
  283. */
  284. // public static String getDomain() {
  285. // return fileStorageService.getFileStorage().getDomain();
  286. // return "";
  287. // }
  288. /**
  289. * 删除文件
  290. */
  291. public static boolean removeFile(FileParameter parameter) {
  292. process(parameter);
  293. FileDetail fileDetail = getFileDetail(parameter);
  294. return fileStorageService.delete(fileDetail.getUrl());
  295. }
  296. // /**
  297. // * 下载文件
  298. // *
  299. // * @param path
  300. // * @param fileName
  301. // */
  302. // public static void downloadFile(String path, String fileName) {
  303. // String fileUrl = getFileUrl(path, fileName);
  304. // FileInfo fileInfo = fileStorageService.getFileInfoByUrl(fileUrl);
  305. // fileStorageService.download(fileInfo).file(fileUrl);
  306. // }
  307. /**
  308. * 下载文件
  309. */
  310. public static File downloadFileToLocal(FileParameter parameter) {
  311. process(parameter);
  312. if (StringUtil.isEmpty(parameter.getLocalFileName())) {
  313. parameter.setLocalFileName(parameter.getRemoteFileName());
  314. }
  315. if (StringUtil.isEmpty(parameter.getLocaFilelPath())) {
  316. parameter.setLocaFilelPath(parameter.getRemotePath());
  317. }
  318. FileStorageProperties.LocalPlusConfig localStorageConfig = getLocalStorageConfig();
  319. String localPath = Tools.getNotNull(localStorageConfig.getStoragePath(), StrUtil.EMPTY)
  320. + Tools.getNotNull(localStorageConfig.getBasePath(), StrUtil.EMPTY)
  321. + Tools.getNotNull(parameter.getLocaFilelPath(), StrUtil.EMPTY);
  322. FileInfo fileInfo = getFileInfo(parameter);
  323. File localFile = new File(localPath, parameter.getLocalFileName());
  324. if(!fileStorageProperties.getDefaultPlatform().startsWith(GenerateConstant.LOCAL)
  325. || !Objects.equals(parameter.getRemotePath(), parameter.getLocaFilelPath())
  326. || !Objects.equals(parameter.getRemoteFileName(), parameter.getLocalFileName())) {
  327. fileStorageService.download(fileInfo).file(localFile);
  328. }
  329. return localFile;
  330. }
  331. /**
  332. * 下载文件得到字节数组
  333. */
  334. public static byte[] downloadFile(FileParameter parameter) {
  335. process(parameter);
  336. FileInfo fileInfo = getFileInfo(parameter);
  337. return fileStorageService.download(fileInfo).bytes();
  338. }
  339. /**
  340. * 下载文件到文件
  341. */
  342. public static void downloadFile(FileParameter parameter, File targetFile) {
  343. process(parameter);
  344. FileInfo fileInfo = getFileInfo(parameter);
  345. fileStorageService.download(fileInfo).file(targetFile);
  346. }
  347. /**
  348. * 下载文件到输出流
  349. */
  350. public static void downloadFile(FileParameter parameter, OutputStream outputStream) {
  351. process(parameter);
  352. FileInfo fileInfo = getFileInfo(parameter);
  353. fileStorageService.download(fileInfo).outputStream(outputStream);
  354. }
  355. /**
  356. * 下载文件到输入流, 自定义处理
  357. */
  358. public static void downloadFile(FileParameter parameter, Consumer<InputStream> fileConsumer) {
  359. process(parameter);
  360. FileInfo fileInfo = getFileInfo(parameter);
  361. fileStorageService.download(fileInfo).inputStream(fileConsumer);
  362. }
  363. // /**
  364. // * 下载文件得到流
  365. // *
  366. // * @param path
  367. // * @param fileName
  368. // * @param origin
  369. // */
  370. // public static ByteArrayOutputStream downloadFilStream(String path, String fileName, boolean origin) {
  371. // FileInfo fileInfo = getFileInfo(path, fileName, origin);
  372. // ByteArrayOutputStream out = new ByteArrayOutputStream();
  373. // fileStorageService.download(fileInfo).outputStream(out);
  374. // return out;
  375. // }
  376. /**
  377. * 默认存储平台
  378. *
  379. * @param
  380. * @return
  381. */
  382. public static String getDefaultPlatform() {
  383. return fileStorageProperties.getDefaultPlatform();
  384. }
  385. /**
  386. * 判断文件是否存在
  387. *
  388. * @return
  389. */
  390. public static boolean exists(FileParameter parameter) {
  391. process(parameter);
  392. FileInfo fileInfo = getFileInfo(parameter);
  393. return fileStorageService.exists(fileInfo);
  394. }
  395. public static void process(FileParameter parameter) {
  396. String typePath = FilePathUtil.getFilePath(parameter.getRemotePath());
  397. String fileName = parameter.getRemoteFileName();
  398. if (StringUtil.isNotEmpty(fileName)) {
  399. if(fileName.contains(",")) {
  400. typePath += fileName.substring(0, fileName.lastIndexOf(",") + 1).replaceAll(",", "/");
  401. fileName = fileName.substring(fileName.lastIndexOf(",") + 1);
  402. }
  403. if(fileName.startsWith("/")){
  404. fileName = fileName.substring(1);
  405. }
  406. }
  407. typePath = XSSEscape.escapePath(typePath);
  408. fileName = XSSEscape.escapePath(fileName);
  409. if(!typePath.endsWith("/")){
  410. typePath += "/";
  411. }
  412. parameter.setRemotePath(typePath);
  413. parameter.setRemoteFileName(fileName);
  414. }
  415. /**
  416. * 根据路径和文件名删除文件
  417. *
  418. * @return
  419. */
  420. public static boolean deleteFileByPathAndFileName(FileParameter parameter) {
  421. FileInfo fileInfo = getFileInfo(parameter);
  422. return fileStorageService.delete(fileInfo);
  423. }
  424. }