-----------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;
}
'java > 유틸' 카테고리의 다른 글
템플릿 다운로드. (0) | 2020.04.29 |
---|---|
zip utill (0) | 2020.03.02 |
[전자정부 프레임워크] 중복로그인 방지. (0) | 2019.12.15 |
Excel로 올라온 19리가 넘는 숫자데이터 controller에서 받기 (0) | 2019.12.15 |
싱글톤 패턴,인스턴스 ,클래스, 객체 (0) | 2019.02.21 |