//騰訊雲對象存儲
@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; } }