개발Story
Published 2020. 3. 2. 17:10
File 저장하기. java/유틸

-----------controller-------------

<java />
File convFile = null; convFile = new file("파일 디렉토리"); Utill.saveFile("convFile","업로드 경로","저장파일명")

-----------utill---------------------

 

<java />
public static void saveFile(File origin, String saveDirPath, String fileName) throws IOException { saveDirPath = dirPathCheck(saveDirPath); //originFilePath = dirPathCheck(originFilePath); // 원본 파일 //File origin = new File(originFilePath + fileName); // 저장 경로 체크 dirCheck(saveDirPath); // 복사 파일 객체 생성 File copyFile = new File(saveDirPath + fileName); // 파일 복사 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(origin)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(copyFile)); int read; byte[] data = new byte[2048]; while((read = bis.read(data, 0, 2048)) != -1) { bos.write(data, 0, read); } bos.flush(); bos.close(); bis.close(); } /** * 디렉토리 String 마지막에 '/' 를 확인하고 없으면 추가한다 * @param dirPath * @return */ private static String dirPathCheck(String dirPath) { if(!(dirPath.charAt(dirPath.length() -1) == '/')) { dirPath += "/"; } return dirPath; }

 

profile

개발Story

@슬래기

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!