22
33import java .io .FileOutputStream ;
44
5+ import org .springframework .core .io .Resource ;
6+ import org .springframework .core .io .UrlResource ;
7+ import org .springframework .http .HttpHeaders ;
8+ import org .springframework .http .MediaType ;
9+ import org .springframework .http .ResponseEntity ;
510import org .springframework .util .FileCopyUtils ;
11+ import org .springframework .web .bind .annotation .GetMapping ;
612import org .springframework .web .bind .annotation .PostMapping ;
713import org .springframework .web .bind .annotation .RequestMapping ;
814import org .springframework .web .bind .annotation .RequestParam ;
1218import io .github .devopsws .demo .model .Message ;
1319
1420@ RestController
15- @ RequestMapping ("/upload " )
21+ @ RequestMapping ("/file " )
1622public class FileService {
17- @ PostMapping ("/" )
23+ @ PostMapping ("/upload " )
1824 public Message <?> upload (@ RequestParam ("file" ) MultipartFile file ) {
1925 System .out .println ("Received file uploading request" );
2026 Message <String > message = new Message <String >();
@@ -34,4 +40,17 @@ public Message<?> upload(@RequestParam("file") MultipartFile file) {
3440 }
3541 return message ;
3642 }
43+
44+ @ GetMapping ("/download" )
45+ public ResponseEntity <Resource > downloadFile () {
46+ Resource resource = new UrlResource (FileService .class .getResource ("/application.yml" ));
47+ if (resource .exists () || resource .isReadable ()) {
48+ return ResponseEntity .ok ()
49+ .contentType (MediaType .APPLICATION_OCTET_STREAM )
50+ .header (HttpHeaders .CONTENT_DISPOSITION , "attachment; filename=\" " + resource .getFilename () + "\" " )
51+ .body (resource );
52+ } else {
53+ return ResponseEntity .notFound ().build ();
54+ }
55+ }
3756}
0 commit comments