package com.ld.igds.basic.manager;
|
|
import com.ld.igds.constant.RespCodeEnum;
|
import com.ld.igds.data.PageResponse;
|
import com.ld.igds.file.CoreFileService;
|
import com.ld.igds.util.ContextUtil;
|
import com.ld.igds.util.FilesUtil;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.Date;
|
|
/**
|
* 文件上传管理类
|
*
|
* @author
|
*/
|
@Slf4j
|
@Component
|
public class FileManager {
|
|
@Autowired
|
private CoreFileService fileService;
|
@Resource
|
private FilesUtil filesUtil;
|
|
|
/**
|
* 出入库的附件上次,参数是车牌号
|
*
|
* @param file
|
* @return
|
* @throws IOException
|
*/
|
public PageResponse<String> upLoadInoutImg(MultipartFile file, String plateNum)
|
throws IOException {
|
|
log.debug("上传出入库的附件……");
|
|
// 获取默认编码的字节数组
|
byte[] fileByte = file.getBytes();
|
|
// 获取文件的源文件名称
|
String oldFileName = file.getOriginalFilename();
|
|
// 获取文件保存路径
|
String filePath = fileService.getInoutFilePath(new Date());
|
|
// 获取新的ID
|
String newFileName = ContextUtil.getUUID();
|
|
// 文件后缀名
|
String suffixName = oldFileName.substring(oldFileName.lastIndexOf("."));
|
|
// 合成新的文件名
|
if (StringUtils.isEmpty(plateNum)) {
|
newFileName = newFileName + suffixName;
|
} else {
|
newFileName = plateNum + "_" + newFileName + suffixName;
|
}
|
|
// 文件上传
|
boolean flag = uploadFile(fileByte, filePath, newFileName);
|
|
// 判断文件上传是否成功,成功返回文件名
|
if (flag) {
|
return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(),
|
newFileName);
|
} else {
|
return new PageResponse<String>(RespCodeEnum.CODE_1111.getCode(),
|
"上传失败");
|
}
|
}
|
|
/**
|
* 文件上传
|
*
|
* @param file
|
* @return
|
* @throws IOException
|
*/
|
public PageResponse<String> upLoad(MultipartFile file, String bizTag)
|
throws IOException {
|
|
// 获取默认编码的字节数组
|
byte[] fileByte = file.getBytes();
|
|
// 获取文件的源文件名称
|
String oldFileName = file.getOriginalFilename();
|
|
// 获取文件保存路径
|
String filePath = fileService.getInoutFilePath(new Date());
|
|
// 获取新的ID
|
String newFileName = ContextUtil.getUUID();
|
|
// 文件后缀名
|
String suffixName = oldFileName.substring(oldFileName.lastIndexOf("."));
|
|
// 合成新的文件名
|
if (StringUtils.isEmpty(bizTag)) {
|
newFileName = newFileName + suffixName;
|
} else {
|
newFileName = bizTag + "_" + newFileName + suffixName;
|
}
|
// 文件上传
|
boolean flag = uploadFile(fileByte, filePath, newFileName);
|
|
// 判断文件上传是否成功,成功返回文件名
|
if (flag) {
|
return new PageResponse<String>(RespCodeEnum.CODE_0000.getCode(),
|
newFileName);
|
} else {
|
return new PageResponse<String>(RespCodeEnum.CODE_1111.getCode(),
|
"上传失败");
|
}
|
}
|
|
/**
|
* 文件流上传文件
|
*
|
* @param file
|
* @param filePath
|
* @param fileName
|
* @return
|
*/
|
private static boolean uploadFile(byte[] file, String filePath, String fileName) throws IOException {
|
// 默认文件上传成功
|
boolean flag = true;
|
// new一个文件对象实例
|
File targetFile = new File(filePath);
|
// 如果当前文件目录不存在就自动创建该文件或者目录
|
if (!targetFile.exists()) {
|
targetFile.mkdirs();
|
}
|
// 创建文件流
|
FileOutputStream fileOutputStream = new FileOutputStream(filePath + fileName);
|
try {
|
fileOutputStream.write(file);
|
} catch (FileNotFoundException e) {
|
flag = false;
|
} catch (IOException ioException) {
|
flag = false;
|
} finally {
|
fileOutputStream.flush();
|
fileOutputStream.close();
|
}
|
return flag;
|
}
|
|
public File getTempFilePath(String fileName, String companyId) {
|
String path = filesUtil.getTempPath(companyId) + "/" + fileName;
|
return new File(path);
|
}
|
|
public String getSnapFilePath(String fileName, String dateStr) {
|
return fileService.getSnapFilePath(dateStr) + "/" + fileName;
|
}
|
|
public String getFailImg() throws IOException {
|
|
String failImg = "static/images/img-fail.jpg";
|
|
ClassPathResource readFile = new ClassPathResource(failImg);
|
|
return readFile.getFile().getAbsolutePath();
|
}
|
|
/**
|
* 根据不同的类型获取文件路径
|
* @param filePathType
|
* @param time
|
* @return
|
*/
|
public String getPathByType(String filePathType, Date time) {
|
if("common".equals(filePathType)){
|
return fileService.getCommonFilePath(time);
|
}
|
return null;
|
}
|
}
|