peter
2025-11-26 566a1b9fda0276e2cc4a35f7ba322c0e599a2c84
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.nq.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.nq.common.ServerResponse;
import com.nq.service.IFileUploadService;
import com.nq.utils.FTPUtil;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
 
import com.nq.utils.PropertiesUtil;
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);
 
 
    @Override
    public ServerResponse uploadFile(MultipartFile file) {
        // 兼容c端组件上传原理
        String fileType = file.getOriginalFilename();
        if (StrUtil.isEmpty(fileType) || fileType.contains("blob")) {
            fileType = "blob.png";
        }
 
        String fileName = file.getOriginalFilename();
        String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1);
 
        // 生成唯一的文件名
        String id = UUID.randomUUID().toString();
        String path = LocalDate.now() + "/" + id + "." + fileExtentionName;
 
        // 确保目标文件夹存在
        File targetDir = new File(PropertiesUtil.getProperty("loca.images.dir")  + "/" + LocalDate.now());
        if (!targetDir.exists()) {
            targetDir.mkdirs();
        }
 
        // 构建本地文件路径
        File localFile = new File(targetDir, id + "." + fileExtentionName);
 
        // 打印上传路径
        log.info("LocalFileUploadService uploadFile localFilePath: {}", localFile.getAbsolutePath());
 
        try {
            // 将文件保存到本地
            file.transferTo(localFile);
 
        } catch (IOException e) {
            log.error("LocalFileUploadService uploadFile IOException", e.getMessage(), e);
        }
        // 返回相对路径
        return ServerResponse.createBySuccess(path);
    }
 
 
//    public ServerResponse uploadFile(MultipartFile file) {
//        // 兼容c端组件上传原理
//
//        String fileName = file.getOriginalFilename();
//
//
//        String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1);
//
//
//        String uploadFileName = UUID.randomUUID() + "." + fileExtentionName;
//
//        String path = LocalDate.now() + "/" + uploadFileName;
//
//        // 确保目标文件夹存在
//        File targetDir = new File(PropertiesUtil.getProperty("ftp.server.http.prefix")  + "/" + LocalDate.now());
//        if (!targetDir.exists()) {
//            targetDir.mkdirs();
//        }
//
//        // 构建本地文件路径
//        File localFile = new File(targetDir, uploadFileName);
//
//        // 打印上传路径
//        log.info("LocalFileUploadService uploadFile localFilePath: {}", localFile.getAbsolutePath());
//
//        try {
//            // 将文件保存到本地
//            file.transferTo(localFile);
//
//            // 如果需要自定义元数据,可以在此处理
//            Map<String, String> metadata = new HashMap<>();
//            metadata.put("x-amz-meta-myVal", "test");  // 如果需要元数据,可以放在文件名或其他地方
//
//
//
//        } catch (IOException e) {
//            log.error("LocalFileUploadService uploadFile IOException", e.getMessage(), e);
//            log.error("上传文件异常 , 错误信息 = {}", e);
//        }
//        // 返回相对路径
//        return ServerResponse.createBySuccess(path);
//    }
 
 
 
 
    public ServerResponse upload(MultipartFile file, String path) {
 
        String fileName = file.getOriginalFilename();
 
 
        String fileExtentionName = fileName.substring(fileName.lastIndexOf(".") + 1);
 
 
        String uploadFileName = UUID.randomUUID() + "." + fileExtentionName;
 
 
        File fileDir = new File(path);
 
 
        if (!fileDir.exists()) {
 
 
            fileDir.setWritable(true);
 
            fileDir.mkdirs();
 
        }
 
 
        File tartgetFile = new File(path, uploadFileName);
 
        boolean result = false;
 
        try {
 
            file.transferTo(tartgetFile);
 
 
            result = FTPUtil.uploadFile(Lists.newArrayList(new File[]{tartgetFile}));
 
 
            tartgetFile.delete();
 
        } catch (Exception e) {
 
            log.error("上传文件异常 , 错误信息 = {}", e);
 
            return null;
 
        }
 
 
        if (result) {
 
            return ServerResponse.createBySuccess(tartgetFile.getName());
 
        }
 
        return ServerResponse.createByErrorMsg("上传失败");
 
    }
 
}