拆分-图片上传接口

This commit is contained in:
liyawei
2022-04-21 14:28:25 +08:00
parent 42c9f87c69
commit 3040e45e33
4 changed files with 175 additions and 4 deletions
@@ -1,12 +1,12 @@
package com.jero.modules.oss.service;
import java.io.IOException;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jero.modules.oss.entity.OSSFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
public interface IOSSFileService extends IService<OSSFile> {
void upload(MultipartFile multipartFile) throws IOException;
@@ -19,6 +19,8 @@ public interface IOSSFileService extends IService<OSSFile> {
*/
public OSSFile uploadLocal(MultipartFile mf, String bizPath,String state,String cut);
public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath,String state,String cut);
public List<OSSFile> getFileInfos(String id);
@@ -38,6 +38,10 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
@Value(value="${jero.fileSuffixLimits}")
private String[] fileSuffixLimits;
// 文档拆分条款图片访问路径
@Value(value = "${jero.path.img}")
private String imgpath;
@Override
public void upload(MultipartFile multipartFile) throws IOException {
String fileName = multipartFile.getOriginalFilename();
@@ -160,6 +164,117 @@ public class OSSFileServiceImpl extends ServiceImpl<OSSFileMapper, OSSFile> impl
return oSSFile;
}
/**
* 本地文件上传-文档拆分专用
*
* @param mf 文件
* @param bizPath 自定义路径
* @return
*/
@Override
public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath,String state,String cut) {
//处理文件大小,大于100M抛出异常
long fileSize = mf.getSize() / 1024 / 1024;
String originalFilename = mf.getOriginalFilename();
if (fileSize >= 100) {
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!");
}
}
OSSFile oSSFile = new OSSFile();
try {
String ctxPath = uploadpath;
String fileName = null;
String fileType = null;
File file = new File(ctxPath + File.separator + bizPath + File.separator);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
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("."));
String fileTypeStr = ".doc,.DOC,.docx,.DOCX,.xls, .XLS,.xlsx,.XLSX,.pdf,.PDF";
//判断文件类型
if("1".equals(state)){
//固定文件
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[] 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/静态图片类型的文件。请重新选择文件!");
}
}
}
String savePath = file.getPath() + File.separator + fileName;
File savefile = new File(savePath);
FileCopyUtils.copy(mf.getBytes(), savefile);
String dbpath = null;
if (oConvertUtils.isNotEmpty(bizPath)) {
dbpath = bizPath + File.separator + fileName;
} else {
dbpath = fileName;
}
if (dbpath.contains("\\")) {
dbpath = dbpath.replace("\\", "/");
}
LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
//存入文件表
oSSFile.setFileName(orgName);
oSSFile.setId(UuidUtils.getUUID());
oSSFile.setUrl(savePath);
oSSFile.setCreateBy(loginUser.getUsername());
oSSFile.setCreateTime(new Date());
this.save(oSSFile);
// 文件路径做特殊处理
String imgUrl = imgpath + savePath.substring(savePath.lastIndexOf(File.separator)+1);
oSSFile.setUrl(imgUrl);
return oSSFile;
} catch (IOException e) {
log.error(e.getMessage(), e);
}
return oSSFile;
}
@Override
public List<OSSFile> getFileInfos(String id) {
if(com.jero.modules.system.util.StringUtils.isEmpty(id)){
@@ -5,7 +5,7 @@
<!-- 通过字典code获取字典数据 -->
<select id="queryDictItemsByCode" parameterType="String" resultType="com.jero.common.system.vo.DictModel">
select s.item_value as "value",s.item_text as "text" from sys_dict_item s
where dict_id = (select id from sys_dict where dict_code = #{code})
where dict_id = (select id from sys_dict where dict_code = #{code}) and del_flag = '0'
order by s.sort_order asc
</select>