Guest User

UploadController.java

a guest
Jan 3rd, 2019
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package com.liferon.boot2demo;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.MalformedURLException;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.nio.file.StandardCopyOption;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import org.springframework.core.io.Resource;
  13. import org.springframework.core.io.UrlResource;
  14. import org.springframework.http.HttpHeaders;
  15. import org.springframework.http.ResponseEntity;
  16. import org.springframework.util.StringUtils;
  17. import org.springframework.web.bind.annotation.GetMapping;
  18. import org.springframework.web.bind.annotation.PathVariable;
  19. import org.springframework.web.bind.annotation.PostMapping;
  20. import org.springframework.web.bind.annotation.RequestParam;
  21. import org.springframework.web.bind.annotation.ResponseBody;
  22. import org.springframework.web.bind.annotation.RestController;
  23. import org.springframework.web.multipart.MultipartFile;
  24.  
  25. /**
  26.  *
  27.  * @author olanrewaju.ebenezer
  28.  */
  29. @RestController
  30. public class UploadController {
  31.  
  32.     private final String uploadFolder = "uploads";
  33.     private final Path rootLocation;
  34.  
  35.     public UploadController() throws IOException {
  36.         rootLocation = Paths.get(uploadFolder);
  37.         if (Files.notExists(rootLocation)) {
  38.             try {
  39.                 Files.createDirectories(rootLocation);
  40.             } catch (IOException ex) {
  41.                 System.err.println("IOException: " + ex.getMessage());
  42.                 throw ex;
  43.                 //Logger.getLogger(UploadController.class.getName()).log(Level.SEVERE, null, ex);
  44.             }
  45.         }
  46.     }
  47.  
  48.     @GetMapping("/pictures/{filename:.+}")
  49.     @ResponseBody
  50.     public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
  51.  
  52.         Path path = rootLocation.resolve(filename);
  53.         Resource file = null;
  54.         try {
  55.             file = new UrlResource(path.toUri());
  56.         } catch (MalformedURLException ex) {
  57.             Logger.getLogger(UploadController.class.getName()).log(Level.SEVERE, null, ex);
  58.         }
  59.         return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
  60.                 "attachment; filename=\"" + file.getFilename() + "\"").body(file);
  61.     }
  62.  
  63.     @PostMapping("/api/upload")
  64.     public ResponseEntity<?> handleFileUpload(@RequestParam("picture") MultipartFile picture) {
  65.         String filename = StringUtils.cleanPath(picture.getOriginalFilename());
  66.  
  67.         try (InputStream inputStream = picture.getInputStream()) {
  68.             Files.copy(inputStream, this.rootLocation.resolve(filename),
  69.                     StandardCopyOption.REPLACE_EXISTING);
  70.         } catch (IOException ex) {
  71.             Logger.getLogger(UploadController.class.getName()).log(Level.SEVERE, null, ex);
  72.         }
  73.  
  74.         return ResponseEntity.ok("File uploaded successfully");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment