OWL ITS + 탐지시스템(인터넷 진흥원)
src/main/java/kr/wisestone/owl/util/CommonUtil.java
@@ -6,6 +6,7 @@
import com.google.gson.Gson;
import kr.wisestone.owl.domain.enumType.FileType;
import kr.wisestone.owl.type.LikeType;
import kr.wisestone.owl.vo.DepartmentVo;
import kr.wisestone.owl.vo.UserVo;
import org.apache.commons.codec.binary.*;
import org.apache.commons.codec.binary.Base64;
@@ -39,7 +40,10 @@
import java.security.MessageDigest;
import java.security.spec.KeySpec;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommonUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonUtil.class);
@@ -343,6 +347,24 @@
        return fileMap;
    }
    //  string file 정보를 file Map 형태로 변경한다.
    public static Map<String, Object> makeFileMap(String fileName, String file, String contentType) {
        Map<String, Object> fileMap = new HashMap<>();
        try {
            byte[] bytes = Base64.decodeBase64(file);
            fileMap.put("fileName", fileName);
            fileMap.put("fileSize", bytes.length);
            fileMap.put("contentType", contentType);
            fileMap.put("file", CommonUtil.bytesToFile(fileName, bytes));
        } catch (Exception e) {
            LOGGER.debug(e.getMessage());
        }
        return fileMap;
    }
    public static String getPostDataString(Map<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
@@ -372,6 +394,42 @@
            multipart.transferTo(convertFile);
        } catch (IllegalStateException | IOException e) {
            LOGGER.debug("multipart 파일 file 변환 오류");
        }
        return convertFile;
    }
    // string을 파일로 변환
    public static File stringToFile(String fileName, String file) {
        byte[] bytes = null;
        try {
            bytes = Base64.decodeBase64(file);
        } catch (Exception ex) {
            LOGGER.debug("string to bytes 변환 오류");
        }
        if (bytes != null) {
            return bytesToFile(fileName, bytes);
        }
        return  null;
    }
    // bytes를 파일로 변환
    public static File bytesToFile(String fileName, byte[] bytes) {
        File convertFile = new File(WebAppUtil.getContextRealPath() + TMP_UPLOAD_FOLDER + getFileNameByUUID(fileName));
        if (!convertFile.exists()) {
            convertFile.mkdirs();
        }
        try{
            FileOutputStream lFileOutputStream = new FileOutputStream(convertFile);
            lFileOutputStream.write(bytes);
            lFileOutputStream.close();
        }catch (IllegalStateException | IOException e) {
            LOGGER.debug("bytes 파일 file 변환 오류");
        }
        return convertFile;
@@ -690,6 +748,36 @@
        return stringBuilder.toString();
    }
    //  DepartmentVos 에서 부서 정보를 추출해서 문자열로 리턴해준다. - 주로 엑셀 download 에서 사용된다.
    public static String convertDepartmentVosToString(List<DepartmentVo> departmentVos) {
        StringBuilder stringBuilder = new StringBuilder();
        int count = 0;
        for (DepartmentVo departmentVo : departmentVos) {
            if (count > 0) {
                stringBuilder.append("\n");
            }
            stringBuilder.append(departmentVo.getDepartmentName());
            count++;
        }
        return stringBuilder.toString();
    }
    public static List<Date> findSearchPeriod(Date startDate, Date endDate) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(endDate);
        List<Date> days = Lists.newArrayList();
        //  이번달 날짜 리스트 가져오기
        while (cal.getTime().after(startDate)){
            days.add(cal.getTime());
            cal.add(Calendar.DATE, -1);
        }
        return days;
    }
    //  검색 일자를 구한다.
    public static List<Date> findSearchPeriod(String searchPeriod) {
        List<Date> searchDates = Lists.newArrayList();
@@ -919,4 +1007,30 @@
        }
    }
    // 메인 url만 추출
    public static String extractUrl(String content){
        try {
            String REGEX = "(http(s)?:\\/\\/)([a-z0-9\\w]+\\.*)+[a-z0-9]{2,4}";
            Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
            Matcher m = p.matcher(content);
            if (m.find()) {
                return m.group();
            }
            return "";
        } catch (Exception e) {
            return "";
        }
    }
    // 메인 url 추출을 위해 http: 확인
    public static String getUrl(String fullUrl) {
        if (fullUrl != null) {
            if (fullUrl.indexOf("http") == -1) {
                fullUrl = "http://" + fullUrl;
            }
            return extractUrl(fullUrl);
        }
        return "";
    }
}