//腾讯云对象存储
@ResponseBody @PostMapping(value = "/upload") public UploadResponse upload(@RequestParam(value = "file", required = false) MultipartFile file) throws Exception { if(file == null || file.isEmpty()) { throw new UploadFileNotFoundException(UploadResponse.Error.FILENOTFOUND); } try { String originalFileName = file.getOriginalFilename(); String suffix = originalFileName.substring(originalFileName.lastIndexOf(".")).toLowerCase(); // 数据库查询云配置
String value = sysConfigService.selectAll().get(SysConfigKey.CLOUD_STORAGE_CONFIG.getValue()); Gson gson = new Gson(); // 转换为一个配置对象
CloudStorageConfigVo cloudStorageConfig = gson.fromJson(value, CloudStorageConfigVo.class); //获取腾讯云路径前缀
String dir = cloudStorageConfig.getQcloudPrefix(); // 获取字符串的MD5结果,file.getBytes()--输入的字符串转换成字节数组
String md5 = MD5.getMessageDigest(file.getBytes()); String filePath = String.format("%1$s/%2$s%3$s", dir, md5, suffix); ResponseVo responseVo = QCloudUtil.writeFile(cloudStorageConfig, filePath, file); String qCloudDomain = cloudStorageConfig.getQcloudDomain(); String url = String.format("%1$s/%2$s", qCloudDomain, filePath); if(responseVo.getStatus().equals(CoreConst.SUCCESS_CODE)){ return new UploadResponse(url,originalFileName, suffix, url, CoreConst.SUCCESS_CODE); }else{ return new UploadResponse(originalFileName, CoreConst.FAIL_CODE,responseVo.getMsg()); } } catch (Exception e) { logger.error(String.format("UploadController.upload%s", e)); throw e; } }