source: src/main/java/com/tourMate/controllers/ImagesController.java@ ac19a0c

Last change on this file since ac19a0c was e9b4ba9, checked in by darsov2 <62809499+darsov2@…>, 12 months ago

prototype

  • Property mode set to 100644
File size: 1.9 KB
Line 
1package com.tourMate.controllers;
2
3import org.slf4j.Logger;
4import org.slf4j.LoggerFactory;
5import org.springframework.core.io.InputStreamResource;
6import org.springframework.http.HttpHeaders;
7import org.springframework.http.MediaType;
8import org.springframework.http.ResponseEntity;
9import org.springframework.web.bind.annotation.*;
10import org.springframework.web.multipart.MultipartFile;
11
12import java.io.File;
13import java.io.FileInputStream;
14import java.io.IOException;
15import java.nio.file.Path;
16import java.nio.file.Paths;
17
18@CrossOrigin("*")
19@RestController
20@RequestMapping("/images")
21class 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}
Note: See TracBrowser for help on using the repository browser.