caTea 블로그

http 데이터 송수신 클라이언트단 본문

spring(java)

http 데이터 송수신 클라이언트단

ZaRas 2016. 1. 29. 09:30
반응형

protected <T, V> T process(URL url, boolean keepAilve , RequestMethod method, V param, class<T> clazz) throws IOException {

InoutStream is = null;
OutputStram os = null;
ReadableByteChannel rbc = null;

try{
HttpURLConnection conn= (HttpURLConnection) url.openConnection;

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod(method.name());
if(keepAilve){
conn.setTequestProperty("Connection", "keep-alive");
} else {
conn.setTequestProperty("Connection", "close");
}
conn.setReadTimeout(30000);

swith(method){
case get:{
break;

}
case POST : {
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
break;
}

ObjectMapper om = new ObjectMapper();
os = conn.getOutputStram();
os.write(om.writeValueAsBytes(params));

is = conn.getInputStream();
rbc = Channels.newChannel(is);
int size = is.available();

ByteBuffer buffer = ByteBuffer.allocate(size);
rbc.read(buffer);
buffer.filp();

rbc.close();
is.close();
os.close();

if(conn != null){
conn.disconnection();
}

ObjectReader or = om.reader(clazz);
return or.readValue(buffer.array());
} finally {
if (rbc != null){
try{rbc.close();}catch(Exception e){}
}

if (os != null){
try{os.close();}catch(Exception e){}
}
if (is != null){
try{is.close();}catch(Exception e){}
}
}

}

728x90

'spring(java)' 카테고리의 다른 글

바코드 생성  (0) 2016.05.17
html convert pdf (html문서를 pdf 파일로 변환)  (0) 2016.05.11
java mail sand example  (0) 2015.09.10
java excel paser example  (0) 2015.09.10
apache ftpclient example  (0) 2015.09.10