XIFileUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. package com.miniframe.tools;
  2. import com.miniframe.constant.MFConstant;
  3. import com.miniframe.core.exception.BusinessException;
  4. import com.miniframe.core.ext.UtilTools;
  5. import com.miniframe.generate.appcode.FileType;
  6. import com.miniframe.model.system.SysFile;
  7. import com.miniframe.model.system.dao.SysFileMapper;
  8. import com.miniframe.spring.properties.MFMiniCoreProperties;
  9. import org.apache.commons.io.FileUtils;
  10. import org.apache.commons.io.FilenameUtils;
  11. import org.apache.commons.io.IOUtils;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import java.io.*;
  15. import java.math.BigDecimal;
  16. import java.nio.file.Files;
  17. import java.nio.file.Path;
  18. import java.nio.file.Paths;
  19. import java.nio.file.StandardCopyOption;
  20. import java.sql.SQLException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.stream.Collectors;
  24. public class XIFileUtils {
  25. private static Logger logger = LoggerFactory.getLogger(XIFileUtils.class);
  26. private final static String fileRootPathStr = "/cephfs";
  27. static {
  28. // IniFileReader reader = new IniFileReader(IniFileReader.defaultConfig);
  29. // String pathStr = reader.getStrValue("fileuploadpath");
  30. // if(UtilTools.isNullOrBlank(pathStr)){
  31. // pathStr = XIConfig.fileRootPath;
  32. // }
  33. // fileRootPathStr =Paths.get(pathStr).toString().replace("\\", XIConfig.separator)+XIConfig.separator;
  34. }
  35. public static void fileCopy(String sourcePath ,String destinationPath){
  36. File sourceFile = new File(sourcePath);
  37. File destinationFile = new File(destinationPath);
  38. try {
  39. FileUtils.copyFile(sourceFile, destinationFile); // 拷贝文件到目标位置,如果目标文件已存在,会被覆盖。
  40. System.out.println("文件拷贝成功!");
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. public static void main(String[] args) throws BusinessException {
  46. //TODO 解析 边界文件 生成瓦斯积聚区数据
  47. File bfFile =new File("D://pipes3.inp");
  48. List<String> fileStrs =new ArrayList<>();
  49. try{
  50. FileReader fileReader = new FileReader(bfFile);
  51. BufferedReader reader = new BufferedReader(fileReader);
  52. String line;
  53. int linNum =1;
  54. while ((line = reader.readLine()) != null) {
  55. fileStrs.add(line);
  56. }
  57. fileReader.close();
  58. reader.close();
  59. }catch (Exception e){
  60. System.out.println(e);
  61. throw new BusinessException("EB3100021");
  62. }
  63. if(fileStrs.size()<=3){
  64. throw new BusinessException("EB3100025");
  65. }
  66. boolean isBlockStart =false;
  67. boolean isBlockNumStart =false;
  68. int blockNum =0;
  69. List<String> blockNames =new ArrayList<>();
  70. for (int i = 3; i < fileStrs.size(); i++) {
  71. if(!isBlockStart){
  72. isBlockStart=true;
  73. blockNames.add(fileStrs.get(i));
  74. continue;
  75. }
  76. if(!isBlockNumStart){
  77. blockNum = Integer.valueOf(fileStrs.get(i));
  78. isBlockNumStart=true;
  79. continue;
  80. }
  81. int j = 0;
  82. int dyhang =0;
  83. for (; j < blockNum; j++) {
  84. String line =fileStrs.get(i+dyhang+j);
  85. String[] t =line.trim().split(" ");
  86. int last =Integer.valueOf(t[t.length-1]);
  87. if(last==-1){
  88. ++dyhang;
  89. }
  90. }
  91. i=i+dyhang+blockNum;
  92. isBlockStart=false;
  93. isBlockNumStart=false;
  94. }
  95. System.out.println(blockNames);
  96. }
  97. public static String replacekg(String line){
  98. while (line.lastIndexOf(" ")>0){
  99. String s =line.replace(" "," ");
  100. return replacekg(s);
  101. }
  102. return line;
  103. }
  104. public static String getRelativizePathStr(Path path) {
  105. String relativizePath = null;
  106. try {
  107. Path rootPath = Paths.get(getRootPathStr());
  108. if (XIPlatformUtils.isWindows()) {
  109. rootPath = Paths.get(getRootPathStr()).toRealPath();
  110. path = path.toRealPath();
  111. }
  112. relativizePath = rootPath.relativize(path).toString().replace("\\", MFConstant.separator);
  113. } catch (IOException e) {
  114. e.printStackTrace();
  115. }
  116. return relativizePath;
  117. }
  118. public static String getRelativizePathStr(String pathStr) {
  119. return getRelativizePathStr(Paths.get(pathStr));
  120. }
  121. public static Path getAbsolutePath(String relativizePathStr) {
  122. Path path = Paths.get(getRootPathStr()).resolve(Paths.get(relativizePathStr));
  123. return path;
  124. }
  125. public static String getAbsolutePathStr(String relativizePathStr) {
  126. String absolutePath = getAbsolutePath(relativizePathStr).toString().replace("\\", MFConstant.separator);
  127. return absolutePath;
  128. }
  129. public static String getFileName(String pathStr) {
  130. Path path = Paths.get(pathStr);
  131. return path.getFileName().toString();
  132. }
  133. public static Long getFileSize(String pathStr) throws IOException {
  134. Path path = Paths.get(pathStr);
  135. if(Files.exists(path)) {
  136. return Files.size(path);
  137. }
  138. return null;
  139. }
  140. public static String getRootPathStr() {
  141. return fileRootPathStr;
  142. }
  143. /**
  144. * 根据hash文件转存
  145. *
  146. * @param fileFromPathStr 原文件
  147. * @param group 分组
  148. * @return 转储后的路径
  149. */
  150. public static String restorageFile(String fileFromPathStr, String group) throws IOException {
  151. String parentPath = getRootPathStr();
  152. Path pathFrom = Paths.get(fileFromPathStr);
  153. if (Files.exists(pathFrom)) {
  154. String fileName = pathFrom.getFileName().toString();
  155. String md5 = XIUtils.getMD5(fileName);
  156. String uploadDirStr = "";
  157. for (int i = 0; i < MFMiniCoreProperties.getInstance().getFileuploadlevel(); i++) {
  158. uploadDirStr += md5.substring(i * 2, i * 2 + 2) + "/";
  159. }
  160. Path pathTo = Paths.get(parentPath, group, uploadDirStr, fileName);
  161. mkdir(pathTo.getParent());
  162. //覆盖+属性
  163. Files.move(pathFrom, pathTo, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
  164. return getRelativizePathStr(pathTo);
  165. }
  166. return null;
  167. }
  168. public static String saveUploadFiles(String files, String userid, String fileMedia) throws Exception {
  169. return saveUploadFiles(files, userid, fileMedia, null, null, null, 1, null,1,1,null);
  170. }
  171. public static String saveUploadFiles(String files, String userid, String fileMedia,String bFid) throws Exception {
  172. return saveUploadFiles(files, userid, fileMedia, null, null, null, 1, null,1,1,bFid);
  173. }
  174. public static String saveUploadFiles(String files, String userid, String fileMedia,
  175. String fileType, String fileStartTime,
  176. String fileInfo, int fileNameType, String needOp,
  177. Integer chunk, Integer chunks, String bFid
  178. ) throws Exception {
  179. String fileIds = "";
  180. if (UtilTools.isNotNullAndBlank(files)) {
  181. List<String> fileList = new ArrayList<>();
  182. if (files.contains(";")) {
  183. String[] fileArray = files.split(";");
  184. if (fileArray != null) {
  185. for (int i = 0; i < fileArray.length; i++) {
  186. fileList.add(fileArray[i]);
  187. }
  188. }
  189. } else {
  190. fileList.add(files);
  191. }
  192. for (String pathStr : fileList) {
  193. if (UtilTools.isNotNullAndBlank(pathStr)) {
  194. Path path = Paths.get(pathStr);
  195. String fielRelativePath = XIFileUtils.getRelativizePathStr(path);
  196. SysFile sysFile = new SysFile();
  197. sysFile.setId(UtilTools.getUUid());
  198. if (fileNameType == 0) {
  199. sysFile.setFilename(path.getFileName().toString());
  200. } else {
  201. //去掉uid部分
  202. int pathIndex = path.getFileName().toString().indexOf("-");
  203. if (pathIndex >= 0 && pathIndex < path.getFileName().toString().length() - 1) {
  204. sysFile.setFilename(path.getFileName().toString().substring(pathIndex + 1));
  205. } else {
  206. sysFile.setFilename(path.getFileName().toString());
  207. }
  208. }
  209. if (UtilTools.isNullOrBlank(fileStartTime)) {
  210. sysFile.setLasttime(XIDateTimeUtils.getNowDate());
  211. } else {
  212. sysFile.setLasttime(XIDateTimeUtils.getDateFromZonedStr(fileStartTime));
  213. }
  214. sysFile.setUid(userid);
  215. sysFile.setFilesize(BigDecimal.valueOf(Files.size(path)));
  216. sysFile.setHashcode(XIUtils.getSHA(path));
  217. sysFile.setFilemedia(fileMedia);
  218. sysFile.setRemarks(fileInfo);
  219. if (UtilTools.isNotNullAndBlank(fileType)) {
  220. sysFile.setFiletype(fileType);
  221. } else {
  222. sysFile.setFiletype(getFileType(path.getFileName().toString()));
  223. }
  224. if (UtilTools.isNotNullAndBlank(needOp)) {
  225. sysFile.setNeedop(needOp);
  226. }
  227. sysFile.setFilepath(fielRelativePath);
  228. sysFile.setChunk(chunk);
  229. sysFile.setChunks(chunks);
  230. sysFile.setParentid(bFid);
  231. SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
  232. sysFileDAO.insertSelective(sysFile);
  233. if (fileIds.isEmpty()) {
  234. fileIds += sysFile.getId();
  235. } else {
  236. fileIds += ";" + sysFile.getId();
  237. }
  238. }
  239. }
  240. }
  241. return fileIds;
  242. }
  243. public static void initHeadFile(){
  244. try {
  245. SysFileMapper sysFileMapper = UtilTools.getBean(SysFileMapper.class);
  246. SysFile sysFile = sysFileMapper.selectByPrimaryKey("head");
  247. if (sysFile == null) {
  248. sysFile = new SysFile();
  249. sysFile.setId("head");
  250. sysFile.setFilename("head.png");
  251. sysFile.setFilepath("head.png");
  252. sysFile.setLasttime(XIDateTimeUtils.getNowDate());
  253. sysFileMapper.insertSelective(sysFile);
  254. }
  255. }catch (Exception e){}
  256. }
  257. public static void delete(String filePathStr) {
  258. try {
  259. Files.deleteIfExists(Paths.get(filePathStr));
  260. } catch (IOException e) {
  261. e.printStackTrace();
  262. }
  263. }
  264. public static String getExtension(String filePathStr) {
  265. Path path = Paths.get(filePathStr);
  266. String suffix = FilenameUtils.getExtension(path.getFileName().toString());
  267. if (UtilTools.isNullOrBlank(suffix)) {
  268. suffix = "";
  269. }
  270. return suffix;
  271. }
  272. public static String getExtensionWithDot(String filePathStr) {
  273. Path path = Paths.get(filePathStr);
  274. String suffix = FilenameUtils.getExtension(path.getFileName().toString());
  275. if (UtilTools.isNotNullAndBlank(suffix)) {
  276. suffix = "." + suffix;
  277. } else {
  278. suffix = "";
  279. }
  280. return suffix;
  281. }
  282. public static String getFilePathStr(String first, String... more) {
  283. Path path = Paths.get(first, more);
  284. return path.toString().replace("\\", MFConstant.separator);
  285. }
  286. public static String getFileType(String fileName) {
  287. String fileType = FileType.other.getIndex();
  288. String suffix = getExtension(fileName);
  289. if (UtilTools.isNotNullAndBlank(suffix)) {
  290. if (suffix.toLowerCase().matches("^(mp3|wav|aac)$")) {
  291. fileType = FileType.audio.getIndex();
  292. } else if (suffix.toLowerCase().matches("^(mp4|mpeg4)$")) {
  293. fileType = FileType.video.getIndex();
  294. } else if (suffix.toLowerCase().matches("^(jpg|png|jpeg|bmp)$")) {
  295. fileType = FileType.image.getIndex();
  296. }
  297. }
  298. return fileType;
  299. }
  300. public static boolean mkdir(Path dirPath) {
  301. try {
  302. if (!Files.exists(dirPath)) {
  303. Files.createDirectories(dirPath);
  304. }
  305. return true;
  306. } catch (IOException e) {
  307. logger.error("mkdir " + dirPath + " " + e);
  308. }
  309. return false;
  310. }
  311. public static boolean mkdir(String dir) {
  312. return mkdir(Paths.get(dir));
  313. }
  314. public static String getFileUrl(String fileId) throws SQLException {
  315. SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
  316. SysFile sysFile = sysFileDAO.selectByPrimaryKey(fileId);
  317. if (sysFile != null) {
  318. return sysFile.getFilepath();
  319. }
  320. return "";
  321. }
  322. public static boolean isFileExist(String filePathStr) {
  323. return Files.exists(Paths.get(filePathStr));
  324. }
  325. /**
  326. * 读取文件内容,作为字符串返回
  327. */
  328. public static String readFileAsString(String filePathStr) throws IOException {
  329. Path path = Paths.get(filePathStr);
  330. if (Files.notExists(path)) {
  331. throw new FileNotFoundException(filePathStr);
  332. } else {
  333. return new String(Files.readAllBytes(path));
  334. }
  335. }
  336. /**
  337. * 根据文件路径读取byte[] 数组
  338. */
  339. public static byte[] readFileByBytes(String filePathStr) throws IOException {
  340. Path path = Paths.get(filePathStr);
  341. if (Files.notExists(path)) {
  342. throw new FileNotFoundException(filePathStr);
  343. } else {
  344. return Files.readAllBytes(path);
  345. }
  346. }
  347. public static String getFileNameFromUrl(String url) {
  348. return url.substring(url.lastIndexOf("/") + 1);
  349. }
  350. //
  351. // public static byte[] getResourceFileBytes(String resourcePath) throws Exception {
  352. //
  353. // byte[] fileBytes = null;
  354. // try {
  355. // fileBytes = IOUtils.toByteArray((BufferedInputStream) Resources.getResource(resourcePath).getContent());
  356. // } catch (IOException e) {
  357. // throw new Exception("get file by path err : " + e.getMessage());
  358. // }
  359. // return fileBytes;
  360. //
  361. // }
  362. public static byte[] getFileBytes(String filePath) throws Exception {
  363. byte[] fileBytes = null;
  364. try {
  365. fileBytes = Files.readAllBytes(Paths.get(filePath));
  366. } catch (IOException e) {
  367. throw new Exception("get file by path err : " + e.getMessage());
  368. }
  369. return fileBytes;
  370. }
  371. public static List<String> getCertFilesByPath(String certPath) {
  372. List<String> fileList = new ArrayList<>();
  373. Path filePath=Paths.get(certPath);
  374. if(Files.exists(filePath) && Files.isDirectory(filePath)){
  375. {
  376. try {
  377. fileList = Files.walk(filePath)
  378. .filter(path -> path.toFile().isFile() && path.toFile().getName().endsWith(".crt"))
  379. .map(Path::toString)
  380. //.map(path-> path.replace("\\","/"))
  381. //.distinct()
  382. .collect(Collectors.toList());
  383. } catch (IOException e) {
  384. e.printStackTrace();
  385. }
  386. }
  387. }
  388. return fileList;
  389. }
  390. public static String getResourceFilePath(String resourcePath) {
  391. return System.getProperty("user.dir") + "/src/main/resources/" + resourcePath;
  392. }
  393. public static String saveUploadFiles(String filePath, String userid, Integer chunk, Integer chunks, String bFid) throws Exception {
  394. return saveUploadFiles(filePath, userid, null, null, null, null, 1, null,chunk,chunks,bFid);
  395. }
  396. }