拆分-图片上传接口
This commit is contained in:
+5
-3
@@ -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);
|
||||
|
||||
|
||||
+115
@@ -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)){
|
||||
|
||||
+1
-1
@@ -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>
|
||||
|
||||
|
||||
+54
@@ -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<SarFileSplitItems
|
||||
@Autowired
|
||||
private ILanguageSwitchService languageSwitchService;
|
||||
|
||||
@Autowired
|
||||
private IOSSFileService ossFileService;
|
||||
|
||||
|
||||
@Value(value = "${jero.path.upload}")
|
||||
private String uploadpath;
|
||||
|
||||
@Value(value="${jero.uploadType}")
|
||||
private String uploadType;
|
||||
|
||||
|
||||
@AutoLog(value = "分页查询")
|
||||
@ApiOperation(value = "分页查询")
|
||||
@@ -278,4 +291,45 @@ public class FileSplitItemsEOController extends JeroController<SarFileSplitItems
|
||||
List<Map<String, Object>> 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<OSSFile> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user