public String[] parseStringByBytes(String raw, int len, String encoding) {

 if (raw == null)
  return null;
 String[] ary = null;

 try {
  // raw 의 byte
  byte[] rawBytes = raw.getBytes(encoding);
  int rawLength = rawBytes.length;

  int index = 0;
  int minus_byte_num = 0;
  int offset = 0;

  int hangul_byte_num = encoding.equals("UTF-8") ? 3 : 2;

  if (rawLength > len) {
   int aryLength = (rawLength / len) + (rawLength % len != 0 ? 1 : 0);
   ary = new String[aryLength];

   for (int i = 0; i < aryLength; i++) {
    minus_byte_num = 0;
    offset = len;

    if (index + offset > rawBytes.length) {
     offset = rawBytes.length - index;
    }

    for (int j = 0; j < offset; j++) {

     if (((int) rawBytes[index + j] & 0x80) != 0) {
      minus_byte_num++;
     }
    }

    if (minus_byte_num % hangul_byte_num != 0) {
     offset -= minus_byte_num % hangul_byte_num;
    }

    ary[i] = new String(rawBytes, index, offset, encoding);
    index += offset;
   }
  } else {
   ary = new String[] { raw };
  }
 } catch (Exception e) {

 }
 return ary;
}

 

 

출처 : http://blog.naver.com/PostView.nhn?blogId=nackhwa7&logNo=140103352320

#URL호출 서버에서 response 오류 발생하였을 때, con.getInputStream()에서 IOException이 발생 : 뿌려주는 오류 메세지를 잡지 못함

HttpsURLConnection con = (HttpsURLConnection)connectUrl.openConnection();

.....

InputStreamReader ir     = new InputStreamReader(con.getInputStream(), charSet);




# 이를 해결하고자 오류가 발생했을 경우(con.getResponseCode()가 400 이상일 경우) con.getErrorStream()으로 받는다.

HttpsURLConnection con = (HttpsURLConnection)connectUrl.openConnection();

....

// SERVER ERROR CHECK

if(con.getResponseCode() < HttpsURLConnection.HTTP_BAD_REQUEST){
/* not error from server : con.getInputStream() */
ir = new InputStreamReader(con.getInputStream(), charSet);
postRes = new BufferedReader(ir);
while ((resultJson = postRes.readLine()) != null){
resultBuffer.append(resultJson);
}
con.disconnect();
}else{
/* error from server : con.getErrorStream() */
ir = new InputStreamReader(con.getErrorStream(), charSet);
postRes = new BufferedReader(ir);
while ((resultJson = postRes.readLine()) != null){
resultBuffer.append(resultJson);
}
con.disconnect();
/* Exception 던지지 않으면 오류가 났음에도 불구하고 로직이 계속 진행되므로 Exception 던져서 오류가 났다는 것을 전달한다. */
throw new Exception(resultBuffer.toString());
}



어떤 오류가 발생 했는지 로그로 출력된다.

이 때 주의할 점은 오류가 발생했을 때 Exception을 반드시 던져주어야 한다. 만약 Exception을 던지지 않는다면 오류가 났음에도 불구하고 로직이 계속 진행되므로 오류가 났다는 것을 전달해주어야 한다.


'Java' 카테고리의 다른 글

encoding 별로 JAVA 문자열 byte단위로 자르기  (0) 2018.08.13
클래스로더 오류 관련  (0) 2018.02.19

http://www.abc.com 이하 "abc"에서 http://www.xyz.com 이하 "xyz"을 window.open(); 형태로 팝업을 호출한 후

xyz(자식)에서 abc(부모)의 함수를 호출 하거나 파라미터에 접근하려고 할 경우


크로스도메인 보안 이슈로 opener로 접근을 하지 못한다. (팝업 호출 시 도메인주소가 달라져서 opener객체를 잃어버림)


오류로그 : Uncaught DOMException: Blocked a frame with origin "http://www.xyz.com" from accessing a cross-origin frame.


이 때 해결방법이 2가지가 있다.



1. postMessage를 사용할 수 있다.(이 경우 IE 8 부터 지원된다)


2. iframe을 사용하여 abc(부모)아래에 xyz(자식)의 jsp호출 후 호출한 iframe영역에서 팝업창을 띄우면 된다. (팝업 창에서는 opener.parent.으로 접근하면 된다)


예제 소스 정리는 나중에 해서 업로드 하자.......! 지금은 바쁘다!

+ Recent posts