zj
2024-06-03 287ac389edd047696d956afafdb855a93830bc0c
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
package com.nq.service.impl;
 
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.util.UUID;
 
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) {
 
        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("上传失败");
 
    }
 
}