{"id":4623,"date":"2019-09-14T22:02:53","date_gmt":"2019-09-14T14:02:53","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4623"},"modified":"2019-09-14T22:02:53","modified_gmt":"2019-09-14T14:02:53","slug":"%e5%a6%82%e4%bd%95%e7%94%a8java%e4%b8%8b%e8%bd%bdbinary%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4623.html","title":{"rendered":"\u5982\u4f55\u7528Java\u4e0b\u8f7dbinary\u6587\u4ef6"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u672c\u6587\u4e3b\u8981\u662f\u5bf9\u4e4b\u524d\u8bb0\u5f55\u7684\u300c<a href=\"https:\/\/ixyzero.com\/blog\/archives\/4409.html\">Java\u4e2d\u5982\u4f55\u53d1\u8d77HTTP\u8bf7\u6c42<\/a>\u300d\u4e00\u6587\u7684\u8865\u5145\/\u4fee\u6b63\uff0c\u5f53\u65f6\u53ea\u8003\u8651\u4e86\u7528Java\u53d1\u8d77GET\/POST\u8bf7\u6c42\uff0c\u7136\u540e\u8bfb\u53d6\u8fd4\u56de\u7684\u6587\u672c\u683c\u5f0f\u7684response\u4fe1\u606f\uff0c\u5728\u4e0b\u8f7d\u6587\u672c\u683c\u5f0f\u7684\u6587\u4ef6\u65f6\u6ca1\u5565\u95ee\u9898\uff0c\u4f46\u662f\u5bf9\u4e8ebinary\u7b49\u975e\u6587\u672c\u683c\u5f0f\u7684\u6587\u4ef6\u65f6\u5c31\u6709\u95ee\u9898\u4e86\uff0c\u8fd9\u91cc\u8bb0\u5f55\u4e00\u4e0b\u89e3\u51b3\u529e\u6cd5\u4ee5\u53ca\u4ea7\u751f\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\"><strong>\u5f53\u4f60\u60f3\u4e0b\u8f7d\u4e00\u4e2a\u4e8c\u8fdb\u5236\u6587\u4ef6\u65f6\uff0c\u4e0d\u8981\u5c06 InputStream \u5d4c\u5165\u4efb\u4f55\u7684 Reader \u7c7b\u578b\u53d8\u91cf\u4e2d\uff0c\u800c\u5e94\u8be5\u662f stream \u683c\u5f0f\u8bfb\u5165\uff0c\uff08\u53ef\u4ee5\u501f\u52a9\u4e8e\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\u4f5c\u4e3a\u7f13\u51b2\u533a\uff09\u7136\u540e stream \u683c\u5f0f\u5199\u51fa\u3002<\/strong><\/p>\n\n\n\n<p>\u5982\u679c\u4f60\u5728\u8fd9\u4e2d\u95f4\u4f7f\u7528\u4e86\u4efb\u4f55\u7c7b\u578b\u7684 Reader \u53d8\u91cf\uff0c\u5c06\u5185\u5bb9\u5148\u8f6c\u6362\u6210\u4e86 \u5b57\u7b26\/\u5b57\u7b26\u4e32 \u518d\u5199\u51fa\u81f3\u6587\u4ef6\uff0c\uff08\u5927\u6982\u7387\uff09\u5c31\u4f1a\u53d7\u5230\u5e73\u53f0\u7f16\u7801\u65b9\u5f0f\u7684\u5f71\u54cd\uff0c\u6700\u540e\u7684\u7ed3\u679c\u53ef\u80fd\u5c31\u548c\u4f60\u9884\u671f\u7684\u4e0d\u4e00\u81f4\u4e86\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>If you are trying to read a binary stream, you should NOT wrap the InputStream in a Reader of any kind.Read the data into a byte array buffer using the InputStream.read(byte[], int, int) method. Then write from the buffer to a FileOutputStream.<\/p><\/blockquote>\n\n\n\n<p>\u4f7f\u7528&nbsp;HttpClient \u548c&nbsp;FileUtils.copyInputStreamToFile \u4ee5\u53ca FileOutputStream \u7684\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package com.ixyzero.learn.utils;<br><br>import org.apache.commons.io.FileUtils;<br>import org.apache.http.HttpResponse;<br>import org.apache.http.client.HttpClient;<br>import org.apache.http.client.methods.HttpGet;<br>import org.apache.http.impl.client.HttpClientBuilder;<br><br>import java.io.*;<br>import java.net.HttpURLConnection;<br>import java.net.MalformedURLException;<br>import java.net.ProtocolException;<br>import java.net.URL;<br><br>\/**<br> * Created by ixyzero on 2019\/9\/14.<br> *\/<br>public class DownloadBinaryFile {<br><br>    \/\/ HttpURLConnection download from remote url to local file<br>    public static void httpGetBinary2(String url, File file) {<br>        URL obj = null;<br>        HttpURLConnection con = null;<br>        try {<br>            obj = new URL(url);<br>            con = (HttpURLConnection) obj.openConnection();<br><br>            con.setRequestMethod(\"GET\");<br>            con.setRequestProperty(\"User-Agent\", \"Mozilla\/5.0\");<br><br>            int responseCode = con.getResponseCode();<br>            System.out.println(\"\\nSending 'GET' request to URL : \" + url);<br>            System.out.println(\"Response Code : \" + responseCode);<br><br>            InputStream inputStream = con.getInputStream();<br>            byte[] buffer = new byte[4096];<br>            int n;<br><br>            OutputStream output = new FileOutputStream( file );<br>            while ((n = inputStream.read(buffer)) != -1) {<br>                output.write(buffer, 0, n);<br>            }<br>            output.close();<br><br>        } catch (MalformedURLException e) {<br>            e.printStackTrace();<br>        } catch (ProtocolException e) {<br>            e.printStackTrace();<br>        } catch (IOException e) {<br>            e.printStackTrace();<br>        }<br><br>    }<br><br>    \/\/ HttpClient download from remote url to local file<br>    public static void httpGetBinary(String url, File file) {<br>        HttpClient client = HttpClientBuilder.create().build();<br>        \/\/ code below adds HTTP REDIRECT support to GET and POST methods<br>        \/\/ HttpClient client = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();<br><br>        HttpGet request = new HttpGet(url);<br>        request.addHeader(\"User-Agent\", \"Mozilla\/5.0\");<br><br>        try {<br>            HttpResponse response = client.execute(request);<br>            System.out.println(\"Response Code : \" + response.getStatusLine().getStatusCode());<br><br>            InputStream inputStream = response.getEntity().getContent();<br><br>            FileUtils.copyInputStreamToFile(inputStream, file);<br>        } catch (IOException e) {<br>            e.printStackTrace();<br>        } finally {<br>            \/\/<br>        }<br><br>    }<br><br>    public static void main(String[] args) {<br>        DownloadBinaryFile downloader = new DownloadBinaryFile();<br>        String url1 = \"https:\/\/www.cnblogs.com\/images\/logo_small.gif\";<br>        String filepath1 = \"\/path\/to\/logo.gif\";<br>        String filepath2 = \"\/path\/to\/logo2.gif\";<br><br>        downloader.httpGetBinary(url1, new File(filepath1));<br>        downloader.httpGetBinary2(url1, new File(filepath2));<br>    }<br>}<\/pre>\n\n\n\n<h5 class=\"wp-block-heading\" id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li>How do I use HttpClient in Java to retrieve a binary file?<br><a href=\"https:\/\/stackoverflow.com\/questions\/6660243\/how-do-i-use-httpclient-in-java-to-retrieve-a-binary-file\">https:\/\/stackoverflow.com\/questions\/6660243\/how-do-i-use-httpclient-in-java-to-retrieve-a-binary-file<\/a><\/li><li><strong>Ultimate Guide for Reading Files in Java<\/strong><br><a href=\"https:\/\/funnelgarden.com\/java_read_file\/\">https:\/\/funnelgarden.com\/java_read_file\/<\/a><\/li><li>Reading binary file from URLConnection<br><a href=\"https:\/\/stackoverflow.com\/questions\/3221979\/reading-binary-file-from-urlconnection\">https:\/\/stackoverflow.com\/questions\/3221979\/reading-binary-file-from-urlconnection<\/a><\/li><li>Write a binary downloaded file to disk in Java<br><a href=\"https:\/\/stackoverflow.com\/questions\/1477269\/write-a-binary-downloaded-file-to-disk-in-java\">https:\/\/stackoverflow.com\/questions\/1477269\/write-a-binary-downloaded-file-to-disk-in-java<\/a><\/li><li>Java HttpURLConnection to download file from an HTTP URL<br><a href=\"https:\/\/www.codejava.net\/java-se\/networking\/use-httpurlconnection-to-download-file-from-an-http-url\">https:\/\/www.codejava.net\/java-se\/networking\/use-httpurlconnection-to-download-file-from-an-http-url<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/576513\/android-download-binary-file-problems\">https:\/\/stackoverflow.com\/questions\/576513\/android-download-binary-file-problems<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/29712554\/how-to-download-a-file-using-a-java-rest-service-and-a-data-stream\">https:\/\/stackoverflow.com\/questions\/29712554\/how-to-download-a-file-using-a-java-rest-service-and-a-data-stream<\/a><\/li><li>HttpClient Download File from URL<br><a href=\"https:\/\/www.technicalkeeda.com\/java-tutorials\/httpclient-download-file-from-url\">https:\/\/www.technicalkeeda.com\/java-tutorials\/httpclient-download-file-from-url<\/a><\/li><li>How do I save a file downloaded with HttpClient into a specific folder<br><a href=\"https:\/\/stackoverflow.com\/questions\/10960409\/how-do-i-save-a-file-downloaded-with-httpclient-into-a-specific-folder\">https:\/\/stackoverflow.com\/questions\/10960409\/how-do-i-save-a-file-downloaded-with-httpclient-into-a-specific-folder<\/a><\/li><li><a href=\"https:\/\/stackabuse.com\/how-to-download-a-file-from-a-url-in-java\/\">https:\/\/stackabuse.com\/how-to-download-a-file-from-a-url-in-java\/<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/8324862\/how-to-create-file-object-from-url-object\">https:\/\/stackoverflow.com\/questions\/8324862\/how-to-create-file-object-from-url-object<\/a><\/li><li><a href=\"https:\/\/gist.github.com\/rponte\/09ddc1aa7b9918b52029\">https:\/\/gist.github.com\/rponte\/09ddc1aa7b9918b52029<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u672c\u6587\u4e3b\u8981\u662f\u5bf9\u4e4b\u524d\u8bb0\u5f55\u7684\u300cJava\u4e2d\u5982\u4f55\u53d1\u8d77HTTP\u8bf7\u6c42\u300d\u4e00\u6587\u7684\u8865\u5145\/\u4fee\u6b63\uff0c\u5f53\u65f6\u53ea\u8003\u8651\u4e86 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[1383,1501,1502,61,1503],"class_list":["post-4623","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-httpclient","tag-httpurlconnection","tag-inputstream","tag-java","tag-reader"],"views":5027,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4623"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4623\/revisions"}],"predecessor-version":[{"id":4624,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4623\/revisions\/4624"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}