1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package com.flow.utils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.File;
- import java.io.IOException;
- import java.util.UUID;
- public class FileUtil {
- private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
- public static File upload(MultipartFile file, String filePath) {
- // 日期字符串
- String format = UUID.randomUUID().toString();
- // 文件名称
- String fileName = file.getOriginalFilename();
- try {
- // 路径
- String path = String.format("%s%s__%s", filePath, format, fileName);
- File dest = new File(path).getCanonicalFile();
- // 检测是否存在目录
- if (!dest.getParentFile().exists()) {
- // 创建目录
- if (!dest.getParentFile().mkdirs()) {
- log.error("创建目录失败: {}", dest.getParentFile().getPath());
- }
- }
- // 文件写入
- file.transferTo(dest);
- return dest;
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- }
- return null;
- }
- public static void del(String filePath) {
- if (filePath != null && filePath.length() > 0) {
- File file = new File(filePath);
- if (file.exists()) {
- file.delete();
- }
- }
- }
- public static String getSuffix(String fileName) {
- return fileName.substring(fileName.lastIndexOf("."));
- }
- }
|