Commit 23ca6210 authored by zhaji's avatar zhaji

"feature-zhaJi:优化文件上传的代码判断逻辑"

parent caf18b91
......@@ -245,6 +245,10 @@ public interface CommonConstants {
public static final String SPOT = ".";
int FIFTY_INT = 50;
int BYTE = 1024;
/**
* 员工初始序列
* @Author pwang
......
......@@ -471,4 +471,28 @@ public class Common {
}
return obj;
}
/**
* 商险附件允许上传的文件类型
*
* @Author pwang
* @Date 2020-01-07 15:52
* @param null
* @return
**/
private static final String insuranceSuffixList = "pdf|png|jpg|word|excel|rar|zip";
/**
* 判断是否为允许的上传文件类型,true表示允许
*/
public static boolean checkInsuranceFile(String fileName) {
// 获取文件后缀
String suffix = getSuffix(fileName);
if (null != suffix) {
if (insuranceSuffixList.contains(suffix.trim().toLowerCase())) {
return true;
}
}
return false;
}
}
......@@ -39,8 +39,6 @@ public class TInsuranceEnclosureController {
@Resource
TInsuranceEnclosureService tInsuranceEnclosureService;
private final OSSUtil ossUtil;
/**
* 商险文件上传
*
......
......@@ -13,14 +13,17 @@ import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceEnclosure;
import com.yifu.cloud.plus.v1.yifu.insurances.mapper.TInsuranceEnclosureMapper;
import com.yifu.cloud.plus.v1.yifu.insurances.service.TInsuranceEnclosureService;
import com.yifu.cloud.plus.v1.yifu.insurances.util.ValidityUtil;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.InsuranceEnclosureParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Objects;
/**
* @author Administrator
......@@ -50,10 +53,19 @@ public class TInsuranceEnclosureServiceImpl extends ServiceImpl<TInsuranceEnclos
*/
@Override
public R uploadInsuranceEnclosure(MultipartFile file, String filePath, String remark) throws IOException {
String enclosureName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf("."));
if(Common.isEmpty(file)){
return R.failed("商险附件为空");
}
if(!Common.checkInsuranceFile(file.getOriginalFilename())){
return R.failed("非法的文件类型");
}
if (file.getSize() > (CommonConstants.FIFTY_INT*CommonConstants.BYTE*CommonConstants.BYTE)){
return R.failed("文件超出上传上限");
}
String enclosureName = Objects.requireNonNull(file.getOriginalFilename()).substring(0,file.getOriginalFilename().lastIndexOf("."));
String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
//filePath不传默认存储空间的根目录
//jpg,jpeg,png,bmp
//支持的附件格式
String key = "";
if (!Common.isNotNull(filePath)) {
key = fileName;
......@@ -61,13 +73,17 @@ public class TInsuranceEnclosureServiceImpl extends ServiceImpl<TInsuranceEnclos
key = filePath + "/" + fileName;
}
boolean flag = ossUtil.uploadFileByStream(file.getInputStream(), key, null);
FileVo fileVo;
TInsuranceEnclosure insuranceEnclosure = new TInsuranceEnclosure();
URL url = null;
if (flag) {
TInsuranceEnclosure insuranceEnclosure = new TInsuranceEnclosure();
log.info("文件:" + fileName + "上传至存储空间" + ossUtil.getBucketName() + "成功!");
insuranceEnclosure.setDeleteFlag(CommonConstants.ZERO_INT);
insuranceEnclosure.setRemark(remark);
if(StringUtils.isNotBlank(remark)){
if(!ValidityUtil.validate50(remark)){
return R.failed("备注字段不能超过50");
}else{
insuranceEnclosure.setRemark(remark);
}
}
insuranceEnclosure.setEnclosureName(enclosureName);
insuranceEnclosure.setEnclosureAddress(key);
try {
......@@ -77,9 +93,7 @@ public class TInsuranceEnclosureServiceImpl extends ServiceImpl<TInsuranceEnclos
ossUtil.deleteObject(null, key);
return R.failed("failed:" + e.getMessage());
}
url = ossUtil.getObjectUrl(null, insuranceEnclosure.getEnclosureAddress());
fileVo = new FileVo(insuranceEnclosure.getId().toString(), insuranceEnclosure.getEnclosureName(), url.toString());
return R.ok(fileVo);
return R.ok("商险附件上传成功");
} else {
return R.failed("failed:上传至存储空间失败");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment