Newer
Older
package com.cesgroup.bdc.attachment.controller;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cesgroup.bdc.attachment.dto.FileUploaderResponseDto;
import com.cesgroup.bdc.attachment.entity.Attachment;
import com.cesgroup.bdc.attachment.entity.DiskFile;
import com.cesgroup.bdc.attachment.service.IAttachmentService;
import com.cesgroup.bdc.attachment.service.IDiskFileService;
import com.cesgroup.bdc.logs.entity.LogType;
import com.cesgroup.bdc.logs.service.ILogsService;
import com.cesgroup.bdc.util.DownloadUtil;
import com.cesgroup.kingkong.core.security.model.IOrganization;
import com.cesgroup.kingkong.core.security.model.IUser;
import com.cesgroup.kingkong.web.BaseServiceController;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
/**
* <p>
* 附件 前端控制器
* </p>
*
* @author shen.shaohua
* @since 2019-07-02
*/
@Controller
@RequestMapping("/attachment")
public class AttachmentController extends BaseServiceController<Attachment, IAttachmentService> {
@Autowired
private IDiskFileService diskFileService;
@Autowired
private ILogsService logsService;
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* 默认文件存储路径
*/
@Value("${file.upload.path}")
private String fileBasePath;
/**
* 文件上传
* <p>
* 1.上传文件对应的名称默认为:uploadFile
* 2.返回默认的名称为:files
* </p>
*
* @return 上传的文件
* @author: shen.shaohua
* @since: 2019/7/2 13:37
*/
@RequestMapping(value = "/upload/{module}/{relevanceId}", method = RequestMethod.POST)
@ResponseBody
public Map<String, List<FileUploaderResponseDto>> saveMultiUpload(@RequestParam("uploadFile") MultipartFile[] files,
@PathVariable String module, @PathVariable String relevanceId,
HttpSession session) {
Map<String, List<FileUploaderResponseDto>> result = new HashMap<>();
//HttpServletRequest request = SpringMvcWebUtils.getRequest();
try {
String fileRelativePath = DateFormatUtils.format(new Date(), "yyyyMM") + File.separatorChar;
IUser uu = getUser();
IOrganization org = getOrg(session);
List<FileUploaderResponseDto> responseDtos = this.getBaseService().saveMultiFile2(files, module, relevanceId, fileRelativePath, uu, org);
//与组件库对应的固定写法
result.put("files", responseDtos);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
return result;
}
@RequestMapping("/updateSecurityLevelFile")
@ResponseBody
public Map<String, String> updateSecurityLevelFile (String ids, String securityLevelFile,HttpSession session) {
String[] idArr = ids.split(",");
List<String> idList = CollUtil.newArrayList(idArr);
QueryWrapper<Attachment> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id",idList);
List<Attachment> attachments= this.getBaseService().list(queryWrapper);
attachments.forEach(a->{
a.setSecurityLevelFile(securityLevelFile);
});
boolean result = this.getBaseService().saveOrUpdateBatch(attachments);
Map<String, String> mm = new HashMap<>();
if(result){
mm.put("code", "0");
}else{
mm.put("code", "-1");
}
return mm;
}
/**
* 下载
*
* @param id 文件id
* @param response
* @author: shen.shaohua
* @since: 2019/7/9 16:46
*/
@RequestMapping(value = "/download/{id}")
public void download(@PathVariable(value = "id") String id, HttpServletResponse response, HttpServletRequest request) {
FileInputStream fis = null;
ServletOutputStream sos = null;
try {
Attachment attachment = getBaseService().getById(id);
DiskFile diskFile = diskFileService.getById(attachment.getDiskFileId());
File file = getBaseService().getOfdFileByAttachment(attachment);
if (file.exists()) {
response.setStatus(200);
response.setContentType("application/x-msdownload");
String fileName = diskFile.getOriginalName();
fileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".ofd";
response.setHeader("Content-Disposition", DownloadUtil.getContentDisposition(request, fileName));
sos = response.getOutputStream();
byte[] buffer = new byte[1024 * 1024];
response.setContentLength((int) file.length());
fis = new FileInputStream(file);
int readBytes = -1;
while ((readBytes = fis.read(buffer)) != -1) {
sos.write(buffer, 0, readBytes);
}
logsService.operateLog(request, this.getUser().getId(), this.getUser().getUserName(), LogType.DOWNLOAD, "成功", "附件", "", "收件箱", diskFile.getOriginalName() + "." + diskFile.getExt());
sos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(fis);
IoUtil.close(sos);
}
}
/**
* 下载原附件
*
* @param id 文件id
* @param response
* @author: shen.shaohua
* @since: 2019/9/22 20:57
*/
@RequestMapping(value = "/downloadOriginal/{id}")
public void downloadOriginal(@PathVariable(value = "id") String id, HttpServletResponse response, HttpServletRequest request) {
FileInputStream fis = null;
ServletOutputStream sos = null;
try {
Attachment attachment = getBaseService().getById(id);
DiskFile diskFile = diskFileService.getById(attachment.getDiskFileId());
File file = getBaseService().getFileByAttachment(attachment);
if (file.exists()) {
response.setStatus(200);
response.setContentType("application/x-msdownload");
String fileName = diskFile.getOriginalName();
;
response.setHeader("Content-Disposition", DownloadUtil.getContentDisposition(request, fileName));
sos = response.getOutputStream();
byte[] buffer = new byte[1024 * 1024];
response.setContentLength((int) file.length());
fis = new FileInputStream(file);
int readBytes = -1;
while ((readBytes = fis.read(buffer)) != -1) {
sos.write(buffer, 0, readBytes);
}
logsService.operateLog(request, this.getUser().getId(), this.getUser().getUserName(), LogType.DOWNLOAD, "成功", "附件", "", "收件箱", diskFile.getOriginalName() + "." + diskFile.getExt());
sos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(fis);
IoUtil.close(sos);
}
}
/**
* 下载
*
* @param id 文件id
* @param response
* @author: shen.shaohua
* @since: 2019/9/16 19:11
*/
@RequestMapping(value = "/viewOfd/{id}")
public void viewOfd(@PathVariable(value = "id") String id, HttpServletResponse response, HttpServletRequest request) {
FileInputStream fis = null;
ServletOutputStream sos = null;
try {
Attachment attachment = getBaseService().getById(id);
DiskFile diskFile = diskFileService.getById(attachment.getDiskFileId());
File file = getBaseService().getOfdFileByAttachment(attachment);
if (file.exists()) {
response.setStatus(200);
response.setContentType("application/x-msdownload");
String fileName = diskFile.getOriginalName();
fileName = fileName.substring(0, fileName.lastIndexOf('.')) + ".ofd";
response.setHeader("Content-Disposition", DownloadUtil.getContentDisposition(request, fileName));
sos = response.getOutputStream();
byte[] buffer = new byte[1024 * 1024];
response.setContentLength((int) file.length());
fis = new FileInputStream(file);
int readBytes = -1;
while ((readBytes = fis.read(buffer)) != -1) {
sos.write(buffer, 0, readBytes);
}
sos.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(fis);
IoUtil.close(sos);
}
}
/**
* 删除附件
*
* @param id 附件id
* @return
* @author: shen.shaohua
* @since: 2019/7/2 13:37
*/
@RequestMapping(value = "/del/{id}")
@ResponseBody
public Boolean del(@PathVariable String id) {
Boolean b = false;
getBaseService().deleteFile(id);
b = true;
return b;
}
/**
* 获取附件列表
*
* @param module
* @param relevanceId
* @return
*/
@RequestMapping(value = "/find/{module}/{relevanceId}")
@ResponseBody
public List<FileUploaderResponseDto> find(@PathVariable String module, @PathVariable String relevanceId) {
List<Attachment> attachmentList = getBaseService().selectByModuleAndRelevanceId(module, relevanceId);
List<FileUploaderResponseDto> list = new ArrayList<>(attachmentList.size());
for (Attachment attachment : attachmentList) {
DiskFile diskFile = diskFileService.getById(attachment.getDiskFileId());
FileUploaderResponseDto dto = new FileUploaderResponseDto(attachment, diskFile);
list.add(dto);
}
return list;
}
@RequestMapping(value = "/uploadOnlyOneDZYZ/{attachmentId}", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> uploadOnlyOneDZYZ(@PathVariable String attachmentId,
HttpServletRequest request) {
MultipartHttpServletRequest mhs = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = mhs.getFileMap();
System.out.println("通过Map.keySet遍历key和value:");
MultipartFile mFile = null;
for (String key : fileMap.keySet()) {
MultipartFile file = fileMap.get(key);
mFile = file;
break;
}
Map<String, Object> result = new HashMap<>();
try {
Attachment attachment = getBaseService().getById(attachmentId);
File oldFile = getBaseService().getFile(attachment);//原文件
String oldFileName = oldFile.getName();
String newFileName = oldFileName.substring(0, oldFileName.lastIndexOf(".")) + ".ofd";
// 覆盖原有文件
File newFile = new File(oldFile.getParent(), newFileName);//新文件
mFile.transferTo(newFile);
DiskFile diskFile = diskFileService.getById(attachment.getDiskFileId());
// 更新文件大小
diskFile.setFileSize(FileUtil.size(newFile));
diskFileService.updateById(diskFile);
result.put("status", "success");
} catch (Exception e) {
e.printStackTrace();
result.put("status", "error");
result.put("errMsg", e.toString());
}
return result;
}
}