ApkVersionServiceImpl.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.jsh.erp.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.jsh.erp.datasource.entities.ApkVersion;
  4. import com.jsh.erp.datasource.mappers.ApkVersionMapper;
  5. import com.jsh.erp.service.ApkVersionService;
  6. import com.jsh.erp.utils.FileUtils;
  7. import com.jsh.erp.utils.Tools;
  8. import lombok.RequiredArgsConstructor;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.util.FileCopyUtils;
  13. import org.springframework.web.multipart.MultipartFile;
  14. import javax.servlet.http.HttpServletRequest;
  15. import java.io.File;
  16. import java.io.IOException;
  17. @Service
  18. @RequiredArgsConstructor
  19. @Slf4j
  20. public class ApkVersionServiceImpl extends ServiceImpl<ApkVersionMapper, ApkVersion> implements ApkVersionService {
  21. @Value(value="${file.apkPath}")
  22. private String apkPath;
  23. /**
  24. * apk文件上传
  25. */
  26. @Override
  27. public String uploadLocal(MultipartFile mf, HttpServletRequest request) {
  28. try {
  29. String token = request.getHeader("X-Access-Token");
  30. Long tenantId = Tools.getTenantIdByToken(token);
  31. String ctxPath = apkPath;
  32. String fileName = null;
  33. File file = new File(ctxPath + File.separator);
  34. if (!file.exists()) {
  35. file.mkdirs();// 创建文件根目录
  36. }
  37. String orgName = mf.getOriginalFilename();// 获取文件名
  38. orgName = FileUtils.getFileName(orgName);
  39. // 校验文件类型
  40. String[] allowedExtensions = {".apk"};
  41. boolean isValidExtension = false;
  42. for (String ext : allowedExtensions) {
  43. if (orgName.toLowerCase().endsWith(ext)) {
  44. isValidExtension = true;
  45. break;
  46. }
  47. }
  48. if (!isValidExtension) {
  49. throw new IllegalArgumentException("Invalid file type");
  50. }
  51. if(orgName.contains(".")){
  52. fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf("."));
  53. }else{
  54. fileName = orgName+ "_" + System.currentTimeMillis();
  55. }
  56. String savePath = file.getPath() + File.separator + fileName;
  57. File savefile = new File(savePath);
  58. FileCopyUtils.copy(mf.getBytes(), savefile);
  59. // 返回路径
  60. String dbpath = fileName;
  61. if (dbpath.contains("\\")) {
  62. dbpath = dbpath.replace("\\", "/");
  63. }
  64. return apkPath + "/" +dbpath;
  65. } catch (IOException e) {
  66. log.error(e.getMessage(), e);
  67. }
  68. return "";
  69. }
  70. }