이전에 사용했던 만들어 두었던 GetHttp 클래스를 이용한다.
html을 읽을때 line단위로 읽었던 부분을 전체 영역을 읽도록 변경하였다.
if( resCode == HttpURLConnection.HTTP_OK ) { htmlByte = inputStreamToByte(conn.getInputStream()); if(htmlByte == null || htmlByte.length == 0){ errorCode = ERROR_CODE_HTTP_ELSE; retval = false; } }
byte단위로 읽도록 변경하였고 stream을 byte로 변경하는 함수를 추가하였다.
private byte[] inputStreamToByte(InputStream in) { final int BUF_SIZE = 1024; ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[BUF_SIZE]; try { int length; while ((length = in.read(buffer)) != -1) out.write(buffer, 0, length); } catch (IOException e) { e.printStackTrace(); return null; } return out.toByteArray(); }
전체함수의 소스 코드이다.
public class GetHttp { final static double VERSION = 1.20141016; static final int ERROR_CODE_HTTP_NOT_FOUND = -404; static final int ERROR_CODE_HTTP_UNAUTHORIZED = -401; static final int ERROR_CODE_HTTP_ELSE = -1; static final int ERROR_CODE_HTTP_EXCEPTION = -1000; static final int ERROR_CODE_NOERROR = 0; byte[] htmlByte = null; int errorCode = 0; boolean bUseCache = true; public String getString() { try { return new String(htmlByte, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; } public byte[] getByte() { return htmlByte; } public int getErrorCode() { return errorCode; } public boolean execute(String addr) { return execute(addr, 5, 10); } // 참고 소스:http://markan82.tistory.com/32 public boolean execute(String addr, int connTimeOutSec, int readTimeOutSec) { boolean retval = true; errorCode = ERROR_CODE_NOERROR; try { URL url = new URL(addr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); if( conn != null ) { conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); conn.setConnectTimeout(1000*connTimeOutSec); conn.setReadTimeout(1000*readTimeOutSec); conn.setUseCaches(bUseCache); int resCode = conn.getResponseCode(); if( resCode == HttpURLConnection.HTTP_OK ) { htmlByte = inputStreamToByte(conn.getInputStream()); if(htmlByte == null || htmlByte.length == 0){ errorCode = ERROR_CODE_HTTP_ELSE; retval = false; } } else if( resCode == HttpURLConnection.HTTP_NOT_FOUND ) { errorCode = ERROR_CODE_HTTP_NOT_FOUND; retval = false; } else if( resCode == HttpURLConnection.HTTP_UNAUTHORIZED ) { errorCode = ERROR_CODE_HTTP_UNAUTHORIZED; retval = false; } else { errorCode = ERROR_CODE_HTTP_ELSE; retval = false; } // DISCONNECT conn.disconnect(); } else { errorCode = ERROR_CODE_HTTP_ELSE; retval = false; } } catch(Exception e) { errorCode = ERROR_CODE_HTTP_EXCEPTION; retval = false; } return retval; } private byte[] inputStreamToByte(InputStream in) { final int BUF_SIZE = 1024; ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[BUF_SIZE]; try { int length; while ((length = in.read(buffer)) != -1) out.write(buffer, 0, length); } catch (IOException e) { e.printStackTrace(); return null; } return out.toByteArray(); } }
아래는 위 함수를 테스트하기 위한 소스이다.
실행하면 D드라이브 루트에 파일이 생성된다.
public class TestMain { public static void main(String[] args) { // 읽어오는예제 GetHttp html = new GetHttp(); html.execute("http://img.s-msn.com/tenant/amp/entityid/BB9oYEf.img?h=187&w=300&m=6&q=60&u=t&o=t&l=f&f=jpg"); Util.fileWriteByte("D:\\test.jpg",html.getByte()); html.execute("http://www.google.co.kr/"); Util.fileWriteByte("D:\\test1-1.html",html.getByte()); Util.fileWriteString("D:\\test1-2.html",html.getString()); } }마지막으로 frileWrite관련 함수가 빠졌다.
public class Util { public static boolean fileWriteByte(String path, byte[] data) { try { //////////////////////////////////////////////////////////////// FileOutputStream out = new FileOutputStream(path); out.write(data); out.close(); //////////////////////////////////////////////////////////////// } catch (IOException e) { return false; } return true; } public static boolean fileWriteString(String path, String data) { try { //////////////////////////////////////////////////////////////// BufferedWriter out = new BufferedWriter(new FileWriter(path)); out.write(data); out.close(); //////////////////////////////////////////////////////////////// } catch (IOException e) { return false; } return true; } }
댓글 없음:
댓글 쓰기