{"id":1569,"date":"2011-12-27T01:22:32","date_gmt":"2011-12-27T01:22:32","guid":{"rendered":"http:\/\/www.gisdeveloper.co.kr\/?p=1569"},"modified":"2017-01-27T19:26:20","modified_gmt":"2017-01-27T10:26:20","slug":"java-url%ec%9d%84-%ed%86%b5%ed%95%9c-binary-%eb%8d%b0%ec%9d%b4%ed%84%b0-%eb%b0%9b%ea%b8%b0","status":"publish","type":"post","link":"http:\/\/www.gisdeveloper.co.kr\/?p=1569","title":{"rendered":"[Java] URL\uc744 \ud1b5\ud55c Binary \ub370\uc774\ud130 \ubc1b\uae30"},"content":{"rendered":"<p>\uc790\ubc14\uc5d0\uc11c \uc11c\ubc84\uac00 \uc81c\uacf5\ud558\ub294 \ubc14\uc774\ub108\ub9ac \ub370\uc774\ud130\ub97c \ubc1b\uc544 \uc800\uc7a5\ud574 \uc8fc\ub294 \ucf54\ub4dc\uc785\ub2c8\ub2e4. \ud544\uc694\ud560 \ub54c \ucc3e\uc544 \ubcf4\uae30 \uc27d\ub3c4\ub85d \uc815\ub9ac\ud574 \ubd05\ub2c8\ub2e4.<\/p>\n<pre>URL url;\r\ntry {\r\n    url = new URL(\"http:\/\/somewhere\/binary.zip\");\r\n  \r\n    URLConnection connection = url.openConnection();\r\n    int contentLength = connection.getContentLength();\r\n    if (contentLength > 0) {\r\n        InputStream raw = connection.getInputStream();\r\n        InputStream in = new BufferedInputStream(raw);\r\n        byte[] data = new byte[contentLength];\r\n        int bytesRead = 0;\r\n        int offset = 0;\r\n        while (offset < contentLength) {\r\n            bytesRead = in.read(data, offset, data.length - offset);\r\n            if (bytesRead == -1) break;\r\n            offset += bytesRead;\r\n        }\r\n        in.close();\r\n  \r\n        if (offset == contentLength) {\r\n            \/\/ \ubc14\uc774\ub108\ub9ac \ub370\uc774\ud130 \uc900\ube44 \uc644\ub8cc !\r\n            return true;\r\n        }\r\n    }\r\n} catch (MalformedURLException e) {\r\n    e.printStackTrace();\r\n} catch (IOException e) {\r\n    e.printStackTrace();\r\n}<\/pre>\n<p><strong><font color=\"#d41a01\">\uc704\uc758 \ucf54\ub4dc\ub294 \uc0ac\uc6a9\ud574 \ubcf4\ub2c8 \ubb38\uc81c\uac00 \uc788\uc2b5\ub2c8\ub2e4.<\/font><\/strong> \uc774\uc720\ub294 6\ubc88 \ucf54\ub4dc\ub97c \ud1b5\ud574 \uc11c\ubc84\ub85c\ubd80\ud130 \ubc1b\uc740 \ub370\uc774\ud130\uc758 \ud06c\uae30\uac00 \ud56d\uc0c1 \uc633\ubc14\ub974\uc9c0 \uc54a\ub2e4\ub294 \uac83\uc785\ub2c8\ub2e4. \uc608\ub97c \ub4e4\uc5b4 \ub370\uc774\ud130\uac00 \ud074 \uacbd\uc6b0.. -1\uc774 \ubc18\ud658\ub418\uae30\ub3c4 \ud569\ub2c8\ub2e4. \uc774\ub7ec\ud55c \uc774\uc720\ub85c \uc704\uc758 \ucf54\ub4dc\ub294 \uc0ac\uc6a9\ud558\uba74 \uc54a\ub418\uace0.. \uc544\ub798\uc758 \ucf54\ub4dc\ub97c \uc0ac\uc6a9\ud558\uc2dc\uae30 \ubc14\ub78d\ub2c8\ub2e4.<\/p>\n<pre>private boolean requestUrl(String urlString, ByteBuffer out) {\r\n    URL url;\r\n  \r\n    try {\r\n        url = new URL(urlString);\r\n        URLConnection connection = url.openConnection();\r\n        InputStream raw = connection.getInputStream();\r\n        InputStream in = new BufferedInputStream(raw);\r\n        byte[] chunk = new byte[1024];\r\n        int bytesRead = 0;\r\n\r\n        while((bytesRead = in.read(chunk)) != -1) {\r\n            out.put (chunk, 0, bytesRead);\r\n        }\r\n   \r\n        out.flip();\r\n        in.close();\r\n    } catch (MalformedURLException e) {\r\n        e.printStackTrace();\r\n    } catch (IOException e) {\r\n        e.printStackTrace();\r\n    }  \r\n    \r\n    return true;\r\n}<\/pre><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc790\ubc14\uc5d0\uc11c \uc11c\ubc84\uac00 \uc81c\uacf5\ud558\ub294 \ubc14\uc774\ub108\ub9ac \ub370\uc774\ud130\ub97c \ubc1b\uc544 \uc800\uc7a5\ud574 \uc8fc\ub294 \ucf54\ub4dc\uc785\ub2c8\ub2e4. \ud544\uc694\ud560 \ub54c \ucc3e\uc544 \ubcf4\uae30 \uc27d\ub3c4\ub85d \uc815\ub9ac\ud574 \ubd05\ub2c8\ub2e4. URL url; try { url = new URL(&#8220;http:\/\/somewhere\/binary.zip&#8221;); URLConnection connection = url.openConnection(); int contentLength = connection.getContentLength(); if (contentLength > 0) { InputStream raw = connection.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int bytesRead = 0; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.gisdeveloper.co.kr\/?p=1569\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;[Java] URL\uc744 \ud1b5\ud55c Binary \ub370\uc774\ud130 \ubc1b\uae30&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[51],"class_list":["post-1569","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/1569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1569"}],"version-history":[{"count":1,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":3107,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/1569\/revisions\/3107"}],"wp:attachment":[{"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gisdeveloper.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}