package com.jsh.erp.service.impl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.jsh.erp.datasource.entities.ApkVersion; import com.jsh.erp.datasource.mappers.ApkVersionMapper; import com.jsh.erp.service.ApkVersionService; import com.jsh.erp.utils.FileUtils; import com.jsh.erp.utils.Tools; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; @Service @RequiredArgsConstructor @Slf4j public class ApkVersionServiceImpl extends ServiceImpl implements ApkVersionService { @Value(value="${file.apkPath}") private String apkPath; /** * apk文件上传 */ @Override public String uploadLocal(MultipartFile mf, HttpServletRequest request) { try { String token = request.getHeader("X-Access-Token"); Long tenantId = Tools.getTenantIdByToken(token); String ctxPath = apkPath; String fileName = null; File file = new File(ctxPath + File.separator); if (!file.exists()) { file.mkdirs();// 创建文件根目录 } String orgName = mf.getOriginalFilename();// 获取文件名 orgName = FileUtils.getFileName(orgName); // 校验文件类型 String[] allowedExtensions = {".apk"}; boolean isValidExtension = false; for (String ext : allowedExtensions) { if (orgName.toLowerCase().endsWith(ext)) { isValidExtension = true; break; } } if (!isValidExtension) { throw new IllegalArgumentException("Invalid file type"); } if(orgName.contains(".")){ fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.indexOf(".")); }else{ fileName = orgName+ "_" + System.currentTimeMillis(); } String savePath = file.getPath() + File.separator + fileName; File savefile = new File(savePath); FileCopyUtils.copy(mf.getBytes(), savefile); // 返回路径 String dbpath = fileName; if (dbpath.contains("\\")) { dbpath = dbpath.replace("\\", "/"); } return apkPath + "/" +dbpath; } catch (IOException e) { log.error(e.getMessage(), e); } return ""; } }