BaseService.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.lqkj.link.module.base.service;
  2. import com.lqkj.link.message.MessageBean;
  3. import com.lqkj.link.util.FileUtils;
  4. import org.springframework.stereotype.Service;
  5. import org.springframework.web.multipart.MultipartFile;
  6. import java.util.UUID;
  7. @Service
  8. public class BaseService {
  9. public MessageBean<String> uploadImg(MultipartFile file, String path) {
  10. String fileName = file.getOriginalFilename();
  11. String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
  12. if (!suffix.equals("png") && !suffix.equals("jpg"))
  13. return MessageBean.error("上传文件类型必须是png、jpg图片文件");
  14. String newFileName = UUID.randomUUID() + "." + suffix;
  15. String filePath = "./upload/" + path;
  16. FileUtils.saveFile(file, filePath, newFileName);
  17. return MessageBean.ok(filePath.substring(1) + newFileName, "上传图标文件");
  18. }
  19. public MessageBean<String> uploadVideo(MultipartFile file, String path) {
  20. String fileName = file.getOriginalFilename();
  21. String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
  22. if (!suffix.equals("mp4"))
  23. return MessageBean.error("上传文件类型必须是mp4视频文件");
  24. String newFileName = UUID.randomUUID() + "." + suffix;
  25. String filePath = "./upload/" + path;
  26. FileUtils.saveFile(file, filePath, newFileName);
  27. return MessageBean.ok(filePath.substring(1) + newFileName, "上传视频文件");
  28. }
  29. public MessageBean<String> uploadAudio(MultipartFile file, String path) {
  30. String fileName = file.getOriginalFilename();
  31. String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
  32. if (!suffix.equals("mp3"))
  33. return MessageBean.error("上传文件类型必须是mp3音频文件");
  34. String newFileName = UUID.randomUUID() + "." + suffix;
  35. String filePath = "./upload/" + path;
  36. FileUtils.saveFile(file, filePath, newFileName);
  37. return MessageBean.ok(filePath.substring(1) + newFileName, "上传音频文件");
  38. }
  39. public MessageBean<String> uploadModelCompress(MultipartFile file, String path) {
  40. String fileName = file.getOriginalFilename();
  41. String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
  42. if (!suffix.equals("zip") || !suffix.equals("rar")) {
  43. return MessageBean.error("上传文件类型必须是zip、rar格式的模型文件");
  44. }
  45. String newFileName = UUID.randomUUID() + "." + suffix;
  46. String filePath = "./upload/" + path;
  47. FileUtils.saveFile(file, filePath, newFileName);
  48. return MessageBean.ok(filePath.substring(1) + newFileName, "上传模型压缩文件");
  49. }
  50. public MessageBean<String> uploadModel(MultipartFile file, String path) {
  51. String fileName = file.getOriginalFilename();
  52. String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
  53. if (!suffix.equals("obj") && !suffix.equals("fbx"))
  54. return MessageBean.error("上传文件类型必须是obj、fbx格式的压缩文件");
  55. String newFileName = UUID.randomUUID() + "." + suffix;
  56. String filePath = "./upload/" + path;
  57. FileUtils.saveFile(file, filePath, newFileName);
  58. return MessageBean.ok(filePath.substring(1) + newFileName, "上传模型文件");
  59. }
  60. public MessageBean<String> uploadJsonFile(MultipartFile file, String path) {
  61. String fileName = file.getOriginalFilename();
  62. String suffix = fileName == null ? "" : fileName.substring(fileName.lastIndexOf(".") + 1);
  63. if (!suffix.equals("json"))
  64. return MessageBean.error("上传文件类型必须是json格式的json文件");
  65. String newFileName = UUID.randomUUID() + "." + suffix;
  66. String filePath = "./upload/" + path;
  67. FileUtils.saveFile(file, filePath, newFileName);
  68. return MessageBean.ok(filePath.substring(1) + newFileName, "上传模型文件");
  69. }
  70. }