파일 크기 단위 출력 설정

파일명 가져올 시 디코딩

파일 저장 및 디렉토리 생성

Path uploadPath = Paths.get("C:/temp"); // 저장 경로 설정
Files.createDirectories(uploadPath);
String file = "파일명";
Path targetPath = uploadPath.resolve(file);
MultipartFile mf = mf.transferTo(targetPath.toFile());

파일 삭제 및 이동

// 파일 삭제
Path targetPath = Paths.get("C:/temp/asd.png");
Files.deleteIfExists(targetPath);

-------------

// 파일 이동
try (DirectoryStream<Path> stream = Files.newDirectoryStream("C:/temp")) {
	for (Path file : stream) {
		Path targetPath = taskFolder.resolve(file.getFileName()); // 파일 옮길 폴더 위치
		Files.move(file, targetPath, StandardCopyOption.REPLACE_EXISTING); // 옮기기
	}
	Files.deleteIfExists(tempFolder); // temp에 남은 폴더 삭제
} catch (IOException e) {
	System.out.println("Temp 폴더 못찾음");
}

파일 자동 삭제

@Component
public class TempCleaner {
	
	private final String WIN_TEMP_DIR = "C:/WorkFlow/";
	
	// 자동 삭제 시간 설정
	@Scheduled(fixedRate = 12 * 60 * 60 * 1000) // ms 단위12 * 60 * 60 * 1000
	public void cleanOldFiles() {
		
		Path path = Paths.get(WIN_TEMP_DIR, "temp");
		
		Instant cutOff = Instant.now().minusSeconds(12 * 60 * 60); // 초 단위12 * 60 * 60
		
		if (!Files.exists(path) || !Files.isDirectory(path)) {
			System.out.println("temp가 없음");
			return;
		}
		
		// 경로 안에 있는 모든 항목(폴더/파일) 처리하기 위해 디렉토리 스트림 열기
		try(DirectoryStream<Path> stream = Files.newDirectoryStream(path)){
			// 디렉토리 스트림 안의 각각 항목 반복
			for (Path folder : stream) {
				// 현재 항목이 폴더인지 확인
				if(Files.isDirectory(folder)) {
					try {
						// 폴더 속성 확인 : 생성 시간
						BasicFileAttributes attr = Files.readAttributes(folder, BasicFileAttributes.class);
						Instant creationTime = attr.creationTime().toInstant();
						
						// 현재 시간 기준 12시간 이상된 폴더
						if(creationTime.isBefore(cutOff)) {
							// 폴더 안의 파일과 하위 폴더를 탐색
							Files.walk(folder)
							// 하위부터 삭제
							.sorted((a, b) -> b.compareTo(a))
							// 각각 파일 폴더 삭제 시도
							.forEach(path1 -> {
								try {
									Files.deleteIfExists(path1);
									System.out.println("삭제 : " + path1);
								}catch(IOException e) {
									System.out.println("실패 : " + path1);
								}
							});
						}
					} catch(IOException e) {
						System.out.println("폴더 속성 읽기 실패 : " + folder);
					}
				}
			}
		}catch(IOException e) {
			System.out.println("Temp 폴더 탐색 실패 : " + path);
		}
		
	}

}