Browse Source

【代码优化】关闭未知文件上传接口

national_dev
Bill 2 years ago
parent
commit
cdbddd6aa2
  1. 568
      code/smart-community/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java
  2. 102
      code/smart-community/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/TestController.java

568
code/smart-community/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/OssController.java

@ -71,64 +71,64 @@ public class OssController {
private final static String KEY = ModuleConstant.CLOUD_STORAGE_CONFIG_KEY; private final static String KEY = ModuleConstant.CLOUD_STORAGE_CONFIG_KEY;
@GetMapping("page") // @GetMapping("page")
public Result<PageData<OssEntity>> page(@RequestParam Map<String, Object> params){ // public Result<PageData<OssEntity>> page(@RequestParam Map<String, Object> params){
PageData<OssEntity> page = ossService.page(params); // PageData<OssEntity> page = ossService.page(params);
//
return new Result<PageData<OssEntity>>().ok(page); // return new Result<PageData<OssEntity>>().ok(page);
} // }
//
@GetMapping("info") // @GetMapping("info")
public Result<CloudStorageConfig> info(){ // public Result<CloudStorageConfig> info(){
CloudStorageConfig config = paramsRemoteService.getValueObject(KEY, CloudStorageConfig.class); // CloudStorageConfig config = paramsRemoteService.getValueObject(KEY, CloudStorageConfig.class);
//
return new Result<CloudStorageConfig>().ok(config); // return new Result<CloudStorageConfig>().ok(config);
} // }
@PostMapping // @PostMapping
// @LogOperation("保存云存储配置信息") //// @LogOperation("保存云存储配置信息")
public Result saveConfig(@RequestBody CloudStorageConfig config){ // public Result saveConfig(@RequestBody CloudStorageConfig config){
//校验类型 // //校验类型
ValidatorUtils.validateEntity(config); // ValidatorUtils.validateEntity(config);
//
if(config.getType() == OssTypeEnum.QINIU.value()){ // if(config.getType() == OssTypeEnum.QINIU.value()){
//校验七牛数据 // //校验七牛数据
ValidatorUtils.validateEntity(config, QiniuGroup.class); // ValidatorUtils.validateEntity(config, QiniuGroup.class);
}else if(config.getType() == OssTypeEnum.ALIYUN.value()){ // }else if(config.getType() == OssTypeEnum.ALIYUN.value()){
//校验阿里云数据 // //校验阿里云数据
ValidatorUtils.validateEntity(config, AliyunGroup.class); // ValidatorUtils.validateEntity(config, AliyunGroup.class);
}else if(config.getType() == OssTypeEnum.QCLOUD.value()){ // }else if(config.getType() == OssTypeEnum.QCLOUD.value()){
//校验腾讯云数据 // //校验腾讯云数据
ValidatorUtils.validateEntity(config, QcloudGroup.class); // ValidatorUtils.validateEntity(config, QcloudGroup.class);
} // }
//
paramsRemoteService.updateValueByCode(KEY, new Gson().toJson(config)); // paramsRemoteService.updateValueByCode(KEY, new Gson().toJson(config));
//
return new Result(); // return new Result();
} // }
@PostMapping("upload") // @PostMapping("upload")
public Result<UploadDTO> upload(@RequestPart("file") MultipartFile file) throws Exception { // public Result<UploadDTO> upload(@RequestPart("file") MultipartFile file) throws Exception {
if (file.isEmpty()) { // if (file.isEmpty()) {
return new Result<UploadDTO>().error(ModuleErrorCode.UPLOAD_FILE_EMPTY); // return new Result<UploadDTO>().error(ModuleErrorCode.UPLOAD_FILE_EMPTY);
} // }
//
//上传文件 // //上传文件
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); // String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String url = OssFactory.build().uploadSuffix(file.getBytes(), extension, null); // String url = OssFactory.build().uploadSuffix(file.getBytes(), extension, null);
//
//保存文件信息 // //保存文件信息
OssEntity ossEntity = new OssEntity(); // OssEntity ossEntity = new OssEntity();
ossEntity.setUrl(url); // ossEntity.setUrl(url);
ossService.insertOssEntity(ossEntity); // ossService.insertOssEntity(ossEntity);
//
//文件信息 // //文件信息
UploadDTO dto = new UploadDTO(); // UploadDTO dto = new UploadDTO();
dto.setUrl(url); // dto.setUrl(url);
dto.setSize(file.getSize()); // dto.setSize(file.getSize());
//
return new Result<UploadDTO>().ok(dto); // return new Result<UploadDTO>().ok(dto);
} // }
@DeleteMapping @DeleteMapping
// @LogOperation("删除") // @LogOperation("删除")
@ -150,182 +150,182 @@ public class OssController {
return ossService.uploadImg(file, null); return ossService.uploadImg(file, null);
} }
/** // /**
* 压缩上传 // * 压缩上传
* // *
* @param file // * @param file
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.result.UploadImgResultDTO> // * @return com.epmet.commons.tools.utils.Result<com.epmet.dto.result.UploadImgResultDTO>
* @author zhy // * @author zhy
* @date 2022/5/24 13:32 // * @date 2022/5/24 13:32
*/ // */
@PostMapping("compressuploadimg") // @PostMapping("compressuploadimg")
public Result<UploadImgResultDTO> compressUploadImg(@RequestParam("file") MultipartFile file) { // public Result<UploadImgResultDTO> compressUploadImg(@RequestParam("file") MultipartFile file) {
return ossService.compressUploadImg(file, null,200L); // return ossService.compressUploadImg(file, null,200L);
} // }
@PostMapping("uploadwximg") // @PostMapping("uploadwximg")
public Result<UploadImgResultDTO> uploadWxImg(@RequestPart("media") MultipartFile media) { // public Result<UploadImgResultDTO> uploadWxImg(@RequestPart("media") MultipartFile media) {
return ossService.uploadImg(media, null); // return ossService.uploadImg(media, null);
} // }
//
@PostMapping("uploadqrcode") // @PostMapping("uploadqrcode")
public Result<UploadImgResultDTO> uploadQrCode(@RequestPart(value = "file") MultipartFile file) { // public Result<UploadImgResultDTO> uploadQrCode(@RequestPart(value = "file") MultipartFile file) {
return ossService.uploadImg(file, null); // return ossService.uploadImg(file, null);
} // }
/** // /**
* 上传客户logo(考虑到以后可能会针对不同的业务有不同的限制条件这里不再使用通用的接口 // * 上传客户logo(考虑到以后可能会针对不同的业务有不同的限制条件,这里不再使用通用的接口
* 针对每一个业务新建上传接口) // * 针对每一个业务新建上传接口)
* @param file // * @param file
* @return // * @return
*/ // */
@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())) { // if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg());
} // }
//
// 校验文件体积,不超过1m // // 校验文件体积,不超过1m
long maxSize = 1024 * 1024; // long maxSize = 1024 * 1024;
long size = file.getSize(); // long size = file.getSize();
if (size > maxSize) { // if (size > maxSize) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg());
} // }
//
return ossService.uploadImg(file, null); // return ossService.uploadImg(file, null);
} // }
@PostMapping("function/upload") // @PostMapping("function/upload")
public Result uploadFunctionLogo(@RequestParam("file") MultipartFile file) { // public Result uploadFunctionLogo(@RequestParam("file") MultipartFile file) {
//
// 校验文件类型 // // 校验文件类型
if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())) { // if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg());
} // }
//
// 校验文件体积,不超过1m // // 校验文件体积,不超过1m
long maxSize = 1024 * 1024; // long maxSize = 1024 * 1024;
long size = file.getSize(); // long size = file.getSize();
if (size > maxSize) { // if (size > maxSize) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg());
} // }
//
return ossService.uploadImg(file, null); // return ossService.uploadImg(file, null);
} // }
/** // /**
* desc: 党建声音模块 上传图片2M或附件5M // * desc: 党建声音模块 上传图片(2M)或附件(5M)
* // *
* @param file // * @param file
* @return com.epmet.commons.tools.utils.Result<com.epmet.dto.result.UploadImgResultDTO> // * @return com.epmet.commons.tools.utils.Result<com.epmet.dto.result.UploadImgResultDTO>
* @author LiuJanJun // * @author LiuJanJun
* @date 2021/3/29 4:56 下午 // * @date 2021/3/29 4:56 下午
*/ // */
@PostMapping("article/upload") // @PostMapping("article/upload")
public Result<UploadImgResultDTO> uploadArticleImg(@RequestParam("file") MultipartFile file) { // public Result<UploadImgResultDTO> uploadArticleImg(@RequestParam("file") MultipartFile file) {
//
// 校验文件类型 图片或pdf // // 校验文件类型 图片或pdf
if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType()) // if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())
&& !MediaType.IMAGE_JPEG_VALUE.equals(file.getContentType()) // && !MediaType.IMAGE_JPEG_VALUE.equals(file.getContentType())
&& !ModuleConstant.FILE_CONTENT_TYPE_JPG.equals(file.getContentType()) // && !ModuleConstant.FILE_CONTENT_TYPE_JPG.equals(file.getContentType())
&& !ModuleConstant.FILE_CONTENT_TYPE_PDF.equals(file.getContentType()) // && !ModuleConstant.FILE_CONTENT_TYPE_PDF.equals(file.getContentType())
&& !file.getContentType().startsWith(ModuleConstant.FILE_CONTENT_TYPE_VEDIO_PREFIX) // && !file.getContentType().startsWith(ModuleConstant.FILE_CONTENT_TYPE_VEDIO_PREFIX)
) { // ) {
log.error("uploadArticleImg file type:{} is not support 2 upload", file.getContentType()); // log.error("uploadArticleImg file type:{} is not support 2 upload", file.getContentType());
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg());
} // }
long size = file.getSize(); // long size = file.getSize();
if (ModuleConstant.FILE_CONTENT_TYPE_PDF.equals(file.getContentType())) { // if (ModuleConstant.FILE_CONTENT_TYPE_PDF.equals(file.getContentType())) {
// 校验文件大小,不超过5m // // 校验文件大小,不超过5m
long maxSize = 5 * 1024 * 1024; // long maxSize = 5 * 1024 * 1024;
if (size > maxSize) { // if (size > maxSize) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg());
} // }
} else if (file.getContentType().startsWith(ModuleConstant.FILE_CONTENT_TYPE_VEDIO_PREFIX)) { // } else if (file.getContentType().startsWith(ModuleConstant.FILE_CONTENT_TYPE_VEDIO_PREFIX)) {
// 校验文件大小,不超过5m // // 校验文件大小,不超过5m
long maxSize = 25 * 1024 * 1024; // long maxSize = 25 * 1024 * 1024;
if (size > maxSize) { // if (size > maxSize) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg());
} // }
} // }
Result<UploadImgResultDTO> result =null; // Result<UploadImgResultDTO> result =null;
String originFileName = file.getOriginalFilename(); // String originFileName = file.getOriginalFilename();
String format = "-" + originFileName.substring(originFileName.lastIndexOf(".") + NumConstant.ONE) + "-"; // String format = "-" + originFileName.substring(originFileName.lastIndexOf(".") + NumConstant.ONE) + "-";
format = format.toLowerCase(); // format = format.toLowerCase();
int compressSize = 2 * 1024 * 1024; // int compressSize = 2 * 1024 * 1024;
// 如果是图片,超过2M的压缩一下 // // 如果是图片,超过2M的压缩一下
if (ModuleConstant.PROJECT_FILE_IMAGE.contains(format) && file.getSize() > compressSize) { // if (ModuleConstant.PROJECT_FILE_IMAGE.contains(format) && file.getSize() > compressSize) {
// 单个图片10M // // 单个图片10M
result= ossService.compressUploadImg(file, null, 2 * 1024); // result= ossService.compressUploadImg(file, null, 2 * 1024);
} else { // } else {
result=ossService.uploadImg(file, null); // result=ossService.uploadImg(file, null);
} // }
if (result != null && result.success() ){ // if (result != null && result.success() ){
UploadImgResultDTO data = result.getData(); // UploadImgResultDTO data = result.getData();
if ( data!= null && StringUtils.isNotBlank(data.getUrl())){ // if ( data!= null && StringUtils.isNotBlank(data.getUrl())){
String path = data.getUrl().replace(data.getDomain(), StrConstant.EPMETY_STR); // String path = data.getUrl().replace(data.getDomain(), StrConstant.EPMETY_STR);
if (path.startsWith(StrConstant.SEPARATOR)){ // if (path.startsWith(StrConstant.SEPARATOR)){
path = path.replaceFirst(StrConstant.SEPARATOR,StrConstant.EPMETY_STR); // path = path.replaceFirst(StrConstant.SEPARATOR,StrConstant.EPMETY_STR);
} // }
String fileName = data.getUrl().substring(data.getUrl().lastIndexOf(StrConstant.SEPARATOR)+1); // String fileName = data.getUrl().substring(data.getUrl().lastIndexOf(StrConstant.SEPARATOR)+1);
redisUtils.set(RedisKeys.getOssFileKey(fileName),path,RedisUtils.MINUTE_THIRTY_EXPIRE); // redisUtils.set(RedisKeys.getOssFileKey(fileName),path,RedisUtils.MINUTE_THIRTY_EXPIRE);
} // }
} // }
return result; // return result;
} // }
@PostMapping("uploadimg/badgeicon") // @PostMapping("uploadimg/badgeicon")
public Result<UploadImgResultDTO> badgeIcon(@RequestParam("file") MultipartFile file) throws IOException { // public Result<UploadImgResultDTO> badgeIcon(@RequestParam("file") MultipartFile file) throws IOException {
//
// 校验文件类型 // // 校验文件类型
if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())) { // if (!MediaType.IMAGE_PNG_VALUE.equals(file.getContentType())) {
log.error("uploadArticleImg file type:{} is not support 2 upload", file.getContentType()); // log.error("uploadArticleImg file type:{} is not support 2 upload", file.getContentType());
throw new RenException(EpmetErrorCode.OPER_UPLOAD_IMG_TYPE_ERROR.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_IMG_TYPE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_IMG_TYPE_ERROR.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_IMG_TYPE_ERROR.getMsg());
} // }
//
// 校验文件体积,不超过2m // // 校验文件体积,不超过2m
long maxSize = 2 * 1024 * 1024; // long maxSize = 2 * 1024 * 1024;
long size = file.getSize(); // long size = file.getSize();
if (size > maxSize) { // if (size > maxSize) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg());
} // }
//
BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); // BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
int width = bufferedImage.getWidth(); // int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight(); // int height = bufferedImage.getHeight();
if (width != 200 || height != 200) { // if (width != 200 || height != 200) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_IMG_SIZE_ERROR.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_IMG_SIZE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_IMG_SIZE_ERROR.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_IMG_SIZE_ERROR.getMsg());
//
} // }
return ossService.uploadImg(file, null); // return ossService.uploadImg(file, null);
} // }
/** // /**
* @param byteFile fileName // * @param byteFile fileName
* @Description 外挂-文件上传 // * @Description 外挂-文件上传
* @Author sun // * @Author sun
**/ // **/
@PostMapping("ext/upload") // @PostMapping("ext/upload")
public Result<UploadImgResultDTO> extUpload(@RequestParam("byteFile") byte[] byteFile, @RequestParam("fileName") String fileName) throws IOException { // public Result<UploadImgResultDTO> extUpload(@RequestParam("byteFile") byte[] byteFile, @RequestParam("fileName") String fileName) throws IOException {
//public Result<UploadImgResultDTO> extUpload(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName) throws IOException { // //public Result<UploadImgResultDTO> extUpload(@RequestParam("file") MultipartFile file, @RequestParam("fileName") String fileName) throws IOException {
//byte[] byteFile = file.getBytes(); // //byte[] byteFile = file.getBytes();
InputStream inputStream = new ByteArrayInputStream(byteFile); // InputStream inputStream = new ByteArrayInputStream(byteFile);
MultipartFile filse = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); // MultipartFile filse = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
return ossService.extUpload(filse,fileName, null); // return ossService.extUpload(filse,fileName, null);
} // }
/** /**
* @param file * @param file
@ -388,16 +388,16 @@ public class OssController {
return new Result<Boolean>().ok(ossService.articleRemoveFile(formDTO)); return new Result<Boolean>().ok(ossService.articleRemoveFile(formDTO));
} }
/** // /**
* 运营端生成小程序体验码传递客户Id [开发测试环境会直接访问生产third没有token需要业务中传递客户Id值] // * 运营端生成小程序体验码,传递客户Id [开发、测试环境会直接访问生产third,没有token,需要业务中传递客户Id值]
* @param file 文件 // * @param file 文件
* @param file 文件 // * @param file 文件
* @author sun // * @author sun
*/ // */
@PostMapping("uploadqrcodeV2") // @PostMapping("uploadqrcodeV2")
public Result<UploadImgResultDTO> uploadQrCodeV2(@RequestPart(value = "file") MultipartFile file, @RequestParam("customerId") String customerId) { // public Result<UploadImgResultDTO> uploadQrCodeV2(@RequestPart(value = "file") MultipartFile file, @RequestParam("customerId") String customerId) {
return ossService.uploadImgV2(file, null, customerId); // return ossService.uploadImgV2(file, null, customerId);
} // }
/** /**
* @Description 上传报事文件(目前只有图片) * @Description 上传报事文件(目前只有图片)
@ -432,51 +432,51 @@ public class OssController {
return ossService.uploadImgV2(file, PrivacyType.EXTERNAL, customerId); return ossService.uploadImgV2(file, PrivacyType.EXTERNAL, customerId);
} }
/** // /**
* @Description 办事指南上传附件 // * @Description 办事指南上传附件
* @Param file // * @Param file
* @Param customerId // * @Param customerId
* @Return {@link Result< UploadImgResultDTO>} // * @Return {@link Result< UploadImgResultDTO>}
* @Author zhaoqifeng // * @Author zhaoqifeng
* @Date 2021/9/8 17:14 // * @Date 2021/9/8 17:14
*/ // */
@PostMapping("upload-guide-file") // @PostMapping("upload-guide-file")
public Result<UploadImgResultDTO> uploadGuideFile(@RequestPart(value = "file") MultipartFile file, @RequestParam("customerId") String customerId) { // public Result<UploadImgResultDTO> uploadGuideFile(@RequestPart(value = "file") MultipartFile file, @RequestParam("customerId") String customerId) {
String fileName = file.getOriginalFilename(); // String fileName = file.getOriginalFilename();
String format = "-" + fileName.substring(fileName.lastIndexOf(".") + NumConstant.ONE) + "-"; // String format = "-" + fileName.substring(fileName.lastIndexOf(".") + NumConstant.ONE) + "-";
format = format.toLowerCase(); // format = format.toLowerCase();
// 体积限制 // // 体积限制
int sizeMb = 10; // int sizeMb = 10;
// 大小限制10m // // 大小限制10m
int sizeThreshold = sizeMb * 1024 * 1024; // int sizeThreshold = sizeMb * 1024 * 1024;
if (file.getSize() > sizeThreshold) { // if (file.getSize() > sizeThreshold) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode(), // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getCode(),
EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg().concat(",限制在").concat(String.valueOf(sizeMb)).concat("M以内")); // EpmetErrorCode.OPER_UPLOAD_FILE_OVER_SIZE.getMsg().concat(",限制在").concat(String.valueOf(sizeMb)).concat("M以内"));
} // }
//
// 格式限制,只允许png和jpeg // // 格式限制,只允许png和jpeg
if (!ModuleConstant.GUIDE_FILE_DOC.contains(format)) { // if (!ModuleConstant.GUIDE_FILE_DOC.contains(format)) {
throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode() // throw new RenException(EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getCode()
, EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg()); // , EpmetErrorCode.OPER_UPLOAD_FILE_TYPE_ERROR.getMsg());
} // }
//
return ossService.uploadImgV2(file, PrivacyType.EXTERNAL, customerId); // return ossService.uploadImgV2(file, PrivacyType.EXTERNAL, customerId);
} // }
@GetMapping("getOssFileUrl") @GetMapping("getOssFileUrl")
public Result<String> download(@RequestParam(required = false) String filePath,@RequestParam(required = false) String privacy){ public Result<String> download(@RequestParam(required = false) String filePath,@RequestParam(required = false) String privacy){
return new Result<String>().ok(ossService.getOssFileUrl(filePath,privacy)); return new Result<String>().ok(ossService.getOssFileUrl(filePath,privacy));
} }
/** // /**
* 上传任务描述文件 // * 上传任务描述文件
* 不限制大小 // * 不限制大小
* @param file // * @param file
* @return // * @return
*/ // */
@PostMapping("/importTaskDescFile/upload") // @PostMapping("/importTaskDescFile/upload")
public Result<UploadImgResultDTO> uploadImportTaskDescFile(@RequestPart("file") MultipartFile file) { // public Result<UploadImgResultDTO> uploadImportTaskDescFile(@RequestPart("file") MultipartFile file) {
return ossService.uploadImg(file, null); // return ossService.uploadImg(file, null);
} // }
} }

102
code/smart-community/epmet-module/epmet-oss/epmet-oss-server/src/main/java/com/epmet/controller/TestController.java

@ -13,57 +13,57 @@ import java.io.*;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@Slf4j //@Slf4j
@RestController //@RestController
@RequestMapping("test") //@RequestMapping("test")
public class TestController { public class TestController {
@Autowired // @Autowired
private OssService ossService; // private OssService ossService;
//
@PostMapping("local-upload") // @PostMapping("local-upload")
public Result localUpload(@RequestPart("file") MultipartFile file, @RequestParam("fileName") String fileName) { // public Result localUpload(@RequestPart("file") MultipartFile file, @RequestParam("fileName") String fileName) {
//
final File tempFile = new File("/opt/upload_files/" + fileName); // final File tempFile = new File("/opt/upload_files/" + fileName);
//
try(InputStream inputStream = file.getInputStream()) { // try(InputStream inputStream = file.getInputStream()) {
FileUtils.copyInputStreamToFile(inputStream, tempFile); // FileUtils.copyInputStreamToFile(inputStream, tempFile);
} catch (IOException e) { // } catch (IOException e) {
log.error("method exception", e); // log.error("method exception", e);
} // }
return new Result(); // return new Result();
} // }
//
@PostMapping("upload2aliyun") // @PostMapping("upload2aliyun")
public Result upload2aliyun() { // public Result upload2aliyun() {
try (final FileInputStream fis = new FileInputStream("C:\\Users\\wxz\\Documents\\微信截图_20230101224521.png")) { // try (final FileInputStream fis = new FileInputStream("C:\\Users\\wxz\\Documents\\微信截图_20230101224521.png")) {
final MockMultipartFile mockMultipartFile = new MockMultipartFile("1.png", fis); // final MockMultipartFile mockMultipartFile = new MockMultipartFile("1.png", fis);
return ossService.uploadImg(mockMultipartFile, null); // return ossService.uploadImg(mockMultipartFile, null);
} catch (FileNotFoundException e) { // } catch (FileNotFoundException e) {
log.error("method exception", e); // log.error("method exception", e);
} catch (IOException e) { // } catch (IOException e) {
log.error("method exception", e); // log.error("method exception", e);
} // }
return new Result(); // return new Result();
} // }
//
/** // /**
* @Description ribbon测试 // * @Description ribbon测试
* @return // * @return
* @author wxz // * @author wxz
* @date 2021.08.05 16:28 // * @date 2021.08.05 16:28
*/ // */
@PostMapping("test-ribbon-rcv/{sleep}") // @PostMapping("test-ribbon-rcv/{sleep}")
public Result testRibbonRcv(@PathVariable("sleep") Long sleep) { // public Result testRibbonRcv(@PathVariable("sleep") Long sleep) {
InetAddress localHost = null; // InetAddress localHost = null;
try { // try {
Thread.sleep(sleep); // Thread.sleep(sleep);
localHost = Inet4Address.getLocalHost(); // localHost = Inet4Address.getLocalHost();
} catch (UnknownHostException e) { // } catch (UnknownHostException e) {
log.error("method exception", e); // log.error("method exception", e);
} catch (InterruptedException e) { // } catch (InterruptedException e) {
log.error("method exception", e); // log.error("method exception", e);
} // }
return new Result().ok(localHost); // return new Result().ok(localHost);
} // }
} }

Loading…
Cancel
Save