UploadUtil.java
10.6 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package com.xiniunet.web.util;
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.xiniunet.foundation.contract.UploadTypeEnum;
import com.xiniunet.foundation.request.FileUploadRequest;
import com.xiniunet.foundation.response.FileUploadResponse;
import com.xiniunet.foundation.service.FoundationService;
import com.xiniunet.framework.security.Passport;
import com.xiniunet.framework.util.SettingUtil;
import com.xiniunet.framework.util.excel.Excel;
import com.xiniunet.framework.util.excel.OldExcel;
import com.xiniunet.framework.util.excel.datatable.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* Created on 2016-12-12.
*
* @author 小昊
*/
public class UploadUtil<T> {
protected Logger logger = LoggerFactory.getLogger(this.getClass());
private FoundationService foundationService;
public UploadUtil(FoundationService foundationService) {
this.foundationService = foundationService;
}
public String upload(DataTable<T> table, Long tenantId, Long id, Passport passport) {
return this.upload(table, tenantId, id, UploadUtil.FileType.XLSX, passport);
}
public String upload(DataTable<T> table, Long tenantId, Long id, UploadUtil.FileType fileType, Passport passport) {
byte[] bytes;
if(fileType == UploadUtil.FileType.XLSX) {
try {
bytes = (new Excel(false, new DataTable[]{table})).getBytes();
} catch (Exception var19) {
this.logger.error(var19.getMessage(), var19);
return null;
}
} else {
try {
bytes = (new OldExcel(false, new DataTable[]{table})).getBytes();
} catch (Exception var18) {
this.logger.error(var18.getMessage(), var18);
return null;
}
}
FileUploadRequest uploadRequest = new FileUploadRequest();
uploadRequest.setFileExt(fileType.toString().toLowerCase());
uploadRequest.setId(id);
uploadRequest.setType(UploadTypeEnum.TMP);
uploadRequest.setFileStream(bytes);
passport.setTenantId(tenantId);
FileUploadResponse uploadResponse = foundationService.uploadFile(uploadRequest, passport);
return uploadResponse.getUrl();
}
public String upload(DataTable<T> table, Long tenantId, String moduleName, UploadUtil.FileType fileType, Passport passport) {
String bucketName = "";
String urlPath = "";
String AccessKeyId = "";
String AccessKeySecret = "";
Properties prop = new Properties();
InputStream in = Object.class.getResourceAsStream("/setting.properties");
if(in == null) {
in = SettingUtil.getCaller(3).getResourceAsStream("/setting.properties");
}
try {
prop.load(in);
bucketName = prop.getProperty("aliyun.oss.bucket.name").trim();
urlPath = prop.getProperty("aliyun.oss.url").trim();
AccessKeyId = prop.getProperty("aliyun.accesskey.id").trim();
AccessKeySecret = prop.getProperty("aliyun.accesskey.secret").trim();
} catch (IOException var19) {
this.logger.error(var19.getMessage(), var19);
}
OSSClient client;
if(!"internal".equals(prop.getProperty("aliyun.oss.network"))) {
client = new OSSClient(AccessKeyId, AccessKeySecret);
} else {
client = new OSSClient("http://oss-cn-hangzhou-internal.aliyuncs.com", AccessKeyId, AccessKeySecret);
}
String fileName = moduleName + (new SimpleDateFormat("yyyyMMddHHmmss")).format(new Date());
String outputFilePath;
byte[] bytes;
if(fileType == UploadUtil.FileType.XLSX) {
outputFilePath = tenantId + "/tmp/" + fileName + ".xlsx";
try {
bytes = (new Excel(false, new DataTable[]{table})).getBytes();
} catch (Exception var18) {
this.logger.error(var18.getMessage(), var18);
return null;
}
} else {
outputFilePath = tenantId + "/tmp/" + fileName + ".xls";
try {
bytes = (new OldExcel(false, new DataTable[]{table})).getBytes();
} catch (Exception var17) {
this.logger.error(var17.getMessage(), var17);
return null;
}
}
ByteArrayInputStream content = new ByteArrayInputStream(bytes);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength((long)bytes.length);
client.putObject(bucketName, outputFilePath, content, meta);
return urlPath + outputFilePath;
}
public String upload(List<T> beanList, Long tenantId, Long id, Passport passport) {
try {
DataTable e = new DataTable(beanList);
return this.upload(e, tenantId, id, UploadUtil.FileType.XLSX, passport);
} catch (Exception var5) {
this.logger.error(var5.getMessage(), var5);
return null;
}
}
public String upload(List<T> beanList, Long tenantId, String moduleName, Passport passport) {
try {
DataTable e = new DataTable(beanList);
return this.upload(e, tenantId, moduleName, UploadUtil.FileType.XLSX, passport);
} catch (Exception var5) {
this.logger.error(var5.getMessage(), var5);
return null;
}
}
public String upload(List<T> beanList, Long tenantId, Long id, UploadUtil.FileType type, Passport passport) {
try {
DataTable e = new DataTable(beanList);
return this.upload(e, tenantId, id, type, passport);
} catch (Exception var6) {
this.logger.error(var6.getMessage(), var6);
return null;
}
}
public String uploadWeb(List<T> beanList, Long tenantId, Long id, Passport passport) {
String bucketName = "";
String urlPath = "";
String AccessKeyId = "";
String AccessKeySecret = "";
Properties prop = new Properties();
InputStream in = getCaller(4).getResourceAsStream("/setting.properties");
try {
prop.load(in);
bucketName = prop.getProperty("aliyun.oss.bucket.name").trim();
urlPath = prop.getProperty("aliyun.oss.url").trim();
AccessKeyId = prop.getProperty("aliyun.accesskey.id").trim();
AccessKeySecret = prop.getProperty("aliyun.accesskey.secret").trim();
} catch (IOException var18) {
this.logger.error(var18.getMessage(), var18);
}
OSSClient client;
if(!"production".equals(prop.getProperty("deploy.mode"))) {
client = new OSSClient(AccessKeyId, AccessKeySecret);
} else {
client = new OSSClient("http://oss-cn-hangzhou-internal.aliyuncs.com", AccessKeyId, AccessKeySecret);
}
String outputFilePath = tenantId + "/tmp/" + id + ".xlsx";
String fileUrl = urlPath + outputFilePath;
byte[] bytes;
try {
DataTable table = new DataTable(beanList);
bytes = (new Excel(false, new DataTable[]{table})).getBytes();
} catch (Exception var17) {
this.logger.error(var17.getMessage(), var17);
return null;
}
ByteArrayInputStream content = new ByteArrayInputStream(bytes);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength((long)bytes.length);
client.putObject(bucketName, outputFilePath, content, meta);
return fileUrl;
}
public String upload(Excel excel, Long tenantId, Long id, Passport passport) {
return this.upload(excel, tenantId, id, UploadUtil.FileType.XLSX, passport);
}
public String upload(Excel excel, Long tenantId, Long id, UploadUtil.FileType fileType, Passport passport) {
String bucketName = "";
String urlPath = "";
String AccessKeyId = "";
String AccessKeySecret = "";
Properties prop = new Properties();
InputStream in = Object.class.getResourceAsStream("/setting.properties");
if(in == null) {
in = SettingUtil.getCaller(3).getResourceAsStream("/setting.properties");
}
try {
prop.load(in);
bucketName = prop.getProperty("aliyun.oss.bucket.name").trim();
urlPath = prop.getProperty("aliyun.oss.url").trim();
AccessKeyId = prop.getProperty("aliyun.accesskey.id").trim();
AccessKeySecret = prop.getProperty("aliyun.accesskey.secret").trim();
} catch (IOException var20) {
this.logger.error(var20.getMessage(), var20);
}
OSSClient client;
if(!"production".equals(prop.getProperty("deploy.mode"))) {
client = new OSSClient(AccessKeyId, AccessKeySecret);
} else {
client = new OSSClient("http://oss-cn-hangzhou-internal.aliyuncs.com", AccessKeyId, AccessKeySecret);
}
String outputFilePath;
byte[] bytes;
if(fileType == UploadUtil.FileType.XLSX) {
outputFilePath = tenantId + "/tmp/" + id + ".xlsx";
try {
bytes = excel.getBytes();
} catch (Exception var19) {
this.logger.error(var19.getMessage(), var19);
return null;
}
} else {
outputFilePath = tenantId + "/tmp/" + id + ".xls";
try {
bytes = excel.getBytes();
} catch (Exception var18) {
this.logger.error(var18.getMessage(), var18);
return null;
}
}
ByteArrayInputStream content = new ByteArrayInputStream(bytes);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength((long)bytes.length);
client.putObject(bucketName, outputFilePath, content, meta);
String fileUrl = urlPath + outputFilePath;
return fileUrl;
}
public static Class getCaller(int level) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
Class clazz;
try {
String ex = stack[level].getClassName();
clazz = Class.forName(ex);
} catch (Exception var4) {
var4.printStackTrace();
clazz = Object.class;
}
return clazz;
}
public static enum FileType {
XLS,
XLSX;
private FileType() {
}
}
}