2
2
3
3
4
4
import oracleai .services .ORDSCalls ;
5
+ import org .apache .tomcat .util .http .fileupload .FileUtils ;
6
+ import org .springframework .http .ResponseEntity ;
5
7
import org .springframework .stereotype .Controller ;
6
8
import org .springframework .ui .Model ;
7
- import org .springframework .web .bind .annotation .GetMapping ;
8
- import org .springframework .web .bind .annotation .PostMapping ;
9
- import org .springframework .web .bind .annotation .RequestMapping ;
10
- import org .springframework .web .bind .annotation .RequestParam ;
9
+ import org .springframework .web .bind .annotation .*;
11
10
import org .springframework .web .multipart .MultipartFile ;
12
11
12
+ import java .io .File ;
13
+ import java .io .IOException ;
14
+ import java .nio .file .Path ;
15
+ import java .nio .file .Paths ;
16
+
17
+ import org .apache .commons .io .FileUtils ;
18
+ import org .springframework .http .MediaType ;
19
+
13
20
@ Controller
14
21
@ RequestMapping ("/transferimage" )
15
22
public class UploadDownloadImage {
@@ -23,16 +30,54 @@ public String uploadImage(@RequestParam("image") MultipartFile image, Model mode
23
30
return "images" ;
24
31
}
25
32
33
+ // @PostMapping("/uploadimageandvideo")
34
+ // public String uploadimageandvideo(@RequestParam("image") MultipartFile image, Model model) {
35
+ //// ORDSCalls.uploadImage(image);
36
+ //// System.out.println("Image upload complete for: " + image.getOriginalFilename());
37
+ // System.out.println("convertImage(): " + ORDSCalls.convertImage());
38
+ // ImageStore[] imageStores = ORDSCalls.getImageStoreData();
39
+ // model.addAttribute("images", imageStores);
40
+ // return "images";
41
+ // }
42
+
43
+
44
+ private static final String DIRECTORY = "/tmp/images/" ;
45
+
26
46
@ PostMapping ("/uploadimageandvideo" )
27
- public String uploadimageandvideo (@ RequestParam ("image" ) MultipartFile image , Model model ) {
28
- // ORDSCalls.uploadImage(image);
29
- // System.out.println("Image upload complete for: " + image.getOriginalFilename());
30
- System .out .println ("convertImage(): " + ORDSCalls .convertImage ());
31
- ImageStore [] imageStores = ORDSCalls .getImageStoreData ();
32
- model .addAttribute ("images" , imageStores );
33
- return "images" ;
47
+ public ResponseEntity <String > uploadImage (@ RequestParam ("image" ) MultipartFile file ) throws IOException {
48
+ if (file .isEmpty ()) {
49
+ return ResponseEntity .badRequest ().body ("Cannot upload empty file" );
50
+ }
51
+
52
+ try {
53
+ FileUtils .forceMkdir (new File (DIRECTORY ));
54
+ Path path = Paths .get (DIRECTORY + file .getOriginalFilename ());
55
+ file .transferTo (path );
56
+
57
+ return ResponseEntity .ok ("File uploaded and available at: " + "/images/" + file .getOriginalFilename ());
58
+ } catch (Exception e ) {
59
+ return ResponseEntity .internalServerError ().body ("Could not upload the file: " + e .getMessage ());
60
+ }
34
61
}
35
62
63
+ @ GetMapping ("/images/{filename:.+}" )
64
+ public ResponseEntity <byte []> getImage (@ PathVariable String filename ) throws IOException {
65
+ try {
66
+ File file = new File (DIRECTORY , filename );
67
+ byte [] fileContent = FileUtils .readFileToByteArray (file );
68
+ return ResponseEntity .ok ().contentType (MediaType .IMAGE_JPEG ).body (fileContent );
69
+ } catch (IOException e ) {
70
+ return ResponseEntity .notFound ().build ();
71
+ }
72
+ }
73
+
74
+
75
+
76
+
77
+
78
+
79
+
80
+
36
81
37
82
38
83
@ GetMapping ("/downloadimages" )
0 commit comments