文件上传工具类

FileUtil.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.cmbchina.ccd.itpm.utils;
 
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;
 
import static org.apache.tomcat.util.http.fileupload.FileUtils.cleanDirectory;
 
public class FileUtil {
    private FileUtil() {
 
    }
 
    public static void uploadFile(byte[] file, String filePath, String fileName) throws IOException {
        File targetFile = new File(filePath);
        if (!targetFile.exists()) {
            if (!targetFile.mkdirs()) {
                throw new IOException();
            }
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(filePath + fileName);
            out.write(file);
            out.flush();
        } catch (IOException e) {
            throw new IOException();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
 
    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        }
        return false;
 
    }
 
    public static String renameToUUID(String fileName) {
        return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
    }
 
    /**
     * 文件删除方法
     *
     * @param fileAddress
     * @return
     */
    public static boolean deleteQuietly(String fileAddress) {
        File file = new File(fileAddress);
        if (file == null) {
            return false;
        } else {
            try {
                if (file.isDirectory()) {
                    cleanDirectory(file);
                }
            } catch (Exception var3) {
                ;
            }
 
            try {
                return file.delete();
            } catch (Exception var2) {
                return false;
            }
        }
    }
 
    /**
     * 文件保存方法
     *
     * @param file
     * @param destination
     * @return
     * @throws IllegalStateException
     * @throws IOException
     */
    public static String saveFile(MultipartFile file, String destination) throws IllegalStateException, IOException {
        // 获取上传的文件名称,并结合存放路径,构建新的文件名称
        String filename = file.getOriginalFilename();
        File filepath = new File(destination, filename);
 
        // 判断路径是否存在,不存在则新创建一个
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }
 
        // 将上传文件保存到目标文件目录
        file.transferTo(new File(destination + File.separator + filename));
        String fileMD5String = MD5Util.getFileMD5String(new File(destination + File.separator + filename));
        return fileMD5String;
    }
 
}

  

posted @   少说点话  阅读(1062)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
网站运行:7年51天17时24分1秒
点击右上角即可分享
微信分享提示