1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @PostMapping("/upload") public R upload(@RequestParam("file") MultipartFile file, String folder) { folder = Optional.ofNullable(folder).orElse("folder"); String fileName = file.getOriginalFilename(); Map<String, Object> resultMap = new HashMap<>(4); resultMap.put("fileName", fileName); resultMap.put("url", String.format("/%s/%s", folder, fileName));
try { ossTemplate.createBucket(folder); ossTemplate.putObject(folder, fileName, file.getInputStream()); resultMap.put("name", file.getOriginalFilename()); } catch (Exception e) { log.error("上传失败", e); return R.fail(e.getLocalizedMessage()); } return R.ok(resultMap); }
|