주로 많이 사용하는 utils들
package egovframework.cts.utl.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.ZipInputStream;
public class CommonUtils {
public static String leftOfR(String str, String delimeter) {
int ndx = str.lastIndexOf(delimeter);
if (ndx == -1) {
return "";
}
else {
return str.substring(0,ndx); // ndx위치 제외
}
}
public static String rightOfR(String str, String delimeter) {
int ndx = str.lastIndexOf(delimeter);
if (ndx == -1) {
return "";
}
else {
return str.substring(ndx+delimeter.length(),str.length());
}
}
public static boolean makePath(String filePath) {
try {
Path path = Paths.get(filePath);
Files.createDirectories(path.getParent()); // 폴더 없으면 생성.
return true;
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
return false;
}
}
public static boolean fileExist(String fileName) {
boolean bExist = false;
System.out.println("fileExist.fileName:"+fileName);
bExist = new File(fileName).isFile();
System.out.println("fileExist:"+bExist);
return bExist;
}
public static void createFile(File file, ZipInputStream zis) throws Throwable {
System.out.println("createFile");
int MAX_SIZE = 2048000;
// 디렉토리 확인
File parentDir = new File(file.getParent());
System.out.println("parentDir : "+parentDir);
// 디렉토리가 없으면 생성하자
if(!parentDir.exists()){
parentDir.mkdirs();
}
// 파일 스트림 선언
FileOutputStream fos = null;
try{
fos = new FileOutputStream(file);
byte[] buffer = new byte[MAX_SIZE];
int size = 0;
// Zip스트림으로부터 byte뽑아내기
while ((size = zis.read(buffer)) > 0) {
// byte로 파일 만들기
fos.write(buffer, 0, size);
}
}catch(Throwable e){
System.out.println(e.getMessage());
}finally{
if(fos != null){
try{
fos.close();
}catch (IOException e) {
}
}
}
}
}
압축, 압축 풀기도 파일 정리해서 utils로 정리하는 게 좋을 듯
압축풀기를 할때 createFile을 호출한다.
'03.업 > 01.자바' 카테고리의 다른 글
Map을 queryString으로 변환하기 (0) | 2023.11.08 |
---|---|
[펌]Spring Profile 활용 Application.properties (1) | 2023.11.01 |
svg파일 읽어오기 (0) | 2023.07.04 |
zip파일 header 로 전송 및 요청 사이트에서 파일 받기 (0) | 2023.06.22 |
java로 여러 파일들 zip파일로 압축 및 압출 풀기 (0) | 2023.06.22 |