diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java index b39a9833c..bb2c8280b 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/IOSSFileService.java @@ -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 { void upload(MultipartFile multipartFile) throws IOException; @@ -19,6 +19,8 @@ public interface IOSSFileService extends IService { */ public OSSFile uploadLocal(MultipartFile mf, String bizPath,String state,String cut); + public OSSFile uploadLocalForSplit(MultipartFile mf, String bizPath,String state,String cut); + public List getFileInfos(String id); diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java index 2eaf09ae8..31b01c9ab 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/oss/service/impl/OSSFileServiceImpl.java @@ -38,6 +38,10 @@ public class OSSFileServiceImpl extends ServiceImpl 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 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 getFileInfos(String id) { if(com.jero.modules.system.util.StringUtils.isEmpty(id)){ diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/mapper/xml/SysDictMapper.xml b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/mapper/xml/SysDictMapper.xml index a371633b3..b288054fd 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/mapper/xml/SysDictMapper.xml +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/mapper/xml/SysDictMapper.xml @@ -5,7 +5,7 @@ diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/controller/FileSplitItemsEOController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/controller/FileSplitItemsEOController.java index e29f44a90..14277d806 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/controller/FileSplitItemsEOController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/split/controller/FileSplitItemsEOController.java @@ -5,8 +5,13 @@ import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.jero.common.api.vo.Result; import com.jero.common.aspect.annotation.AutoLog; +import com.jero.common.constant.CommonConstant; +import com.jero.common.constant.enums.CutEnum; import com.jero.common.system.base.controller.JeroController; +import com.jero.common.util.oConvertUtils; import com.jero.modules.lanswitch.service.ILanguageSwitchService; +import com.jero.modules.oss.entity.OSSFile; +import com.jero.modules.oss.service.IOSSFileService; import com.jero.modules.split.entity.SarFileSplitInfoEO; import com.jero.modules.split.entity.SarFileSplitItemsEO; import com.jero.modules.split.entity.SarFileSplitItemsValEO; @@ -23,6 +28,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -47,9 +53,16 @@ public class FileSplitItemsEOController extends JeroController> list = languageSwitchService.getForm(flag, cut); return Result.OK(list); } + + /** + * 文件上传统一方法 + * + * @param request + * @param response + * @return + */ + @ApiOperation(value="图片上传", notes="图片上传") + @PostMapping(value = "/upload") + @RequiresPermissions("split:sarFileSplitItems:page") + public Result upload(HttpServletRequest request, HttpServletResponse response, + @RequestParam(value = "state",required = false) String state, + @RequestParam(value = "cut",required = false) String cut) { + Result result = new Result<>(); + String bizPath = request.getParameter("biz"); + MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; + MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象 + if (oConvertUtils.isEmpty(bizPath)) { + if (CommonConstant.UPLOAD_TYPE_OSS.equals(uploadType)) { + //未指定目录,则用阿里云默认目录 upload + bizPath = "upload"; + } else { + bizPath = ""; + } + } + OSSFile oSSFile = ossFileService.uploadLocalForSplit(file, bizPath,state,cut); + if (oConvertUtils.isNotEmpty(oSSFile)) { + result.setResult(oSSFile); + result.setSuccess(true); + } else { + if("cut".equals(CutEnum.CN.getValue())){ + result.setMessage("上传失败!"); + }else{ + result.setMessage("fail to upload!"); + } + result.setSuccess(false); + } + return result; + } + }