FileConvertController.java 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.fdkankan.scene.controller;
  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.common.constant.ConstantFilePath;
  7. import com.fdkankan.common.constant.ErrorCode;
  8. import com.fdkankan.common.controller.BaseController;
  9. import com.fdkankan.common.exception.BusinessException;
  10. import com.fdkankan.common.util.CreateObjUtil;
  11. import java.io.BufferedInputStream;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import java.io.PrintWriter;
  15. import java.nio.file.Paths;
  16. import javax.servlet.ServletOutputStream;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.RestController;
  21. import org.springframework.web.multipart.MultipartFile;
  22. /**
  23. * <p>
  24. * TODO
  25. * </p>
  26. *
  27. * @author dengsixing
  28. * @since 2022/5/17
  29. **/
  30. @RestController
  31. @RequestMapping("/service/file/convert")
  32. public class FileConvertController extends BaseController {
  33. @RequestMapping(value = "/getModeldata", method = RequestMethod.POST)
  34. public void convertTxtToModeldata(@RequestParam("file") MultipartFile file) throws Exception{
  35. //生成uuid
  36. String uuid = UUID.randomUUID().toString();
  37. String path = ConstantFilePath.FILE_CONVERT_PATH + uuid;
  38. String srcPath = path + "/" + file.getOriginalFilename();
  39. String targetPath = path + "vision.modeldata";
  40. File srcFile = new File(srcPath);
  41. if(!srcFile.getParentFile().exists()){
  42. srcFile.getParentFile().mkdirs();
  43. }
  44. file.transferTo(new File(srcPath));
  45. String type = FileTypeUtil.getType(srcFile);
  46. if(!"txt".equals(type)){
  47. throw new BusinessException(ErrorCode.FAILURE_CODE_7007, "txt");
  48. }
  49. CreateObjUtil.convertTxtToVisionmodeldata(srcPath, targetPath);
  50. if(!FileUtil.exist(targetPath)){
  51. throw new BusinessException(ErrorCode.FAILURE_CODE_8005);
  52. }
  53. BufferedInputStream inputStream = FileUtil.getInputStream(targetPath);
  54. ServletOutputStream outputStream = response.getOutputStream();
  55. IoUtil.copy(inputStream, outputStream);
  56. // IoUtil.
  57. }
  58. }