@RequestMapping(value = "/download", method = RequestMethod.GET) public ResponseEntity downloadFile(@RequestParam("filename") String filename) { Document document = documentService.findDocumentByName(filename); // No file found based on the supplied filename if (document == null) { return new ResponseEntity<>("{}", HttpStatus.NOT_FOUND); } // Generate the http headers with the file properties HttpHeaders headers = new HttpHeaders(); headers.add("content-disposition", "attachment; filename=" + document.getName()); // Split the mimeType into primary and sub types String primaryType, subType; try { primaryType = document.getMimeType().split("/")[0]; subType = document.getMimeType().split("/")[1]; } catch (IndexOutOfBoundsException | NullPointerException ex) { return new ResponseEntity<>("{}", HttpStatus.INTERNAL_SERVER_ERROR); } headers.setContentType(new MediaType(primaryType, subType)); return new ResponseEntity<>(document.getContent(), headers, HttpStatus.OK); }
@RequestMapping(value = "/savD", method = RequestMethod.POST) public ResponseEntity<Void> saveFile(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); Document document = new Document(); document.setName(file.getOriginalFilename()); document.setContent(bytes); documentService.save(document); } catch (Exception e) { } } else { } HttpHeaders headers = new HttpHeaders(); return new ResponseEntity<Void>(headers, HttpStatus.CREATED); }
@RequestMapping(value = "/upload", method = RequestMethod.POST) public ResponseEntity uploadFile(MultipartHttpServletRequest request) { try { Iterator<String> itr = request.getFileNames(); while (itr.hasNext()) { String uploadedFile = itr.next(); MultipartFile file = request.getFile(uploadedFile); String mimeType = file.getContentType(); String filename = file.getOriginalFilename(); byte[] bytes = file.getBytes(); Document document = new Document(); document.setContent(bytes); document.setName(filename); document.setMimeType(mimeType); documentService.save(document); } } catch (Exception e) { return new ResponseEntity<>("{}", HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity<>("{}", HttpStatus.OK); }