XIFileUtils.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 fileType, String fileStartTime,
  172. String fileInfo, int fileNameType, String needOp,
  173. Integer chunk, Integer chunks, String bFid
  174. ) throws Exception {
  175. String fileIds = "";
  176. if (UtilTools.isNotNullAndBlank(files)) {
  177. List<String> fileList = new ArrayList<>();
  178. if (files.contains(";")) {
  179. String[] fileArray = files.split(";");
  180. if (fileArray != null) {
  181. for (int i = 0; i < fileArray.length; i++) {
  182. fileList.add(fileArray[i]);
  183. }
  184. }
  185. } else {
  186. fileList.add(files);
  187. }
  188. for (String pathStr : fileList) {
  189. if (UtilTools.isNotNullAndBlank(pathStr)) {
  190. Path path = Paths.get(pathStr);
  191. String fielRelativePath = XIFileUtils.getRelativizePathStr(path);
  192. SysFile sysFile = new SysFile();
  193. sysFile.setId(UtilTools.getUUid());
  194. if (fileNameType == 0) {
  195. sysFile.setFilename(path.getFileName().toString());
  196. } else {
  197. //去掉uid部分
  198. int pathIndex = path.getFileName().toString().indexOf("-");
  199. if (pathIndex >= 0 && pathIndex < path.getFileName().toString().length() - 1) {
  200. sysFile.setFilename(path.getFileName().toString().substring(pathIndex + 1));
  201. } else {
  202. sysFile.setFilename(path.getFileName().toString());
  203. }
  204. }
  205. if (UtilTools.isNullOrBlank(fileStartTime)) {
  206. sysFile.setLasttime(XIDateTimeUtils.getNowDate());
  207. } else {
  208. sysFile.setLasttime(XIDateTimeUtils.getDateFromZonedStr(fileStartTime));
  209. }
  210. sysFile.setUid(userid);
  211. sysFile.setFilesize(BigDecimal.valueOf(Files.size(path)));
  212. sysFile.setHashcode(XIUtils.getSHA(path));
  213. sysFile.setFilemedia(fileMedia);
  214. sysFile.setRemarks(fileInfo);
  215. if (UtilTools.isNotNullAndBlank(fileType)) {
  216. sysFile.setFiletype(fileType);
  217. } else {
  218. sysFile.setFiletype(getFileType(path.getFileName().toString()));
  219. }
  220. if (UtilTools.isNotNullAndBlank(needOp)) {
  221. sysFile.setNeedop(needOp);
  222. }
  223. sysFile.setFilepath(fielRelativePath);
  224. sysFile.setChunk(chunk);
  225. sysFile.setChunks(chunks);
  226. sysFile.setParentid(bFid);
  227. SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
  228. sysFileDAO.insertSelective(sysFile);
  229. if (fileIds.isEmpty()) {
  230. fileIds += sysFile.getId();
  231. } else {
  232. fileIds += ";" + sysFile.getId();
  233. }
  234. }
  235. }
  236. }
  237. return fileIds;
  238. }
  239. public static void initHeadFile(){
  240. try {
  241. SysFileMapper sysFileMapper = UtilTools.getBean(SysFileMapper.class);
  242. SysFile sysFile = sysFileMapper.selectByPrimaryKey("head");
  243. if (sysFile == null) {
  244. sysFile = new SysFile();
  245. sysFile.setId("head");
  246. sysFile.setFilename("head.png");
  247. sysFile.setFilepath("head.png");
  248. sysFile.setLasttime(XIDateTimeUtils.getNowDate());
  249. sysFileMapper.insertSelective(sysFile);
  250. }
  251. }catch (Exception e){}
  252. }
  253. public static void delete(String filePathStr) {
  254. try {
  255. Files.deleteIfExists(Paths.get(filePathStr));
  256. } catch (IOException e) {
  257. e.printStackTrace();
  258. }
  259. }
  260. public static String getExtension(String filePathStr) {
  261. Path path = Paths.get(filePathStr);
  262. String suffix = FilenameUtils.getExtension(path.getFileName().toString());
  263. if (UtilTools.isNullOrBlank(suffix)) {
  264. suffix = "";
  265. }
  266. return suffix;
  267. }
  268. public static String getExtensionWithDot(String filePathStr) {
  269. Path path = Paths.get(filePathStr);
  270. String suffix = FilenameUtils.getExtension(path.getFileName().toString());
  271. if (UtilTools.isNotNullAndBlank(suffix)) {
  272. suffix = "." + suffix;
  273. } else {
  274. suffix = "";
  275. }
  276. return suffix;
  277. }
  278. public static String getFilePathStr(String first, String... more) {
  279. Path path = Paths.get(first, more);
  280. return path.toString().replace("\\", MFConstant.separator);
  281. }
  282. public static String getFileType(String fileName) {
  283. String fileType = FileType.other.getIndex();
  284. String suffix = getExtension(fileName);
  285. if (UtilTools.isNotNullAndBlank(suffix)) {
  286. if (suffix.toLowerCase().matches("^(mp3|wav|aac)$")) {
  287. fileType = FileType.audio.getIndex();
  288. } else if (suffix.toLowerCase().matches("^(mp4|mpeg4)$")) {
  289. fileType = FileType.video.getIndex();
  290. } else if (suffix.toLowerCase().matches("^(jpg|png|jpeg|bmp)$")) {
  291. fileType = FileType.image.getIndex();
  292. }
  293. }
  294. return fileType;
  295. }
  296. public static boolean mkdir(Path dirPath) {
  297. try {
  298. if (!Files.exists(dirPath)) {
  299. Files.createDirectories(dirPath);
  300. }
  301. return true;
  302. } catch (IOException e) {
  303. logger.error("mkdir " + dirPath + " " + e);
  304. }
  305. return false;
  306. }
  307. public static boolean mkdir(String dir) {
  308. return mkdir(Paths.get(dir));
  309. }
  310. public static String getFileUrl(String fileId) throws SQLException {
  311. SysFileMapper sysFileDAO = UtilTools.getBean(SysFileMapper.class);
  312. SysFile sysFile = sysFileDAO.selectByPrimaryKey(fileId);
  313. if (sysFile != null) {
  314. return sysFile.getFilepath();
  315. }
  316. return "";
  317. }
  318. public static boolean isFileExist(String filePathStr) {
  319. return Files.exists(Paths.get(filePathStr));
  320. }
  321. /**
  322. * 读取文件内容,作为字符串返回
  323. */
  324. public static String readFileAsString(String filePathStr) throws IOException {
  325. Path path = Paths.get(filePathStr);
  326. if (Files.notExists(path)) {
  327. throw new FileNotFoundException(filePathStr);
  328. } else {
  329. return new String(Files.readAllBytes(path));
  330. }
  331. }
  332. /**
  333. * 根据文件路径读取byte[] 数组
  334. */
  335. public static byte[] readFileByBytes(String filePathStr) throws IOException {
  336. Path path = Paths.get(filePathStr);
  337. if (Files.notExists(path)) {
  338. throw new FileNotFoundException(filePathStr);
  339. } else {
  340. return Files.readAllBytes(path);
  341. }
  342. }
  343. public static String getFileNameFromUrl(String url) {
  344. return url.substring(url.lastIndexOf("/") + 1);
  345. }
  346. //
  347. // public static byte[] getResourceFileBytes(String resourcePath) throws Exception {
  348. //
  349. // byte[] fileBytes = null;
  350. // try {
  351. // fileBytes = IOUtils.toByteArray((BufferedInputStream) Resources.getResource(resourcePath).getContent());
  352. // } catch (IOException e) {
  353. // throw new Exception("get file by path err : " + e.getMessage());
  354. // }
  355. // return fileBytes;
  356. //
  357. // }
  358. public static byte[] getFileBytes(String filePath) throws Exception {
  359. byte[] fileBytes = null;
  360. try {
  361. fileBytes = Files.readAllBytes(Paths.get(filePath));
  362. } catch (IOException e) {
  363. throw new Exception("get file by path err : " + e.getMessage());
  364. }
  365. return fileBytes;
  366. }
  367. public static List<String> getCertFilesByPath(String certPath) {
  368. List<String> fileList = new ArrayList<>();
  369. Path filePath=Paths.get(certPath);
  370. if(Files.exists(filePath) && Files.isDirectory(filePath)){
  371. {
  372. try {
  373. fileList = Files.walk(filePath)
  374. .filter(path -> path.toFile().isFile() && path.toFile().getName().endsWith(".crt"))
  375. .map(Path::toString)
  376. //.map(path-> path.replace("\\","/"))
  377. //.distinct()
  378. .collect(Collectors.toList());
  379. } catch (IOException e) {
  380. e.printStackTrace();
  381. }
  382. }
  383. }
  384. return fileList;
  385. }
  386. public static String getResourceFilePath(String resourcePath) {
  387. return System.getProperty("user.dir") + "/src/main/resources/" + resourcePath;
  388. }
  389. public static String saveUploadFiles(String filePath, String userid, Integer chunk, Integer chunks, String bFid) throws Exception {
  390. return saveUploadFiles(filePath, userid, null, null, null, null, 1, null,chunk,chunks,bFid);
  391. }
  392. }