FileConvertServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.io.FileTypeUtil;
  3. import cn.hutool.core.io.FileUtil;
  4. import cn.hutool.core.io.IoUtil;
  5. import cn.hutool.core.lang.UUID;
  6. import com.fdkankan.model.constants.ConstantFileName;
  7. import com.fdkankan.model.constants.ConstantFilePath;
  8. import com.fdkankan.common.constant.ErrorCode;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.model.utils.CreateObjUtil;
  11. import com.fdkankan.scene.service.IFileConvertService;
  12. import java.io.BufferedInputStream;
  13. import java.io.File;
  14. import javax.servlet.ServletOutputStream;
  15. import javax.servlet.http.HttpServletResponse;
  16. import lombok.extern.slf4j.Slf4j;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.web.multipart.MultipartFile;
  19. /**
  20. * <p>
  21. * TODO
  22. * </p>
  23. *
  24. * @author dengsixing
  25. * @since 2022/5/17
  26. **/
  27. @Slf4j
  28. @Service
  29. public class FileConvertServiceImpl implements IFileConvertService {
  30. @Override
  31. public void convertTxtToModeldata(MultipartFile file, HttpServletResponse response) throws Exception {
  32. //生成uuid
  33. String fileName = "vision.modeldata";
  34. String uuid = UUID.randomUUID().toString();
  35. String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
  36. // String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
  37. String srcPath = path + File.separator + file.getOriginalFilename();
  38. String targetPath = path + File.separator + fileName;
  39. File srcFile = new File(srcPath);
  40. if(!srcFile.getParentFile().exists()){
  41. srcFile.getParentFile().mkdirs();
  42. }
  43. file.transferTo(new File(srcPath));
  44. String type = FileTypeUtil.getType(srcFile);
  45. if(!"txt".equals(type)){
  46. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
  47. }
  48. CreateObjUtil.convertTxtToVisionmodeldata(srcPath, targetPath);
  49. if(!FileUtil.exist(targetPath)){
  50. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  51. }
  52. // 设置返回内容格式
  53. response.setContentType("application/octet-stream");
  54. // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
  55. // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
  56. // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
  57. // fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
  58. // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
  59. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  60. try(
  61. BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
  62. ServletOutputStream outputStream = response.getOutputStream();
  63. ){
  64. IoUtil.copy(inputStream, outputStream);
  65. //关闭流
  66. IoUtil.close(outputStream);
  67. IoUtil.close(inputStream);
  68. }catch (Exception e){
  69. log.error("文件流输出失败,文件路径: " + targetPath, e);
  70. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  71. }
  72. }
  73. @Override
  74. public void convertTxtToDam(MultipartFile file, HttpServletResponse response) throws Exception {
  75. //生成uuid
  76. String fileName = ConstantFileName.modelUUID+"_50k.dam";
  77. String uuid = UUID.randomUUID().toString();
  78. String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
  79. // String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
  80. String srcPath = path + File.separator + file.getOriginalFilename();
  81. String targetPath = path + File.separator + fileName;
  82. File srcFile = new File(srcPath);
  83. if(!srcFile.getParentFile().exists()){
  84. srcFile.getParentFile().mkdirs();
  85. }
  86. file.transferTo(new File(srcPath));
  87. String type = FileTypeUtil.getType(srcFile);
  88. if(!"txt".equals(type)){
  89. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
  90. }
  91. CreateObjUtil.convertTxtToDam( srcPath, targetPath);
  92. if(!FileUtil.exist(targetPath)){
  93. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  94. }
  95. // 设置返回内容格式
  96. response.setContentType("application/octet-stream");
  97. // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
  98. // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
  99. // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
  100. // fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
  101. // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
  102. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  103. try(
  104. BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
  105. ServletOutputStream outputStream = response.getOutputStream();
  106. ){
  107. IoUtil.copy(inputStream, outputStream);
  108. //关闭流
  109. IoUtil.close(outputStream);
  110. IoUtil.close(inputStream);
  111. }catch (Exception e){
  112. log.error("文件流输出失败,文件路径: " + targetPath, e);
  113. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  114. }
  115. }
  116. @Override
  117. public void convertDamToTxt(MultipartFile file, HttpServletResponse response) throws Exception {
  118. //生成uuid
  119. String fileName = "modeldata.txt";
  120. String uuid = UUID.randomUUID().toString();
  121. String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
  122. // String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
  123. String srcPath = path + File.separator + file.getOriginalFilename();
  124. String targetPath = path + File.separator + fileName;
  125. File srcFile = new File(srcPath);
  126. if(!srcFile.getParentFile().exists()){
  127. srcFile.getParentFile().mkdirs();
  128. }
  129. file.transferTo(new File(srcPath));
  130. String type = FileTypeUtil.getType(srcFile);
  131. if(!"dam".equals(type)){
  132. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "dam");
  133. }
  134. CreateObjUtil.convertDamToTxt(srcPath, targetPath);
  135. if(!FileUtil.exist(targetPath)){
  136. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  137. }
  138. // 设置返回内容格式
  139. response.setContentType("application/octet-stream");
  140. // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
  141. // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
  142. // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
  143. // fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
  144. // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
  145. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
  146. try(
  147. BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
  148. ServletOutputStream outputStream = response.getOutputStream();
  149. ){
  150. IoUtil.copy(inputStream, outputStream);
  151. //关闭流
  152. IoUtil.close(outputStream);
  153. IoUtil.close(inputStream);
  154. }catch (Exception e){
  155. log.error("文件流输出失败,文件路径: " + targetPath, e);
  156. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  157. }
  158. }
  159. @Override
  160. public void convertTxtToLzma(MultipartFile file, HttpServletResponse response) throws Exception {
  161. //生成uuid
  162. String damFileName = ConstantFileName.modelUUID+"_50k.dam";
  163. String lzmaFileName = damFileName + ".lzma";
  164. String uuid = UUID.randomUUID().toString();
  165. String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
  166. // String path = "F:\\mnt\\4Dkankan\\fileConvert\\" + uuid;
  167. String srcPath = path + File.separator + file.getOriginalFilename();
  168. String damFilePath = path + File.separator + damFileName;
  169. String lzmaFilePath = path + File.separator + lzmaFileName;
  170. File srcFile = new File(srcPath);
  171. if(!srcFile.getParentFile().exists()){
  172. srcFile.getParentFile().mkdirs();
  173. }
  174. file.transferTo(new File(srcPath));
  175. String type = FileTypeUtil.getType(srcFile);
  176. if(!"txt".equals(type)){
  177. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
  178. }
  179. CreateObjUtil.convertTxtToDam( srcPath, damFilePath);
  180. if(!FileUtil.exist(damFilePath)){
  181. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  182. }
  183. CreateObjUtil.convertDamToLzmaByAbsolutePath(damFilePath);
  184. if(!FileUtil.exist(lzmaFilePath)){
  185. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  186. }
  187. // 设置返回内容格式
  188. response.setContentType("application/octet-stream");
  189. // 把文件名按UTF-8取出并按ISO8859-1编码,保证弹出窗口中的文件名中文不乱码
  190. // 中文不要太多,最多支持17个中文,因为header有150个字节限制。
  191. // 这一步一定要在读取文件之后进行,否则文件名会乱码,找不到文件
  192. // fileName = new String(fileName.getBytes("UTF-8"),"ISO8859-1");
  193. // 设置下载弹窗的文件名和格式(文件名要包括名字和文件格式)
  194. response.setHeader("Content-Disposition", "attachment;filename=" + lzmaFileName);
  195. try(
  196. BufferedInputStream inputStream = FileUtil.getInputStream(lzmaFilePath);
  197. ServletOutputStream outputStream = response.getOutputStream();
  198. ){
  199. IoUtil.copy(inputStream, outputStream);
  200. //关闭流
  201. IoUtil.close(outputStream);
  202. IoUtil.close(inputStream);
  203. }catch (Exception e){
  204. log.error("文件流输出失败,文件路径: " + lzmaFilePath, e);
  205. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  206. }
  207. }
  208. }