'How do I Access files in Resource sub directory folder?
When files are being saved , It creates a folder in the resource directory with the ID of the customer and save all files for the customer id inside the sub directory in the resource folder . When I try to call the file using its name parameter I get a file not found response but when I removed the file from the sub directory and place it directly in the Resource folder I get the file . The files needed to be stored in a sub directory with an ID . How do I go about it
This is the file upload controller code
@PostMapping(value="/{id}/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
public Customer uploadDocs(@ModelAttribute("customer") Customer customer,
@RequestParam(name="file", required = false) MultipartFile multipartFormfile,
@RequestParam(name="file1", required = false) MultipartFile multipartDocfile,
@RequestParam(name="file2", required = false) MultipartFile multipartAdditionalfile,
@PathVariable Long id) throws java.io.IOException, IOException {
String formFile = StringUtils.cleanPath(multipartFormfile.getOriginalFilename());
String docFile = StringUtils.cleanPath(multipartDocfile.getOriginalFilename());
String additionalImage = StringUtils.cleanPath(multipartAdditionalfile.getOriginalFilename());
customer.setFormFile(formFile);
customer.setDocFile(docFile);
customer.setAdditionalImage(additionalImage);
customerAccountRepository.save(customer);
String uploadDir = "customer-photos/" + id;
FileUploadUtil.saveFile(uploadDir, formFile, multipartFormfile);
FileUploadUtil.saveFile(uploadDir, docFile, multipartDocfile);
FileUploadUtil.saveFile(uploadDir, additionalImage, multipartAdditionalfile);
return customer;
}
To call the file , this is the method in the Service Layer
@Service
public class CustomerAccountService {
private Path fileStorageLocation ;
String uploadDir = "customer-photos/" ;
public CustomerAccountService() throws Exception { this.fileStorageLocation =
Paths.get(uploadDir) .toAbsolutePath().normalize();
try { Files.createDirectories(this.fileStorageLocation); } catch (Exception
ex) { throw new
Exception("Could not create the directory where the uploaded files will be stored."
, ex); } }
public Resource loadFileAsResource(String fileName) throws Exception {
Path filePath = fileStorageLocation.resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
if(resource.exists()) {
return resource;
}else {
throw new Exception("File not found " + fileName);
}
}
This is the controller method to get the file using file name
@GetMapping(value="/files/{fileName:.+}")
public ResponseEntity<Resource> getFileFromResource(@PathVariable String fileName,
HttpServletRequest request) throws Exception {
Resource resource = customerRepo.loadFileAsResource(fileName);
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ex) {
logger.info("Could not determine file type.");
}
if(contentType == null) {
contentType = "application/octet-stream";
}
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
// .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
