{"id":723,"date":"2019-12-05T10:28:05","date_gmt":"2019-12-05T02:28:05","guid":{"rendered":"http:\/\/prayerlaputa.com\/?p=723"},"modified":"2019-12-05T10:28:05","modified_gmt":"2019-12-05T02:28:05","slug":"java%e6%a0%b9%e6%8d%aeurl%e8%8e%b7%e5%8f%96%e8%be%93%e5%85%a5%e6%b5%81%e4%bb%a5%e5%8f%8a%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/prayerlaputa.com\/?p=723","title":{"rendered":"java\u6839\u636eURL\u83b7\u53d6\u8f93\u5165\u6d41\u4ee5\u53ca\u4e0b\u8f7d\u6587\u4ef6"},"content":{"rendered":"<p>\u4ee3\u7801\u53d6\u81ea\uff1a<a href=\"https:\/\/blog.csdn.net\/u012012240\/article\/details\/85078929\">JAVA\u6839\u636eURL\u7f51\u5740\u83b7\u53d6\u8f93\u5165\u6d41<\/a><\/p>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>\/**\r\n * \u6839\u636e\u5730\u5740\u83b7\u5f97\u6570\u636e\u7684\u8f93\u5165\u6d41\r\n * @param strUrl \u7f51\u7edc\u8fde\u63a5\u5730\u5740\r\n * @return url\u7684\u8f93\u5165\u6d41\r\n *\/\r\n    public static InputStream getInputStreamByUrl(String strUrl){\r\n        HttpURLConnection conn = null;\r\n        try {\r\n            URL url = new URL(strUrl);\r\n            conn = (HttpURLConnection)url.openConnection();\r\n            conn.setRequestMethod(\"GET\");\r\n            conn.setConnectTimeout(20 * 1000);\r\n            final ByteArrayOutputStream output = new ByteArrayOutputStream();\r\n            IOUtils.copy(conn.getInputStream(),output);\r\n            return  new ByteArrayInputStream(output.toByteArray());\r\n        } catch (Exception e) {\r\n            logger.error(e+\"\");\r\n        }finally {\r\n            try{\r\n                if (conn != null) {\r\n                    conn.disconnect();\r\n                }\r\n            }catch (Exception e){\r\n                logger.error(e+\"\");\r\n            }\r\n        }\r\n        return null;\r\n    }\r\n<\/code><\/pre>\n<p>\u5982\u82e5\u60f3\u4e0b\u8f7d\u6587\u4ef6\uff0c\u5219\u53ef\u4ee5\u5728\u4e0a\u9762\u65b9\u6cd5\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u5c01\u88c5\u5373\u53ef\uff1a<br \/>\n<!--more--><\/p>\n<pre class=\"language-\" data-role=\"codeBlock\" data-info=\"\"><code>public ResponseEntity&lt;byte[]&gt; downloadFileByUrl(String downUrl, String fileName, String contentType, String contentDisposition) throws IOException {\r\n        InputStream inputStream = getInputStreamByUrl(downUrl);\r\n\r\n        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\r\n        IOUtils.copy(inputStream, byteArrayOutputStream);\r\n        inputStream.close();\r\n        HttpHeaders headers = new HttpHeaders();\r\n        \/\/\u8bbe\u7f6e\u4e0b\u8f7d\u534f\u8bae\u5934\u3002\u4e0b\u9762fileName\u7684\u4e24\u6b21\u8f6c\u6362\u4e3b\u8981\u662f\u4e3a\u4e86\u4fdd\u8bc1\u4e2d\u6587\u6587\u4ef6\u540d\r\n        fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);\r\n        headers.set(HttpHeaders.CONTENT_DISPOSITION, contentDisposition + \";filename=\" + new String(fileName.getBytes(), \"utf-8\"));\r\n        headers.set(HttpHeaders.CONTENT_TYPE, contentType);\r\n        ResponseEntity&lt;byte[]&gt; responseEntity = new ResponseEntity&lt;byte[]&gt;(byteArrayOutputStream.toByteArray(), headers, HttpStatus.OK);\r\n        byteArrayOutputStream.close();\r\n        return responseEntity;\r\n    }\r\n<\/code><\/pre>\n<p>\u5176\u4e2d<code>Content-Type<\/code>\u53ef\u4ee5\u7528MediaType\u4e2d\u7684\u5e38\u91cf\uff0c\u81ea\u884c\u63a7\u5236\uff1b<code>Content-Disposition<\/code>\u5219\u770b\u9700\u6c42\uff0c\u5982\u679c\u9700\u8981\u76f4\u63a5\u6253\u5f00\u6587\u4ef6\uff0c\u5219\u4f20&#8221;inline&#8221;\uff0c\u82e5\u662f\u60f3\u4e0b\u8f7d\u5219\u4f20&#8221;attachment&#8221;\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee3\u7801\u53d6\u81ea\uff1aJAVA\u6839\u636eURL\u7f51\u5740\u83b7\u53d6\u8f93\u5165\u6d41 \/** * \u6839\u636e\u5730\u5740\u83b7\u5f97\u6570\u636e\u7684\u8f93\u5165\u6d41 * @param strUr <a href='http:\/\/prayerlaputa.com\/?p=723' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[6,9],"tags":[33,38],"_links":{"self":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/723"}],"collection":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=723"}],"version-history":[{"count":2,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/723\/revisions"}],"predecessor-version":[{"id":725,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=\/wp\/v2\/posts\/723\/revisions\/725"}],"wp:attachment":[{"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/prayerlaputa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}