@RestController
@RequestMapping("/image")
@Slf4j
@CrossOrigin(origins= "*")public classImageController {
@AutowiredprivateFileService fileService;/*******
* Get image file, this method return an image type file which can be displayed in browser.
* @param bucketName, system, each system should belong a special bucket.
* @param category, a system may contain multiple category
* @param fileName*/@GetMapping(value= "/get/{bucketName}/{category}/{objectName}/{fileName}", produces =MediaType.IMAGE_JPEG_VALUE)public byte[] get(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
@PathVariable("objectName") String objectName,
@PathVariable("fileName") String fileName) throws Exception {returnfileService.getFile(bucketName, category, objectName);
}
@GetMapping("/download/{bucketName}/{category}/{objectName}/{fileName}")public void download(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
@PathVariable("objectName") String objectName,
@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {byte[] buffer =fileService.getFile(bucketName, category, objectName);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
response.getOutputStream().write(buffer);
response.flushBuffer();
response.getOutputStream().close();
}
@PostMapping("/upload/{bucketName}/{category}")public String upload(@PathVariable("bucketName") String bucketName, @PathVariable("category") String category,
@RequestParam("file") MultipartFile file) throws Exception {
String objectName=UUID.randomUUID().toString();
fileService.storeFile(bucketName, category, objectName, file.getBytes());return String.format("image/get/%s/%s/%s/%s", bucketName, category, objectName, file.getOriginalFilename());
}
}