source: src/main/java/it/finki/charitable/util/FileUploadUtil.java@ 5306751

Last change on this file since 5306751 was 3fc9e50, checked in by KostaFortumanov <kfortumanov@…>, 3 years ago

prototip part1

  • Property mode set to 100644
File size: 954 bytes
Line 
1package it.finki.charitable.util;
2
3import org.springframework.web.multipart.MultipartFile;
4
5import java.io.IOException;
6import java.io.InputStream;
7import java.nio.file.Files;
8import java.nio.file.Path;
9import java.nio.file.Paths;
10import java.nio.file.StandardCopyOption;
11
12public class FileUploadUtil {
13
14 public static void saveFile(String uploadDir, String fileName,
15 MultipartFile multipartFile) throws IOException {
16 Path uploadPath = Paths.get(uploadDir);
17
18 if (!Files.exists(uploadPath)) {
19 Files.createDirectories(uploadPath);
20 }
21
22 try (InputStream inputStream = multipartFile.getInputStream()) {
23 Path filePath = uploadPath.resolve(fileName);
24 Files.copy(inputStream, filePath, StandardCopyOption.REPLACE_EXISTING);
25 } catch (IOException ioe) {
26 throw new IOException("Could not save image file: " + fileName, ioe);
27 }
28 }
29}
Note: See TracBrowser for help on using the repository browser.