Java
- encoding 별로 JAVA 문자열 byte단위로 자르기 2018.08.13
- HttpsURLConnection 으로 Http통신할 때 오류 메세지 잡아내기 2018.05.30
- 클래스로더 오류 관련 2018.02.19
encoding 별로 JAVA 문자열 byte단위로 자르기
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
'Java' 카테고리의 다른 글
HttpsURLConnection 으로 Http통신할 때 오류 메세지 잡아내기 (0) | 2018.05.30 |
---|---|
클래스로더 오류 관련 (0) | 2018.02.19 |
HttpsURLConnection 으로 Http통신할 때 오류 메세지 잡아내기
#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 |
클래스로더 오류 관련
오류 :
가끔씩 uncompiled jsp 오류가 나올 때가 있다.
case 1 :
jsp 파일은 계속 재컴파일되지만 가끔씩 누락되는 경우도 있다. ex) svn 원격 scp 이동시
case 2 :
was 기동 시에 jsp/java 파일을 클래스로더가 컴파일해서 class파일로 메모리에 올려 놓게 되는데
was를 내렸다가 올리더라도 클래스로더가 캐쉬메모리에 올려놓고 이미 캐쉬메모리에 있으니 다시 메모리에 올려놓지 않는 경우가 가끔 있다.
해결책 :
이때에는 문제가 되는 jsp/java의 class파일 삭제 후 재컴파일을 유도하면 해결되는 경우가 있다.
결론 :
클래스로더 문제라고 판단되면 해당 class파일 삭제 후 was를 재기동시켜 재컴파일을 유도 한다.
'Java' 카테고리의 다른 글
encoding 별로 JAVA 문자열 byte단위로 자르기 (0) | 2018.08.13 |
---|---|
HttpsURLConnection 으로 Http통신할 때 오류 메세지 잡아내기 (0) | 2018.05.30 |