Your Name
2023-01-30 6bc9483b5bf1d72f6365e23ebcd5703a180bbb90
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
package com.java110.core.client;
 
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.COSUtil;
import com.java110.utils.util.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.InputStream;
 
@Component
public class FileUploadTemplate {
 
    private static final String ROOT_PATH = "hc/";
 
 
    @Autowired
    private FtpUploadTemplate ftpUploadTemplate;
 
    @Autowired
    private JSchFtpUploadTemplate jSchFtpUploadTemplate;
 
    @Autowired
    private OssUploadTemplate ossUploadTemplate;
 
    @Autowired
    private CosUploadTemplate cosUploadTemplate;
 
 
    public String saveFile(InputStream inputStream,String fileName){
 
        String newfileName = ROOT_PATH+ fileName;
 
        String ossSwitch = MappingCache.getValue(MappingConstant.FILE_DOMAIN, OSSUtil.OSS_SWITCH);
 
        if (OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) {
            newfileName = ossUploadTemplate.upload(inputStream, newfileName);
        } else if (COSUtil.COS_SWITCH_COS.equals(ossSwitch)) {
            newfileName = cosUploadTemplate.upload(inputStream, newfileName);
        } else {
            String ftpServer = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_SERVER);
            int ftpPort = Integer.parseInt(MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PORT));
            String ftpUserName = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERNAME);
            String ftpUserPassword = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERPASSWORD);
 
            newfileName = ftpUploadTemplate.upload(inputStream, ftpServer,
                    ftpPort, ftpUserName,
                    ftpUserPassword, newfileName);
        }
        return newfileName;
    }
 
    public InputStream downloadFile(String fileName){
 
        InputStream inputStream = null;
 
        String ossSwitch = MappingCache.getValue(MappingConstant.FILE_DOMAIN, OSSUtil.OSS_SWITCH);
 
        if (OSSUtil.OSS_SWITCH_OSS.equals(ossSwitch)) {
            inputStream = ossUploadTemplate.download(fileName);
        } else if (COSUtil.COS_SWITCH_COS.equals(ossSwitch)) {
            inputStream = cosUploadTemplate.download(fileName);
        } else {
            String ftpServer = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_SERVER);
            int ftpPort = Integer.parseInt(MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_PORT));
            String ftpUserName = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERNAME);
            String ftpUserPassword = MappingCache.getValue(FtpUploadTemplate.FTP_DOMAIN, FtpUploadTemplate.FTP_USERPASSWORD);
 
            inputStream = ftpUploadTemplate.download(fileName, ftpServer,
                    ftpPort, ftpUserName,
                    ftpUserPassword);
        }
 
        return inputStream;
    }
}