Home > Back-end >  Downlad file from resources Spring
Downlad file from resources Spring

Time:12-13

How can i get a file from reosurces / folderX / file.txt

            @PostMapping(value = "/uploadFile", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
        public ResponseEntity<Resource> uploadFile(@RequestParam("file") MultipartFile file) {
  ...............  
    FIRST I UPLOAD FILE NEXT I need return difrent file in this  request 
   ................. 
            return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\""   "file.txt   "\"").body();
        }

CodePudding user response:

You can serve a resource like this:

@GetMapping(value = "file")
public ResponseEntity<Resource> file() {

  Resource resource = new ClassPathResource("folderX/file.txt");

  HttpHeaders headers = new HttpHeaders();
  headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"file.txt\"");

  return new ResponseEntity<>(resource, headers, HttpStatus.OK);
}
  • Related