Загрузить файлы поместить в zip?
Мне нужно зарузить файлы, допустим мне надо принять несколько файлов file1,file2....,добавить их в список(List files = new ArrayList<>()),а после пройтись по списку и каждый добавить в zip,а этот zip конвертировать в формат MultipartFile и сохранить в базу)Помогите не могу понять как файлы засунуть в zip ?
@Controller
public class AttachmentController {
private AttachmentService attachmentService;
public AttachmentController(AttachmentService attachmentService) {
this.attachmentService = attachmentService;
}
@PostMapping("/upload")
public String uploadFile(@RequestParam String title, @RequestParam String author,@RequestParam("file")MultipartFile file1) throws Exception {
Attachment attachment = null;
attachment = attachmentService.saveAttachment(title,author,file1);
return "redirect:/";
}
@GetMapping("/download/{fileId}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) throws Exception {
Attachment attachment = null;
attachment = attachmentService.getAttachment(fileId);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(attachment.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + attachment.getFileName()
+ "\"")
.body(new ByteArrayResource(attachment.getData()));
}
Сервис
@Service
public class AttachmentServiceImpl implements AttachmentService{
private AttachmentRepository attachmentRepository;
public AttachmentServiceImpl(AttachmentRepository attachmentRepository) {
this.attachmentRepository = attachmentRepository;
}
@Override
public Attachment saveAttachment(String title,String author,MultipartFile file) throws Exception {
String fileName = StringUtils.cleanPath(file.getOriginalFilename());
try {
if(fileName.contains("..")) {
throw new Exception("Filename contains invalid path sequence "
+ fileName);
}
Attachment attachment
= new Attachment(title,author,fileName,
file.getContentType(),
file.getBytes());
return attachmentRepository.save(attachment);
} catch (Exception e) {
throw new Exception("Could not save File: " + fileName);
}
}
@Override
public Attachment getAttachment(String fileId) throws Exception {
return attachmentRepository
.findById(fileId)
.orElseThrow(
() -> new Exception("File not found with Id: " + fileId));
}