G2A Many GEOs
SHARE
TWEET

Untitled

a guest Mar 29th, 2020 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.springbootmongodb.photo;
  2.  
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.web.bind.annotation.*;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import javax.imageio.ImageIO;
  9. import java.awt.image.BufferedImage;
  10. import java.io.ByteArrayOutputStream;
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.nio.file.Files;
  14. import java.nio.file.Path;
  15. import java.nio.file.Paths;
  16. import java.util.UUID;
  17.  
  18. @RestController
  19. @RequestMapping("/photo")
  20. public class PhotoController {
  21.     private static String UPLOADED_FOLDER = "/home/asia/projects/weddingbackend/springboot_mongodb_backend/imagesFolder/";
  22.     private static String UPLOADED_QR_CODE_FOLDER = "/home/asia/projects/weddingbackend/springboot_mongodb_backend/qrcode/";
  23.  
  24.     private final static Logger log = LoggerFactory.getLogger(PhotoController.class);
  25.  
  26.     @PostMapping()
  27.     public String addPhoto(@RequestParam("imageFile") MultipartFile imageFile) {
  28.         return saveImageWithReturnId(imageFile);
  29.     }
  30.  
  31.     @PutMapping("/{originalFilename}")
  32.     public String editPhoto(@RequestParam("imageFile") MultipartFile imageFile, @PathVariable("originalFilename") String originalFilename) throws Exception{
  33.         Path path = Paths.get(UPLOADED_FOLDER + originalFilename);
  34.         Files.deleteIfExists(path);
  35.  
  36.         return saveImageWithReturnId(imageFile);
  37.     }
  38.  
  39.     private String saveImageWithReturnId(MultipartFile imageFile) {
  40.         String orgName = "";
  41.         if (!imageFile.isEmpty() && imageFile.getOriginalFilename() != null) {
  42.             try {
  43.                 orgName = UUID.randomUUID().toString().replace("-","").substring(0,8) + ".jpg";
  44.                 String filePath = UPLOADED_FOLDER + orgName;
  45.                 File dest = new File(filePath);
  46.                 imageFile.transferTo(dest);
  47.                 log.info("saveImageWithReturnId: filePath = {}", filePath);
  48.             }catch (Exception e){
  49.                 e.fillInStackTrace();
  50.             }
  51.         }
  52.         return orgName;
  53.     }
  54.  
  55.     @GetMapping(value = "/{originalFilename}", produces = MediaType.IMAGE_JPEG_VALUE)
  56.     public @ResponseBody byte[] getPhoto(@PathVariable("originalFilename") String originalFilename) throws IOException{
  57.         return getBytes(originalFilename, UPLOADED_FOLDER);
  58.     }
  59.  
  60.     @GetMapping(value = "/qrcode/{originalFilename}", produces = MediaType.IMAGE_JPEG_VALUE)
  61.     public @ResponseBody byte[] getQrCodePhoto(@PathVariable("originalFilename") String originalFilename) throws IOException{
  62.         return getBytes(originalFilename, UPLOADED_QR_CODE_FOLDER);
  63.     }
  64.  
  65.     private byte[] getBytes(String originalFilename, String uploadedFolder) throws IOException {
  66.         String path = Paths.get(uploadedFolder + originalFilename).toString();
  67.         log.info("getBytes: path = {}", path);
  68.  
  69.         BufferedImage bufferedImage = ImageIO.read(new File(path));
  70.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  71.         ImageIO.write(bufferedImage, "jpg", output );
  72.         return output.toByteArray();
  73.     }
  74.  
  75.     @DeleteMapping("/{originalFilename}")
  76.     public void deletePhoto(@PathVariable("originalFilename") String originalFilename) throws IOException {
  77.         Path path = Paths.get(UPLOADED_FOLDER + originalFilename);
  78.         Files.deleteIfExists(path);
  79.     }
  80. }
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top