派生自 projectDept/qhighschool

EricsHu
2022-12-05 068fc7f2e81178e55fa191a13709af64b1a163f6
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.qxueyou.scc.sys.service.impl;
 
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
 
import javax.annotation.PostConstruct;
 
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
 
import com.qxueyou.scc.base.model.Result;
import com.qxueyou.scc.base.service.impl.CommonAppService;
import com.qxueyou.scc.config.SccConfig;
import com.qxueyou.scc.sys.service.IOssService;
 
/**
 *上传服务
 * 
 * @author 德虎
 * 
 */
@Service
@EnableConfigurationProperties(SccConfig.class)
public class OssService extends CommonAppService implements IOssService {
 
    /*
     * 
     */
//    private OSSClient client;
 
    private final Logger log = LogManager.getLogger("OssService");
    
    /*
     * 文档服务地址
     */
    private String resDomain;
    
    private static String resRootPath;
    
    @Autowired
    SccConfig sccConfig;
    
 
    /**
     * 初始化客户端
     */
    @PostConstruct
    private void init() {
        resRootPath = sccConfig.getResRootPath();
    }
 
    /*
     * (non-Javadoc)
     * 
     * @see
     * com.etraining.qxueyou.sys.service.IOssService#uplpadImage(java.io.File,
     * java.lang.String)
     */
    @Override
    public Result uplpadImage(File file, String destPath) {
        String fullPath = null;
        try {
            fullPath = resRootPath + destPath;
            File fileRoot = new File(fullPath.substring(0, destPath.lastIndexOf("/")));
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            FileUtils.copyFile(file, new File(fullPath));
            return new Result(true);
        } catch (Exception e) {
            log.error(e, e);
        }
        return new Result(false);
    }
    
    @Override
    public Result uplpadImage(InputStream ins,String fileFormat, String destPath) {
        String fullPath = null;
        try {
            fullPath = resRootPath + destPath;
            File fileRoot = new File(fullPath.substring(0, destPath.lastIndexOf("/")));
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            FileUtils.copyInputStreamToFile(ins, new File(fullPath));
            return new Result(true);
        } catch (Exception e) {
            log.error(e, e);
        }finally{
            IOUtils.closeQuietly(ins);
        }
        return new Result(false);
    }
    
    /*
     * (non-Javadoc)
     * @see com.etraining.qxueyou.sys.service.IOssService#uplpadVideo(java.io.InputStream, java.lang.String, long, java.lang.String)
     */
    @Override
    public Result uplpadFile(InputStream input,String fileFormat,long fileLength, String destPath) {
        String fullPath = null;
        try {
            fullPath = resRootPath + destPath;
            
            
            File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/")));
            
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            FileUtils.copyInputStreamToFile(input, new File(fullPath));
            return new Result(true);
        } catch (Exception e) {
            log.error(e, e);
        }finally{
            IOUtils.closeQuietly(input);
        }
        return new Result(false);
    }
    
    /**
     * 如果路径以 / 或 \ 开头,需要截取
     * @param destPath
     * @return
     */
    public String chopPath(String destPath){
        if(destPath.startsWith("/")||destPath.startsWith("\\")){
            return destPath.substring(1);
        }
        return destPath;
    }
    
    /**
     * 获取ContentType
     * @param fileName
     * @return
     */
    public String getContentType(String fileName){
        if(fileName.toUpperCase().endsWith("PNG")){
            return "image/png";
        }
        return "image/jpeg";
    }
 
    @Override
    public String getServerUrl() {
        return resDomain;
    }
 
    @Override
    public Result uplpadFile(File file, String destPath) {
        String fullPath = null;
        try {
            fullPath = resRootPath + destPath;
            File fileRoot = new File(fullPath.substring(0, destPath.lastIndexOf("/")));
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            FileUtils.copyFile(file,new File(fullPath));
            
            return new Result(true);
        } catch (Exception e) {
            log.error(e, e);
        }
        return new Result(false);
    }
 
    /*
     * (non-Javadoc)
     * @see com.etraining.qxueyou.sys.service.IOssService#getObject(java.lang.String, java.lang.String)
     */
    @Override
    public File getObject(String key, String directory) {
        File sourceFile = null;
        File targetFile = null;
        String fullPath = null;
        try {
            
            sourceFile = new File(resRootPath + key);
            if(sourceFile.exists()){
                fullPath = (resRootPath + directory).replace("//", "/");
                
                File fileRoot = new File(fullPath.substring(0, fullPath.lastIndexOf("/")));
                if(!fileRoot.exists()){
                    fileRoot.mkdirs();    
                }
                targetFile = new File(resRootPath + directory);
                FileUtils.copyFile(sourceFile, targetFile);
            }
        } catch (Exception e) {
            log.error(e, e);
        }
        return targetFile;
    }
 
    /*
     * (non-Javadoc)
     * @see com.etraining.qxueyou.sys.service.IOssService#getObject(java.lang.String)
     */
    @Override
    public InputStream getObject(String key) {
        File sourceFile = null;
        InputStream in = null;
        try {
            sourceFile = new File(resRootPath + key);
            if(sourceFile.exists()){
                in = new FileInputStream(sourceFile);
            }
        } catch (Exception e) {
            log.error(e, e);
        }
        return in;
    }
 
    @Override
    public Result uploadFile(byte[] byteArray, String destPath) {
        InputStream  in = null;
        
        String fullPath = null;
        try {
            fullPath = resRootPath + destPath;
            File fileRoot = new File(fullPath.substring(0, destPath.lastIndexOf("/")));
            if(!fileRoot.exists()){
                fileRoot.mkdirs();    
            }
            in= new BufferedInputStream(new ByteArrayInputStream(byteArray));
            FileUtils.copyInputStreamToFile(in, new File(fullPath));
            return new Result(true);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage(), e);
            return new Result(false);
        }finally {
            IOUtils.closeQuietly(in);
        }
    }
    
    @Override
    public void deleteOSSFile (String destPath) {
        FileUtils.deleteQuietly(new File(resRootPath+destPath));
    }
    
}