文件上传

This commit is contained in:
zhn
2022-06-10 15:20:12 +08:00
parent d1d088a74c
commit 7f58ded5d4
@@ -40,7 +40,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
/** /**
* 文件后缀黑名单 * 文件后缀黑名单
*/ */
@Value(value="${jero.fileSuffixLimits}") @Value(value = "${jero.fileSuffixLimits}")
private String[] fileSuffixLimits; private String[] fileSuffixLimits;
// 文档拆分条款图片访问路径 // 文档拆分条款图片访问路径
@@ -53,13 +53,14 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
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 for:JT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败------------------- //update-begin--Author:scott Date:20201227 for:JT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败-------------------
// 返回阿里云原生域名前缀URL // 返回阿里云原生域名前缀URL
ossFile.setUrl(OssBootUtil.getOriginalUrl(url)); ossFile.setUrl(OssBootUtil.getOriginalUrl(url));
//update-end--Author:scott Date:20201227 for:JT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败------------------- //update-end--Author:scott Date:20201227 for:JT-361【文件预览】阿里云原生域名可以文件预览,自己映射域名kkfileview提示文件下载失败-------------------
this.save(ossFile); this.save(ossFile);
} }
/** /**
* 本地文件上传 * 本地文件上传
* *
@@ -68,15 +69,15 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
* @return * @return
*/ */
@Override @Override
public OSSFile uploadLocal(MultipartFile mf, String bizPath,String state,String cut) { public OSSFile uploadLocal(MultipartFile mf, String bizPath, String state, String cut) {
//处理文件大小,大于100M抛出异常 //处理文件大小,大于100M抛出异常
long fileSize = mf.getSize() / 1024 / 1024; long fileSize = mf.getSize() / 1024 / 1024;
String originalFilename = mf.getOriginalFilename(); String originalFilename = mf.getOriginalFilename();
if (fileSize >= 100) { if (fileSize >= 100) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!"); throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
}else{ } else {
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!"); throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
} }
} }
@@ -101,30 +102,30 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
fileType = orgName.substring(orgName.lastIndexOf(".")); fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型 //判断文件类型
if("1".equals(state)){ if ("1".equals(state)) {
//固定文件 //固定文件
if(!fileTypeStr.contains(fileType)){ if (!fileTypeStr.contains(fileType)) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!"); throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!"); throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!");
} }
} }
}else{ } else {
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) { if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!"); throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again!"); throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again!");
} }
} }
} }
if (StringUtils.isNotEmpty(fileType)) { if (StringUtils.isNotEmpty(fileType)) {
String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX", String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG", ".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP", ".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"}; ".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true; boolean flag = true;
for (String fileTypeTemp : fileTypeArr) { for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) { if (fileTypeTemp.equalsIgnoreCase(fileType)) {
@@ -132,9 +133,9 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
} }
} }
if (flag) { if (flag) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} }
} }
@@ -177,21 +178,20 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
* @return * @return
*/ */
@Override @Override
public OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath,String state,String cut) { public OSSFile uploadLocalOfCos(MultipartFile mf, String bizPath, String state, String cut) {
//处理文件大小,大于100M抛出异常 //处理文件大小,大于100M抛出异常
long fileSize = mf.getSize() / 1024 / 1024; long fileSize = mf.getSize() / 1024 / 1024;
String originalFilename = mf.getOriginalFilename(); String originalFilename = mf.getOriginalFilename();
if (fileSize >= 100) { if (fileSize >= 100) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!"); throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
}else{ } else {
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!"); throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
} }
} }
OSSFile oSSFile = new OSSFile(); OSSFile oSSFile = new OSSFile();
try {
String ctxPath = uploadCospath; String ctxPath = uploadCospath;
String fileName = null; String fileName = null;
String fileType = null; String fileType = null;
@@ -208,30 +208,30 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
fileType = orgName.substring(orgName.lastIndexOf(".")); fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型 //判断文件类型
if("1".equals(state)){ if ("1".equals(state)) {
//固定文件 //固定文件
if(!fileTypeStr.contains(fileType)){ if (!fileTypeStr.contains(fileType)) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!"); throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!"); throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!");
} }
} }
}else{ } else {
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) { if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!"); throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again!"); throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again!");
} }
} }
} }
if (StringUtils.isNotEmpty(fileType)) { if (StringUtils.isNotEmpty(fileType)) {
String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX", String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG", ".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP", ".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"}; ".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true; boolean flag = true;
for (String fileTypeTemp : fileTypeArr) { for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) { if (fileTypeTemp.equalsIgnoreCase(fileType)) {
@@ -239,9 +239,9 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
} }
} }
if (flag) { if (flag) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} }
} }
@@ -260,10 +260,6 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
this.save(oSSFile); this.save(oSSFile);
oSSFile.setUrl(null); oSSFile.setUrl(null);
return oSSFile; return oSSFile;
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return oSSFile;
} }
/** /**
@@ -274,15 +270,15 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
* @return * @return
*/ */
@Override @Override
public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath,String state,String cut) { public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath, String state, String cut) {
//处理文件大小,大于100M抛出异常 //处理文件大小,大于100M抛出异常
long fileSize = mf.getSize() / 1024 / 1024; long fileSize = mf.getSize() / 1024 / 1024;
String originalFilename = mf.getOriginalFilename(); String originalFilename = mf.getOriginalFilename();
if (fileSize >= 100) { if (fileSize >= 100) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!"); throw new JeroBootException(originalFilename + "文件大小超出100MB, 请压缩或降低文件质量!");
}else{ } else {
throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!"); throw new JeroBootException(originalFilename + "File size out 100MB, Please compress or reduce file quality!");
} }
} }
@@ -305,30 +301,30 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
fileType = orgName.substring(orgName.lastIndexOf(".")); fileType = orgName.substring(orgName.lastIndexOf("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF"; String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型 //判断文件类型
if("1".equals(state)){ if ("1".equals(state)) {
//固定文件 //固定文件
if(!fileTypeStr.contains(fileType)){ if (!fileTypeStr.contains(fileType)) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!"); throw new JeroBootException("只能够上传pdf,word,excel类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!"); throw new JeroBootException("Only upload pdf,word,excel type of file Please select the file again!");
} }
} }
}else{ } else {
if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) { if (CommonUtils.limitFileSuffix(orgName, fileSuffixLimits)) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("不能上传"+StringUtils.join(fileSuffixLimits, ",")+"类型的文件。请重新选择文件!"); throw new JeroBootException("不能上传" + StringUtils.join(fileSuffixLimits, ",") + "类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Can't upload"+StringUtils.join(fileSuffixLimits, ",")+"type of file Please select the file again!"); throw new JeroBootException("Can't upload" + StringUtils.join(fileSuffixLimits, ",") + "type of file Please select the file again!");
} }
} }
} }
if (StringUtils.isNotEmpty(fileType)) { if (StringUtils.isNotEmpty(fileType)) {
String[] fileTypeArr = {".doc",".DOC",".txt",".TXT", ".docx",".DOCX", String[] fileTypeArr = {".doc", ".DOC", ".txt", ".TXT", ".docx", ".DOCX",
".xls", ".XLS",".xlsx",".XLSX", ".pdf",".PDF", ".png",".PNG", ".xls", ".XLS", ".xlsx", ".XLSX", ".pdf", ".PDF", ".png", ".PNG",
".jfif",".JFIF", ".pjpeg",".PJPEG", ".jpeg",".JPEG", ".pjp",".PJP", ".jfif", ".JFIF", ".pjpeg", ".PJPEG", ".jpeg", ".JPEG", ".pjp", ".PJP",
".jpg",".JPG", ".swf",".SWF", ".bmp",".BMP",".rar",".RAR",".zip",".ZIP",".ppt",".PPT",".pptx",".PPTX",".csv",".CSV"}; ".jpg", ".JPG", ".swf", ".SWF", ".bmp", ".BMP", ".rar", ".RAR", ".zip", ".ZIP", ".ppt", ".PPT", ".pptx", ".PPTX", ".csv", ".CSV"};
boolean flag = true; boolean flag = true;
for (String fileTypeTemp : fileTypeArr) { for (String fileTypeTemp : fileTypeArr) {
if (fileTypeTemp.equalsIgnoreCase(fileType)) { if (fileTypeTemp.equalsIgnoreCase(fileType)) {
@@ -336,9 +332,9 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
} }
} }
if (flag) { if (flag) {
if(cut.equals(CutEnum.CN.getValue())){ if (cut.equals(CutEnum.CN.getValue())) {
throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("只能够上传pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
}else{ } else {
throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!"); throw new JeroBootException("Only upload pdf/word/excel/csv/ppt/txt/zip/rar/静态图片类型的文件。请重新选择文件!");
} }
} }
@@ -369,19 +365,20 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
@Override @Override
public List<OSSFile> getFileInfos(String id) { public List<OSSFile> getFileInfos(String id) {
if(com.jero.modules.system.util.StringUtils.isEmpty(id)){ if (com.jero.modules.system.util.StringUtils.isEmpty(id)) {
return new ArrayList<>(); return new ArrayList<>();
} }
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
wrapper.in(OSSFile::getId,id.split(",")); wrapper.in(OSSFile::getId, id.split(","));
List<OSSFile> list = this.list(wrapper); List<OSSFile> list = this.list(wrapper);
if(list.size() == 0){ if (list.size() == 0) {
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(OSSFile::getConnectId,id.split(",")); lambdaQueryWrapper.in(OSSFile::getConnectId, id.split(","));
list = this.list(lambdaQueryWrapper); list = this.list(lambdaQueryWrapper);
} }
return list; return list;
} }
@Override @Override
public List<OSSFile> getFileInfoAll(String id) { public List<OSSFile> getFileInfoAll(String id) {
if (com.jero.modules.system.util.StringUtils.isEmpty(id)) { if (com.jero.modules.system.util.StringUtils.isEmpty(id)) {
@@ -399,10 +396,10 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
@Override @Override
public List<OSSFile> getFileInfosByConnectId(String connectId) { public List<OSSFile> getFileInfosByConnectId(String connectId) {
LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<OSSFile> wrapper = new LambdaQueryWrapper<>();
if(connectId.contains(",")){ if (connectId.contains(",")) {
wrapper.in(OSSFile::getConnectId, Arrays.asList(connectId.split(","))); wrapper.in(OSSFile::getConnectId, Arrays.asList(connectId.split(",")));
}else{ } else {
wrapper.eq(OSSFile::getConnectId,connectId); wrapper.eq(OSSFile::getConnectId, connectId);
} }
List<OSSFile> list = this.list(wrapper); List<OSSFile> list = this.list(wrapper);
@@ -414,8 +411,7 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
try { try {
this.removeById(ossFile.getId()); this.removeById(ossFile.getId());
OssBootUtil.deleteUrl(ossFile.getUrl()); OssBootUtil.deleteUrl(ossFile.getUrl());
} } catch (Exception ex) {
catch (Exception ex) {
return false; return false;
} }
return true; return true;
@@ -428,9 +424,9 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
@Override @Override
public void deleteByConnectIdList(List<String> connectIdList) { public void deleteByConnectIdList(List<String> connectIdList) {
if(connectIdList.size() != 0){ if (connectIdList.size() != 0) {
LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<OSSFile> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.in(OSSFile::getConnectId,connectIdList); lambdaQueryWrapper.in(OSSFile::getConnectId, connectIdList);
this.remove(lambdaQueryWrapper); this.remove(lambdaQueryWrapper);
} }
} }