文件上传

This commit is contained in:
zhn
2022-06-10 15:20:12 +08:00
parent d1d088a74c
commit 7f58ded5d4
@@ -32,407 +32,403 @@ import java.util.List;
@Service("ossFileService") @Service("ossFileService")
public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> implements IOSSFileService { public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> implements IOSSFileService {
@Value(value = "${jero.path.upload}") @Value(value = "${jero.path.upload}")
private String uploadpath; private String uploadpath;
@Value(value = "${jero.path.uploadCos}") @Value(value = "${jero.path.uploadCos}")
private String uploadCospath; private String uploadCospath;
/** /**
* 文件后缀黑名单 * 文件后缀黑名单
*/ */
@Value(value="${jero.fileSuffixLimits}") @Value(value = "${jero.fileSuffixLimits}")
private String[] fileSuffixLimits; private String[] fileSuffixLimits;
// 文档拆分条款图片访问路径 // 文档拆分条款图片访问路径
@Value(value = "${jero.splitUrl}") @Value(value = "${jero.splitUrl}")
private String splitUrl; private String splitUrl;
@Override @Override
public void upload(MultipartFile multipartFile) throws IOException { public void upload(MultipartFile multipartFile) throws IOException {
String fileName = multipartFile.getOriginalFilename(); String fileName = multipartFile.getOriginalFilename();
fileName = CommonUtils.getFileName(fileName); fileName = CommonUtils.getFileName(fileName);
OSSFile ossFile = new OSSFile(); OSSFile ossFile = new OSSFile();
ossFile.setFileName(fileName); ossFile.setFileName(fileName);
String url = OssBootUtil.upload(multipartFile,"upload/test"); String url = OssBootUtil.upload(multipartFile, "upload/test");
//update-begin--Author:scott Date:20201227 forJT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败------------------- //update-begin--Author:scott Date:20201227 forJT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败-------------------
// 返回阿里云原生域名前缀URL // 返回阿里云原生域名前缀URL
ossFile.setUrl(OssBootUtil.getOriginalUrl(url)); ossFile.setUrl(OssBootUtil.getOriginalUrl(url));
//update-end--Author:scott Date:20201227 forJT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败------------------- //update-end--Author:scott Date:20201227 forJT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败-------------------
this.save(ossFile); this.save(ossFile);
} }
/**
* 本地文件上传
*
* @param mf 文件
* @param bizPath 自定义路径
* @return
*/
@Override
public OSSFile uploadLocal(MultipartFile mf, String bizPath,String state,String cut) {
//处理文件大小,大于100M抛出异常 /**
long fileSize = mf.getSize() / 1024 / 1024; * 本地文件上传
String originalFilename = mf.getOriginalFilename(); *
if (fileSize >= 100) { * @param mf 文件
if(cut.equals(CutEnum.CN.getValue())){ * @param bizPath 自定义路径
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!"); * @return
}else{ */
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!"); @Override
} public OSSFile uploadLocal(MultipartFile mf, String bizPath, String state, String cut) {
}
OSSFile oSSFile = new OSSFile(); //处理文件大小,大于100M抛出异常
try { long fileSize = mf.getSize() / 1024 / 1024;
String ctxPath = uploadpath; String originalFilename = mf.getOriginalFilename();
String fileName = null; if (fileSize >= 100) {
String fileType = null; if (cut.equals(CutEnum.CN.getValue())) {
File file = new File(ctxPath + File.separator + bizPath + File.separator); throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
if (!file.exists()) { } else {
file.mkdirs();// 创建文件根目录 throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
} }
String orgName = mf.getOriginalFilename();// 获取文件名 }
orgName = CommonUtils.getFileName(orgName);
if (orgName.indexOf(".") != -1) {
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
} else {
fileName = orgName + "_" + System.currentTimeMillis();
}
fileType = orgName.substring(orgName.lastIndexOf(".")); OSSFile oSSFile = new OSSFile();
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; try {
//判断文件类型 String ctxPath = uploadpath;
if("1".equals(state)){ String fileName = null;
//固定文件 String fileType = null;
if(!fileTypeStr.contains(fileType)){ File file = new File(ctxPath + File.separator + bizPath + File.separator);
if(cut.equals(CutEnum.CN.getValue())){ if (!file.exists()) {
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!"); file.mkdirs();// 创建文件根目录
}else{ }
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again"); String orgName = mf.getOriginalFilename();// 获取文件名
} orgName = CommonUtils.getFileName(orgName);
} if (orgName.indexOf(".") != -1) {
}else{ fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) { } else {
if(cut.equals(CutEnum.CN.getValue())){ fileName = orgName + "_" + System.currentTimeMillis();
throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!"); }
}else{
throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again");
}
}
}
if (StringUtils.isNotEmpty(fileType)) { fileType = orgName.substring(orgName.lastIndexOf("."));
String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX", String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG", //判断文件类型
".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP", if ("1".equals(state)) {
".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"}; //固定文件
boolean flag = true; if (!fileTypeStr.contains(fileType)) {
for (String fileTypeTemp : fileTypeArr) { if (cut.equals(CutEnum.CN.getValue())) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) { throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
flag = false; } else {
} throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
} }
if (flag) { }
if(cut.equals(CutEnum.CN.getValue())){ } else {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
}else{ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
} } else {
} throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again");
} }
}
}
String savePath = file.getPath() + File.separator + fileName; if (StringUtils.isNotEmpty(fileType)) {
File savefile = new File(savePath); String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
FileCopyUtils.copy(mf.getBytes(), savefile); ".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
String dbpath = null; ".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
if (oConvertUtils.isNotEmpty(bizPath)) { ".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
dbpath = bizPath + File.separator + fileName; boolean flag = true;
} else { for (String fileTypeTemp : fileTypeArr) {
dbpath = fileName; if (fileTypeTemp.equalsIgnoreCase(fileType)) {
} flag = false;
if (dbpath.contains("\\")) { }
dbpath = dbpath.replace("\\", "/"); }
} if (flag) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}
}
}
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); String savePath = file.getPath() + File.separator + fileName;
//存入文件表 File savefile = new File(savePath);
oSSFile.setFileName(orgName); FileCopyUtils.copy(mf.getBytes(), savefile);
oSSFile.setId(UuidUtils.getUUID()); String dbpath = null;
oSSFile.setUrl(savePath); if (oConvertUtils.isNotEmpty(bizPath)) {
oSSFile.setCreateBy(loginUser.getUsername()); dbpath = bizPath + File.separator + fileName;
oSSFile.setCreateTime(new Date()); } else {
this.save(oSSFile); dbpath = fileName;
oSSFile.setUrl(null); }
return oSSFile; if (dbpath.contains("\\")) {
} catch (IOException e) { dbpath = dbpath.replace("\\", "/");
log.error(e.getMessage(), e); }
}
return oSSFile;
}
/** LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
* 本地文件上传 cos //存入文件表
* oSSFile.setFileName(orgName);
* @param mf 文件 oSSFile.setId(UuidUtils.getUUID());
* @param bizPath 自定义路径 oSSFile.setUrl(savePath);
* @return oSSFile.setCreateBy(loginUser.getUsername());
*/ oSSFile.setCreateTime(new Date());
@Override this.save(oSSFile);
public OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath,String state,String cut) { oSSFile.setUrl(null);
return oSSFile;
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return oSSFile;
}
//处理文件大小,大于100M抛出异常 /**
long fileSize = mf.getSize() / 1024 / 1024; * 本地文件上传 cos
String originalFilename = mf.getOriginalFilename(); *
if (fileSize >= 100) { * @param mf 文件
if(cut.equals(CutEnum.CN.getValue())){ * @param bizPath 自定义路径
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!"); * @return
}else{ */
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!"); @Override
} public OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath, String state, String cut) {
}
OSSFile oSSFile = new OSSFile(); //处理文件大小,大于100M抛出异常
try { long fileSize = mf.getSize() / 1024 / 1024;
String ctxPath = uploadCospath; String originalFilename = mf.getOriginalFilename();
String fileName = null; if (fileSize >= 100) {
String fileType = null; if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
} else {
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
}
}
String orgName = mf.getOriginalFilename();// 获取文件名 OSSFile oSSFile = new OSSFile();
orgName = CommonUtils.getFileName(orgName); String ctxPath = uploadCospath;
if (orgName.indexOf(".") != -1) { String fileName = null;
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf(".")); String fileType = null;
} else {
fileName = orgName + "_" + System.currentTimeMillis();
}
String filePath = ctxPath + "/" + fileName; String orgName = mf.getOriginalFilename();// 获取文件名
fileType = orgName.substring(orgName.lastIndexOf(".")); orgName = CommonUtils.getFileName(orgName);
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; if (orgName.indexOf(".") != -1) {
//判断文件类型 fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
if("1".equals(state)){ } else {
//固定文件 fileName = orgName + "_" + System.currentTimeMillis();
if(!fileTypeStr.contains(fileType)){ }
if(cut.equals(CutEnum.CN.getValue())){
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
}else{
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
}
}
}else{
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if(cut.equals(CutEnum.CN.getValue())){
throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!");
}else{
throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again");
}
}
}
if (StringUtils.isNotEmpty(fileType)) { String filePath = ctxPath + "/" + fileName;
String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX", fileType = orgName.substring(orgName.lastIndexOf("."));
".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG", String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP", //判断文件类型
".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"}; if ("1".equals(state)) {
boolean flag = true; //固定文件
for (String fileTypeTemp : fileTypeArr) { if (!fileTypeStr.contains(fileType)) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) { if (cut.equals(CutEnum.CN.getValue())) {
flag = false; throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
} } else {
} throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
if (flag) { }
if(cut.equals(CutEnum.CN.getValue())){ }
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); } else {
}else{ if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); if (cut.equals(CutEnum.CN.getValue())) {
} throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
} } else {
} throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again");
}
}
}
// 将文件上传至腾讯云 cos if (StringUtils.isNotEmpty(fileType)) {
CosBootUtil.upload(mf, filePath); String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true;
for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) {
flag = false;
}
}
if (flag) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}
}
}
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); // 将文件上传至腾讯云 cos
//存入文件表 CosBootUtil.upload(mf, filePath);
oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(filePath);
oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date());
this.save(oSSFile);
oSSFile.setUrl(null);
return oSSFile;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return oSSFile;
}
/** LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
* 本地文件上传-文档拆分专用 //存入文件表
* oSSFile.setFileName(orgName);
* @param mf 文件 oSSFile.setId(UuidUtils.getUUID());
* @param bizPath 自定义路径 oSSFile.setUrl(filePath);
* @return oSSFile.setCreateBy(loginUser.getUsername());
*/ oSSFile.setCreateTime(new Date());
@Override this.save(oSSFile);
public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath,String state,String cut) { oSSFile.setUrl(null);
return oSSFile;
}
//处理文件大小,大于100M抛出异常 /**
long fileSize = mf.getSize() / 1024 / 1024; * 本地文件上传-文档拆分专用
String originalFilename = mf.getOriginalFilename(); *
if (fileSize >= 100) { * @param mf 文件
if(cut.equals(CutEnum.CN.getValue())){ * @param bizPath 自定义路径
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!"); * @return
}else{ */
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!"); @Override
} public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath, String state, String cut) {
}
OSSFile oSSFile = new OSSFile(); //处理文件大小,大于100M抛出异常
try { long fileSize = mf.getSize() / 1024 / 1024;
String ctxPath = uploadCospath; String originalFilename = mf.getOriginalFilename();
String fileName = null; if (fileSize >= 100) {
String fileType = null; if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
} else {
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
}
}
String orgName = mf.getOriginalFilename();// 获取文件名 OSSFile oSSFile = new OSSFile();
orgName = CommonUtils.getFileName(orgName); try {
if (orgName.indexOf(".") != -1) { String ctxPath = uploadCospath;
fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf(".")); String fileName = null;
} else { String fileType = null;
fileName = orgName + "_" + System.currentTimeMillis();
}
String filePath = ctxPath + "/" + fileName; String orgName = mf.getOriginalFilename();// 获取文件名
fileType = orgName.substring(orgName.lastIndexOf(".")); orgName = CommonUtils.getFileName(orgName);
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; if (orgName.indexOf(".") != -1) {
//判断文件类型 fileName = orgName.substring(0, orgName.lastIndexOf(".")) + "_" + System.currentTimeMillis() + orgName.substring(orgName.lastIndexOf("."));
if("1".equals(state)){ } else {
//固定文件 fileName = orgName + "_" + System.currentTimeMillis();
if(!fileTypeStr.contains(fileType)){ }
if(cut.equals(CutEnum.CN.getValue())){
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
}else{
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
}
}
}else{
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if(cut.equals(CutEnum.CN.getValue())){
throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!");
}else{
throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again");
}
}
}
if (StringUtils.isNotEmpty(fileType)) { String filePath = ctxPath + "/" + fileName;
String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX", fileType = orgName.substring(orgName.lastIndexOf("."));
".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG", String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP", //判断文件类型
".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"}; if ("1".equals(state)) {
boolean flag = true; //固定文件
for (String fileTypeTemp : fileTypeArr) { if (!fileTypeStr.contains(fileType)) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) { if (cut.equals(CutEnum.CN.getValue())) {
flag = false; throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
} } else {
} throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again");
if (flag) { }
if(cut.equals(CutEnum.CN.getValue())){ }
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); } else {
}else{ if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); if (cut.equals(CutEnum.CN.getValue())) {
} throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
} } else {
} throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again");
}
}
}
// 将文件上传至腾讯云 cos if (StringUtils.isNotEmpty(fileType)) {
CosBootUtil.upload(mf, filePath); String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true;
for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) {
flag = false;
}
}
if (flag) {
if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}
}
}
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal(); // 将文件上传至腾讯云 cos
//存入文件表 CosBootUtil.upload(mf, filePath);
oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(filePath);
oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date());
this.save(oSSFile);
// 文件路径做特殊处理
BASE64Encoder base64Encoder = new BASE64Encoder();
String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
oSSFile.setUrl(imgUrl);
return oSSFile;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return oSSFile;
}
@Override LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
public List<OSSFile> getFileInfos(String id) { //存入文件表
if(com.jero.modules.system.util.StringUtils.isEmpty(id)){ oSSFile.setFileName(orgName);
return new ArrayList<>(); oSSFile.setId(UuidUtils.getUUID());
} oSSFile.setUrl(filePath);
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>(); oSSFile.setCreateBy(loginUser.getUsername());
wrapper.in(OSSFile::getId,id.split(",")); oSSFile.setCreateTime(new Date());
List<OSSFile> list = this.list(wrapper); this.save(oSSFile);
if(list.size() == 0){ // 文件路径做特殊处理
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>(); BASE64Encoder base64Encoder = new BASE64Encoder();
lambdaQueryWrapper.in(OSSFile::getConnectId,id.split(",")); String encode = base64Encoder.encode(filePath.getBytes(StandardCharsets.UTF_8));
list = this.list(lambdaQueryWrapper); String imgUrl = splitUrl + "/jero-boot/sys/split/file/getImage?fileUrl=" + encode;
} oSSFile.setUrl(imgUrl);
return list; return oSSFile;
} } catch (Exception e) {
@Override log.error(e.getMessage(), e);
public List<OSSFile> getFileInfoAll(String id) { }
if (com.jero.modules.system.util.StringUtils.isEmpty(id)) { return oSSFile;
return new ArrayList<>(); }
}
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
wrapper.in(OSSFile::getId, id.split(","));
List<OSSFile> list = this.list(wrapper);
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(OSSFile::getConnectId, id.split(","));
list.addAll(this.list(lambdaQueryWrapper));
return list;
}
@Override @Override
public List<OSSFile> getFileInfosByConnectId(String connectId) { public List<OSSFile> getFileInfos(String id) {
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>(); if (com.jero.modules.system.util.StringUtils.isEmpty(id)) {
if(connectId.contains(",")){ return new ArrayList<>();
wrapper.in(OSSFile::getConnectId, Arrays.asList(connectId.split(","))); }
}else{ LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(OSSFile::getConnectId,connectId); wrapper.in(OSSFile::getId, id.split(","));
} List<OSSFile> list = this.list(wrapper);
if (list.size() == 0) {
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(OSSFile::getConnectId, id.split(","));
list = this.list(lambdaQueryWrapper);
}
return list;
}
List<OSSFile> list = this.list(wrapper); @Override
return list; public List<OSSFile> getFileInfoAll(String id) {
} if (com.jero.modules.system.util.StringUtils.isEmpty(id)) {
return new ArrayList<>();
}
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
wrapper.in(OSSFile::getId, id.split(","));
List<OSSFile> list = this.list(wrapper);
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(OSSFile::getConnectId, id.split(","));
list.addAll(this.list(lambdaQueryWrapper));
return list;
}
@Override @Override
public boolean delete(OSSFile ossFile) { public List<OSSFile> getFileInfosByConnectId(String connectId) {
try { LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
this.removeById(ossFile.getId()); if (connectId.contains(",")) {
OssBootUtil.deleteUrl(ossFile.getUrl()); wrapper.in(OSSFile::getConnectId, Arrays.asList(connectId.split(",")));
} } else {
catch (Exception ex) { wrapper.eq(OSSFile::getConnectId, connectId);
return false; }
}
return true;
}
@Override List<OSSFile> list = this.list(wrapper);
public void updateFileInfo(List<OSSFile> ossFileList) { return list;
this.updateBatchById(ossFileList); }
}
@Override @Override
public void deleteByConnectIdList(List<String> connectIdList) { public boolean delete(OSSFile ossFile) {
if(connectIdList.size() != 0){ try {
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>(); this.removeById(ossFile.getId());
lambdaQueryWrapper.in(OSSFile::getConnectId,connectIdList); OssBootUtil.deleteUrl(ossFile.getUrl());
this.remove(lambdaQueryWrapper); } catch (Exception ex) {
} return false;
} }
return true;
}
@Override
public void updateFileInfo(List<OSSFile> ossFileList) {
this.updateBatchById(ossFileList);
}
@Override
public void deleteByConnectIdList(List<String> connectIdList) {
if (connectIdList.size() != 0) {
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(OSSFile::getConnectId, connectIdList);
this.remove(lambdaQueryWrapper);
}
}
} }