peter
2025-11-19 eb357bb9938d569734b04948695726b80c5dfb85
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
109
110
111
112
113
114
115
package com.nq.service.impl;
 
import com.google.common.collect.Lists;
import com.nq.common.ServerResponse;
import com.nq.service.IFileUploadService;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
 
import com.nq.utils.PropertiesUtil;
import com.nq.utils.ftp.FTPUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
 
@Service("iFileUploadService")
 
public class FileUploadServiceImpl
 
        implements IFileUploadService {
 
    private static final Logger log = LoggerFactory.getLogger(FileUploadServiceImpl.class);
 
 
    public ServerResponse upload(MultipartFile file, String path) {
        path = PropertiesUtil.getProperty("ftp.address");
        // 参数校验
        if (file == null || file.isEmpty()) {
            return ServerResponse.createByErrorMsg("文件不能为空");
        }
 
        try {
            // 获取原始文件名
            String fileName = file.getOriginalFilename();
            if (fileName == null || fileName.trim().isEmpty()) {
                return ServerResponse.createByErrorMsg("文件名无效");
            }
 
            // 安全地获取文件扩展名
            String fileExtensionName = "";
            int lastDotIndex = fileName.lastIndexOf(".");
            if (lastDotIndex > 0 && lastDotIndex < fileName.length() - 1) {
                fileExtensionName = fileName.substring(lastDotIndex + 1).toLowerCase();
            } else {
                return ServerResponse.createByErrorMsg("文件格式错误,无法识别扩展名");
            }
 
            // 验证文件扩展名是否合法
            if (!isValidFileExtension(fileExtensionName)) {
                return ServerResponse.createByErrorMsg("不支持的文件类型: " + fileExtensionName);
            }
 
            // 生成唯一文件名:时间戳 + 随机数
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS").format(new Date());
            int randomNum = new Random().nextInt(99999);
            String uploadFileName = timeStamp + "_" + String.format("%05d", randomNum) + "." + fileExtensionName;
 
            // 确保目标目录存在
            File targetDir = new File(path);
            if (!targetDir.exists()) {
                if (!targetDir.mkdirs()) {
                    return ServerResponse.createByErrorMsg("无法创建上传目录");
                }
            }
 
            // 创建目标文件
            File targetFile = new File(targetDir, uploadFileName);
 
            // 保存文件
            file.transferTo(targetFile);
 
            // 验证文件是否成功保存
            if (!targetFile.exists() || targetFile.length() == 0) {
                return ServerResponse.createByErrorMsg("文件保存失败");
            }
 
            return ServerResponse.createBySuccess(uploadFileName);
 
        } catch (IOException e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("文件上传失败: " + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("上传过程发生异常: " + e.getMessage());
        }
    }
 
    /**
     * 验证文件扩展名是否合法
     */
    private boolean isValidFileExtension(String fileExtension) {
        if (fileExtension == null || fileExtension.trim().isEmpty()) {
            return false;
        }
 
        // 定义允许的文件类型
        String[] allowedExtensions = {
                "jpg", "jpeg", "png"
        };
 
        for (String allowedExt : allowedExtensions) {
            if (allowedExt.equalsIgnoreCase(fileExtension)) {
                return true;
            }
        }
 
        return false;
    }
 
}