package com.hivekion.file; import com.hivekion.common.exception.BusinessException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; @Component public class FileUntils { @Value("${importFile.3D}") private String uploadPath; public String getFilePath(MultipartFile file) { String fileName = file.getOriginalFilename(); String nowDate = new SimpleDateFormat("yyyyMMdd").format((new Date())); String fileDir = uploadPath + "/" + nowDate + "/" + System.currentTimeMillis(); File dir = new File(fileDir); if (!dir.exists()) { boolean mFlag = dir.mkdirs(); if (!mFlag){ throw new BusinessException(500,"新建文件目录失败"); } } String newFileName = fileDir + "/" + fileName; assert fileName != null; if(!fileName.endsWith("ive")){ throw new BusinessException(500,"请选择正确的文件格式(.ive)"); } File newFile = new File(newFileName); try { file.transferTo(newFile); } catch ( IOException e) { e.printStackTrace(); } return newFile.getPath(); } }