Browse Source

1.上传客户logo,增加文件类型和文件体积校验,只能是png类型文件,不超过1m

dev_shibei_match
wxz 5 years ago
parent
commit
f37df203c3
  1. 4
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java
  2. 18
      epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java

4
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/exception/EpmetErrorCode.java

@ -61,7 +61,9 @@ public enum EpmetErrorCode {
OPER_ADD_CUSTOMER_ROOT_AGENCY_EXISTS(8703, "添加客户根级组织失败,根级组织已存在"), OPER_ADD_CUSTOMER_ROOT_AGENCY_EXISTS(8703, "添加客户根级组织失败,根级组织已存在"),
OPER_CUSTOMER_EXISTS(8704, "客户已存在"), OPER_CUSTOMER_EXISTS(8704, "客户已存在"),
OPER_ADD_CUSTOMER_ERROR(8705, "新增客户失败"), OPER_ADD_CUSTOMER_ERROR(8705, "新增客户失败"),
OPER_ADD_CUSTOMER_MANAGER_ERROR(8706, "新增客户管理员失败"); OPER_ADD_CUSTOMER_MANAGER_ERROR(8706, "新增客户管理员失败"),
OPER_UPLOAD_FILE_OVER_SIZE(8707, "文件体积过大"),
OPER_UPLOAD_FILE_TYPE_ERROR(8708, "文件类型错误");
private int code; private int code;
private String msg; private String msg;

18
epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java

@ -10,6 +10,8 @@ package com.epmet.controller;
import com.epmet.cloud.CloudStorageConfig; import com.epmet.cloud.CloudStorageConfig;
import com.epmet.cloud.OssFactory; import com.epmet.cloud.OssFactory;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.page.PageData;
import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.commons.tools.validator.ValidatorUtils;
@ -29,6 +31,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
@ -145,6 +148,21 @@ public class OssController {
*/ */
@PostMapping("customerlogo/upload") @PostMapping("customerlogo/upload")
public Result uploadCustomerLogo(@RequestParam("file") MultipartFile file) { public Result uploadCustomerLogo(@RequestParam("file") MultipartFile file) {
// 校验文件类型
if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg());
}
// 校验文件体积,不超过1m
long maxSize = 1024 * 1024;
long size = file.getSize();
if (size > maxSize) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg());
}
return ossService.uploadImg(file); return ossService.uploadImg(file);
} }

Loading…
Cancel
Save