Сериализовать и сохранить на диск, потом загрузить и десериализовать Java
Ничего дельного не могу найти, буду рад ссылке на ресурс или подсказке.
Я имею объект, который при нажатии на кнопку (Spring MVC) нужно сериализовать и сохранить на компьютере пользователя. При нажатии на другую кнопку нужно выбрать файл из компьютера и снова превратить его в объект.
Вот фрагмент кода с сайта https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/
Главный вопрос: каким образом и где мне создать и вставить мой файл?
@GetMapping("/downloadFile/{fileName:.+}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// Load file as Resource
Resource resource = fileStorageService.loadFileAsResource(fileName);
// Try to determine file's content type
String contentType = null;
try {
contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
} catch (IOException ex) {
logger.info("Could not determine file type.");
}
// Fallback to the default content type if type could not be determined
if(contentType == null) {
contentType = "application/octet-stream";
}
public Resource loadFileAsResource(String fileName) {
try {
Path filePath = this.fileStorageLocation.resolve(fileName).normalize();
Resource resource = new UrlResource(filePath.toUri());
if(resource.exists()) {
return resource;
} else {
throw new MyFileNotFoundException("File not found " + fileName);
}
} catch (MalformedURLException ex) {
throw new MyFileNotFoundException("File not found " + fileName, ex);
}
}
Заранее спасибо