package com.ld.igds.file;
|
|
import com.bstek.bdf2.core.context.ContextHolder;
|
import com.bstek.dorado.annotation.Expose;
|
import com.bstek.dorado.uploader.DownloadFile;
|
import com.bstek.dorado.uploader.UploadFile;
|
import com.bstek.dorado.uploader.annotation.FileProvider;
|
import com.bstek.dorado.uploader.annotation.FileResolver;
|
import com.ld.igds.util.ContextUtil;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.time.DateUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.net.URLDecoder;
|
import java.util.Date;
|
import java.util.Map;
|
|
/**
|
* 工单附件文件上传、下载、删除等操作
|
*
|
* @author Andy
|
*/
|
@Slf4j
|
@Component
|
public class ApplyOrderUploadProcessor {
|
|
@Autowired
|
private CoreFileService fileService;
|
|
/**
|
* applyOrderUploadProcessor#uploadApplyOrderFile
|
*
|
* @param file
|
* @param parameter
|
* @return fileId 新文件ID包含文件后缀
|
*/
|
@FileResolver
|
public String uploadApplyOrderFile(UploadFile file, Map<String, Object> parameter) {
|
|
log.debug("---uploadApplyOrderFile-----{}", file.getFileName());
|
String fileId = null;
|
try {
|
|
String basePath = fileService.getCommonFilePath(null);
|
|
fileId = ContextUtil.getTimeId(1000)
|
+ file.getFileName().substring(
|
file.getFileName().lastIndexOf("."));
|
|
file.transferTo(new File(basePath + fileId));
|
|
} catch (IllegalStateException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
|
return fileId;
|
}
|
|
/**
|
* applyOrderUploadProcessor#downloadApplyOrderFile 文件下载
|
*
|
* @param parameter
|
* @return
|
* @throws IOException
|
*/
|
@FileProvider
|
public DownloadFile downloadApplyOrderFile(Map<String, Object> parameter) throws Exception {
|
InputStream inputStream = null;
|
try {
|
String info = ContextHolder.getRequest().getHeader("User-Agent");
|
String fileName = (String) parameter.get("fileName");
|
String fileId = (String) parameter.get("fileId");
|
String timeStr = (String) parameter.get("createTime");
|
|
Date createTime = DateUtils.parseDate(timeStr, "yyyy-MM-dd hh:mm:ss");
|
|
if (null == createTime) createTime = new Date();
|
|
fileName = URLDecoder.decode(fileName, "UTF-8");
|
|
|
log.debug("---downloadApplyOrderFile-----{}", fileName);
|
|
|
File file = new File(fileService.getCommonFilePath(createTime), fileId);
|
|
if (!file.exists()) {
|
throw new Exception("附件不存在");
|
}
|
|
inputStream = new FileInputStream(file);
|
DownloadFile dlFile = new DownloadFile(fileId, inputStream);
|
|
// 浏览器的默认编码不同,IE下BGK
|
if (info.indexOf("MSIE") != -1) {
|
dlFile.setFileName(new String(fileName.getBytes("GBK"), "iso-8859-1"));
|
} else {// firefox、chrome、safari、opera
|
dlFile.setFileName(new String(fileName.getBytes("UTF-8"), "iso-8859-1"));
|
}
|
return dlFile;
|
|
} catch (Exception e) {
|
if (null != inputStream)
|
inputStream.close();
|
log.error("下载附件执行失败:{}", e.getMessage());
|
}
|
return null;
|
}
|
|
/**
|
* applyOrderUploadProcessor#delFile
|
*
|
* @param parameter
|
* @return
|
* @throws IOException
|
*/
|
@Expose
|
public String delFile(Map<String, Object> parameter) throws Exception {
|
|
String fileName = (String) parameter.get("fileName");
|
String fileId = (String) parameter.get("fileId");
|
Date createTime = (Date) parameter.get("createTime");
|
if (null == createTime) createTime = new Date();
|
fileName = URLDecoder.decode(fileName, "UTF-8");
|
log.debug("---delFile-----{}", fileName);
|
|
File file = new File(fileService.getCommonFilePath(createTime), fileId);
|
|
if (!file.exists()) {
|
return "删除失败,系统未找到当前附件";
|
}
|
|
file.delete();
|
fileService.delFile(fileId);
|
return null;
|
}
|
|
}
|