[e9b4ba9] | 1 | package com.tourMate.controllers;
|
---|
| 2 |
|
---|
| 3 | import org.slf4j.Logger;
|
---|
| 4 | import org.slf4j.LoggerFactory;
|
---|
| 5 | import org.springframework.core.io.InputStreamResource;
|
---|
| 6 | import org.springframework.http.HttpHeaders;
|
---|
| 7 | import org.springframework.http.MediaType;
|
---|
| 8 | import org.springframework.http.ResponseEntity;
|
---|
| 9 | import org.springframework.web.bind.annotation.*;
|
---|
| 10 | import org.springframework.web.multipart.MultipartFile;
|
---|
| 11 |
|
---|
| 12 | import java.io.File;
|
---|
| 13 | import java.io.FileInputStream;
|
---|
| 14 | import java.io.IOException;
|
---|
| 15 | import java.nio.file.Path;
|
---|
| 16 | import java.nio.file.Paths;
|
---|
| 17 |
|
---|
| 18 | @CrossOrigin("*")
|
---|
| 19 | @RestController
|
---|
| 20 | @RequestMapping("/images")
|
---|
| 21 | class ImageController {
|
---|
| 22 |
|
---|
| 23 | private static final Logger logger = LoggerFactory.getLogger(ImageController.class);
|
---|
| 24 |
|
---|
| 25 | @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
---|
| 26 | public ResponseEntity uploadFile(@RequestParam MultipartFile file, @RequestParam String type) {
|
---|
| 27 |
|
---|
| 28 | try
|
---|
| 29 | {
|
---|
| 30 | Path desktopPath = Paths.get(System.getProperty("user.home"), "Desktop\\images_tm");
|
---|
| 31 | Path filePath = desktopPath.resolve(file.getOriginalFilename());
|
---|
| 32 | System.out.println(filePath);
|
---|
| 33 | file.transferTo(filePath.toFile());
|
---|
| 34 | } catch (IOException e) {
|
---|
| 35 | throw new RuntimeException(e);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | logger.info(String.format("File name '%s' uploaded successfully.", file.getOriginalFilename()));
|
---|
| 39 | return ResponseEntity.ok().build();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | @GetMapping("/download")
|
---|
| 43 | public ResponseEntity downloadFile1(@RequestParam String fileName) throws IOException {
|
---|
| 44 |
|
---|
| 45 | File file = new File(fileName);
|
---|
| 46 | InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
---|
| 47 |
|
---|
| 48 | return ResponseEntity.ok()
|
---|
| 49 | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
|
---|
| 50 | .contentType(MediaType.APPLICATION_OCTET_STREAM)
|
---|
| 51 | .contentLength(file.length())
|
---|
| 52 | .body(resource);
|
---|
| 53 | }
|
---|
| 54 | } |
---|