{"id":1340,"date":"2024-07-09T15:05:19","date_gmt":"2024-07-09T07:05:19","guid":{"rendered":"https:\/\/blog.nonot.cn\/?p=1340"},"modified":"2024-07-09T15:05:43","modified_gmt":"2024-07-09T07:05:43","slug":"java%e7%bd%91%e7%ab%99%e8%a7%86%e9%a2%91%e8%b5%84%e6%ba%90%e5%8a%a0%e5%af%86","status":"publish","type":"post","link":"https:\/\/blog.nonot.cn\/index.php\/2024\/07\/09\/java%e7%bd%91%e7%ab%99%e8%a7%86%e9%a2%91%e8%b5%84%e6%ba%90%e5%8a%a0%e5%af%86\/","title":{"rendered":"Java\u7f51\u7ad9\u89c6\u9891\u8d44\u6e90\u52a0\u5bc6"},"content":{"rendered":"<p>\u76ee\u524d\u516c\u53f8\u662f\u505a\u7ebf\u4e0a\u89c6\u9891\u6559\u80b2\u7684\uff0c\u6559\u80b2\u89c6\u9891\u8d44\u6e90\u4e00\u76f4\u88ab\u76d7\u53d6\uff0c\u7248\u6743\u88ab\u4fb5\u72af\u3002\u9886\u5bfc\u7279\u522b\u91cd\u89c6\u6b64\u4e8b\uff0c\u4e8e\u662f\u5c31\u5f00\u59cb\u63a2\u7d22\u5982\u4f55\u52a0\u5bc6\u3002<\/p>\n<p>\u679c\u7136\uff0c\u529f\u592b\u4e0d\u8d1f\u6709\u5fc3\u4eba\u3002\u3002\u3002\u3002<\/p>\n<p>\u4e3a\u4e86\u9075\u5b88\u6280\u672f\u5f00\u6e90\u65e0\u79c1\u5949\u732e\u7684\u539f\u5219\uff0c\u8ba9\u7248\u6743\u80fd\u591f\u5f97\u5230\u66f4\u591a\u7684\u4fdd\u62a4\uff0c\u51b3\u5b9a\u679c\u65ad\u5206\u4eab\u6b64\u529f\u80fd\u7684\u5b9e\u73b0\u65b9\u6cd5\uff01<\/p>\n<p>&nbsp;<\/p>\n<p>\u5148\u5e26\u5927\u5bb6\u770b\u4e00\u4e0b\u4e3b\u6d41\u89c6\u9891\u7f51\u7ad9\u7684\u5b9e\u73b0\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1341\" title=\"1044505-20190513144454097-1375551812\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/2627c4d34d6b1cf8fa94cc4f17c38cac.png\" alt=\"1044505-20190513144454097-1375551812\" width=\"1860\" height=\"707\" \/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1342\" title=\"1044505-20190513144655288-1529745749\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/921f1b77a039728016a50da72c433e6c.png\" alt=\"1044505-20190513144655288-1529745749\" width=\"1820\" height=\"619\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u7b49\u7b49\u5427\uff0c\u5c31\u4e0d\u8fc7\u591a\u5c55\u793a\u4e86\uff0c\u76ee\u6d4b\u5f88\u591a\u7f51\u7ad9\u90fd\u7528\u8fd9\u79cd\u52a0\u5bc6\u65b9\u5f0f\uff0c\u5176\u4e2d\u7684src\u94fe\u63a5\u90fd\u662fblob:http:\/\/xxx\u683c\u5f0f\u7684\uff0c\u4e14\u6839\u636e\u94fe\u63a5\u65e0\u6cd5\u83b7\u53d6\u89c6\u9891\u6e90\u3002\u56e0\u4e3a\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u5728\u7ebf\u7684\u89c6\u9891\u5b58\u653e\u5730\u5740\uff0c\u8fd9\u6837\u4f60\u901a\u8fc7\u722c\u866b\u811a\u672c\u4e5f\u65e0\u6cd5\u4e0b\u8f7d\u8be5\u89c6\u9891\u6587\u4ef6<\/p>\n<p>\u90a3\u4e48\u5177\u4f53\u5982\u4f55\u5b9e\u73b0\u7684\u5462\uff1f<\/p>\n<p>\u4e0d\u6025\uff0c\u54b1\u4eec\u4e00\u6b65\u4e00\u6b65\u6765\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u7b2c\u4e00\u6b65\uff1a<\/strong>java\u00a0\u670d\u52a1\u5668\u63a5\u53e3\uff0c\u6839\u636eurl \u8fd4\u56de\u8d44\u6e90<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1343\" title=\"copycode-3\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/19247c87b1e3b7ab304bd9fedae7eac6.gif\" alt=\"copycode-3\" width=\"20\" height=\"20\" \/><\/a><\/span><\/div>\n<pre>package com.wf.course.web.controller;\r\n\r\n\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\nimport org.springframework.web.bind.annotation.ResponseBody;\r\n\r\nimport javax.servlet.http.HttpServletRequest;\r\nimport javax.servlet.http.HttpServletResponse;\r\nimport java.io.*;\r\nimport java.net.MalformedURLException;\r\nimport java.net.URL;\r\nimport java.net.URLConnection;\r\nimport java.net.URLEncoder;\r\n\r\n@Controller\r\npublic class VideoController {\r\n\r\n    @ResponseBody\r\n    @RequestMapping(\"\/getVideoSrc\")\r\n    public OutputStream getVideoSrc(HttpServletRequest httpServletRequest,\r\n                                    HttpServletResponse httpServletResponse){\r\n        \/\/1.\u521b\u5efa\u6587\u4ef6\u5bf9\u8c61\r\n        File f = new File(\"D:\/test\/x.mp4\");\r\n        \/\/2.\u83b7\u53d6\u6587\u4ef6\u540d\u79f0\r\n        String fileName = f.getName();\r\n        \/\/3.\u5bfc\u51fa\u6587\u4ef6\r\n        String agent = httpServletRequest.getHeader(\"User-Agent\").toUpperCase();\r\n        InputStream fis = null;\r\n        OutputStream os = null;\r\n        try {\r\n            \/\/4.\u83b7\u53d6\u8f93\u5165\u6d41\r\n            fis = new BufferedInputStream(new FileInputStream(f.getPath()));\r\n            byte[] buffer;\r\n            buffer = new byte[fis.available()];\r\n            fis.read(buffer);\r\n            httpServletResponse.reset();\r\n            \/\/5.\u7531\u4e8e\u706b\u72d0\u548c\u5176\u4ed6\u6d4f\u89c8\u5668\u663e\u793a\u540d\u79f0\u7684\u65b9\u5f0f\u4e0d\u76f8\u540c\uff0c\u9700\u8981\u8fdb\u884c\u4e0d\u540c\u7684\u7f16\u7801\u5904\u7406\r\n            if(agent.indexOf(\"FIREFOX\") != -1){\/\/\u706b\u72d0\u6d4f\u89c8\u5668\r\n                httpServletResponse.addHeader(\"Content-Disposition\", \"attachment;filename=\"+ new String(fileName.getBytes(\"GB2312\"),\"ISO-8859-1\"));\r\n            }else{\/\/\u5176\u4ed6\u6d4f\u89c8\u5668\r\n                httpServletResponse.addHeader(\"Content-Disposition\", \"attachment;filename=\"+ URLEncoder.encode(fileName, \"UTF-8\"));\r\n            }\r\n            \/\/6.\u8bbe\u7f6eresponse\u7f16\u7801\r\n            httpServletResponse.setCharacterEncoding(\"UTF-8\");\r\n            httpServletResponse.addHeader(\"Content-Length\", \"\" + f.length());\r\n            \/\/\u8bbe\u7f6e\u8f93\u51fa\u6587\u4ef6\u7c7b\u578b\r\n            httpServletResponse.setContentType(\"video\/mpeg4\");\r\n            \/\/7.\u83b7\u53d6response\u8f93\u51fa\u6d41\r\n            os = httpServletResponse.getOutputStream();\r\n            os.flush();\r\n            \/\/8.\u8f93\u51fa\u6587\u4ef6\r\n            os.write(buffer);\r\n        }catch(Exception e){\r\n            System.out.println(e.getMessage());\r\n        } finally{\r\n            \/\/\u5173\u95ed\u6d41\r\n            try {\r\n                if(fis != null){ fis.close(); }\r\n\r\n                if(os != null){ os.flush(); }\r\n\r\n                if(os != null){os.close(); }\r\n\r\n            } catch (IOException e) {\r\n                System.out.println(e.getMessage());\r\n            }\r\n        }\r\n\r\n        return os;\r\n    }\r\n\r\n\r\n    @RequestMapping(\"\/getVideoBlob_V2\")\r\n    public OutputStream getVideoBlob_V2(HttpServletRequest httpServletRequest,\r\n                                        HttpServletResponse httpServletResponse) {\r\n\r\n        String httpUrl = \"https:\/\/wangfang.oss-cn-qingdao.aliyuncs.com\/wf_video\/videoPath\/863E1B126B81B7B60993CC0B9B1EC1EA.mp3\";\r\n        \/\/ 1.\u4e0b\u8f7d\u7f51\u7edc\u6587\u4ef6\r\n        URL url = null;\r\n        try {\r\n            url = new URL(httpUrl);\r\n        } catch (MalformedURLException e1) {\r\n            e1.printStackTrace();\r\n        }\r\n\r\n        InputStream inStream = null;\r\n        OutputStream outputStream = null;\r\n        try {\r\n\r\n            \/\/2.\u83b7\u53d6\u94fe\u63a5\r\n            URLConnection conn = url.openConnection();\r\n            \/\/3.\u8f93\u5165\u6d41\r\n            inStream = conn.getInputStream();\r\n            httpServletResponse.reset();\r\n            httpServletResponse.addHeader(\"Content-Disposition\", \"attachment;filename=\" + httpUrl);\r\n            \/\/6.\u8bbe\u7f6eresponse\u7f16\u7801\r\n            httpServletResponse.setCharacterEncoding(\"UTF-8\");\/\/\u8bbe\u7f6e\u8f93\u51fa\u6587\u4ef6\u7c7b\u578b\r\n            httpServletResponse.setContentType(\"video\/mpeg4\");\r\n            \/\/7.\u83b7\u53d6response\u8f93\u51fa\u6d41\r\n            outputStream = httpServletResponse.getOutputStream();\r\n            int byteRead;\r\n            while ((byteRead = inStream.read()) != -1) {\r\n                outputStream.write(byteRead);\r\n            }\r\n\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n            System.out.println(e);\r\n        } finally {\r\n\r\n            try {\r\n                inStream.close();\r\n                outputStream.close();\r\n            } catch (IOException e) {\r\n                e.printStackTrace();\r\n            }\r\n        }\r\n\r\n        return outputStream;\r\n    }\r\n}<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1343\" title=\"copycode-3\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/19247c87b1e3b7ab304bd9fedae7eac6.gif\" alt=\"copycode-3\" width=\"20\" height=\"20\" \/><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>\u7b2c\u4e8c\u6b65\uff1a<\/strong>\u9875\u9762\u6dfb\u52a0&lt;video&gt;\u6807\u7b7e\u5f15\u7528<\/p>\n<div class=\"cnblogs_code\">\n<pre>&lt;video controls=\"controls\"  preload=\"auto\"  height=\"300\" width=\"400\" id=\"videoPath\"    type=\"video\/mp4\"&gt;\r\n                                         \u60a8\u7684\u6d4f\u89c8\u5668\u4e0d\u652f\u6301html5!\r\n                                      &lt;\/video&gt;<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>\u7b2c\u4e09\u6b65\uff1a<\/strong>\u9875\u9762js\u5904\u7406\uff0c\u8c03\u7528\u63a5\u53e3\uff0c\u52a0\u8f7d\u8d44\u6e90<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1343\" title=\"copycode-3\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/19247c87b1e3b7ab304bd9fedae7eac6.gif\" alt=\"copycode-3\" width=\"20\" height=\"20\" \/><\/a><\/span><\/div>\n<pre>$(function () {\r\n            var xhr = new XMLHttpRequest();\/\/\u521b\u5efaXMLHttpRequest\u5bf9\u8c61\r\n            xhr.open('GET', 'http:\/\/localhost:8080\/getVideoBlob_V2', true);\/\/\u914d\u7f6e\u8bf7\u6c42\u65b9\u5f0f\u3001\u8bf7\u6c42\u5730\u5740\u4ee5\u53ca\u662f\u5426\u540c\u6b65\r\n            xhr.responseType = 'blob';\/\/\u8bbe\u7f6e\u7ed3\u679c\u7c7b\u578b\u4e3ablob;\r\n            xhr.onload = function(e) {\r\n                alert(this.status);\r\n                if (this.status === 200) {\r\n                    \/\/ \u83b7\u53d6blob\u5bf9\u8c61\r\n                    var blob = this.response;\r\n                    console.log(blob);\r\n                    \/\/ \u83b7\u53d6blob\u5bf9\u8c61\u5730\u5740\uff0c\u5e76\u628a\u503c\u8d4b\u7ed9\u5bb9\u5668\r\n                    $(\"#videoPath\").attr(\"src\", URL.createObjectURL(blob));\r\n                }\r\n            };\r\n            xhr.send();\r\n        });<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1343\" title=\"copycode-3\" src=\"https:\/\/blog.nonot.cn\/wp-content\/uploads\/replace\/19247c87b1e3b7ab304bd9fedae7eac6.gif\" alt=\"copycode-3\" width=\"20\" height=\"20\" \/><\/a><\/span><\/div>\n<\/div>\n<ul>\n<li>\u8fd9\u91cc\u4f7f\u7528\u7684\u6700\u539f\u751f\u7684<code>XMLHttpRequest<\/code>\u5bf9\u8c61\u8bed\u6cd5\uff0c\u8fd9\u91cc\u6700\u91cd\u8981\u7684\u4e00\u70b9\u662f\u8981<strong>\u8bbe\u7f6eresponseType\u4e3ablob<\/strong>\uff0c\u8fd9\u6837\u63a5\u6536\u5230response\u76f4\u63a5\u5c31\u662f\u4e00\u4e2ablob\u5bf9\u8c61\u4f9b\u6211\u4eec\u4f7f\u7528\u3002\u8fd9\u4e2aresponseType\u5c5e\u6027\u4e0d\u5c5e\u4e8ehttp\u5934\u90e8\u4fe1\u606f\uff0c\u800c\u662fajax\u8bf7\u6c42\u4e2dXHR\u5bf9\u8c61\u7684\u5c5e\u6027(\u9ed8\u8ba4\u4e3a&#8221;&#8221;\u4e5f\u5c31\u662f<code>text<\/code>\u7c7b\u578b\uff0c\u800c\u5728\u4e00\u4e9b\u5c01\u88c5XHR\u7684\u6846\u67b6\u4e2d\uff0c\u4e00\u822c\u628a\u9ed8\u8ba4\u503c\u8bbe\u4e3a<code>json<\/code>)\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u4ee5<code>blob:<\/code>\u5f00\u5934\u7684\u4e34\u65f6url\u5730\u5740\uff0c\u800c\u4e14\u5728\u5411\u670d\u52a1\u7aef\u8bf7\u6c42\u65f6\u9875\u9690\u85cf\u4e86\u771f\u5b9e\u7684\u89c6\u9891\u5730\u5740\u3002<\/li>\n<\/ul>\n<div><\/div>\n<div>\n<div>\n<h5>createObjectURL\u4e0eBLOB<\/h5>\n<ul>\n<li>\u6211\u4eec\u518d\u56de\u5230\u90a3\u4e2a\u4ee5<code>blob:<\/code>\u5f00\u5934\u7684\u795e\u79d8\u5b57\u7b26\u4e32\uff0c\u5b83\u5176\u5b9e\u662f\u901a\u8fc7<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/URL\/createObjectURL\" target=\"_blank\" rel=\"noopener\"><code>URL.createObjectURL<\/code><\/a>\u8fd9\u4e2aAPI\u751f\u6210\u7684\uff0c\u8be5\u51fd\u6570\u63a5\u6536\u4e00\u4e2aBLOB\u5bf9\u8c61\uff0c\u8fd4\u56de\u8be5\u5bf9\u8c61\u5bf9\u5e94\u7684<code>DOMString<\/code>\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5176\u5b9e\u4e5f\u53ef\u4ee5\u770b\u505a\u662f\u4e00\u4e2aurl\u5730\u5740\uff0c\u4f46\u5b83\u662f\u4e0e\u5f53\u524d\u7a97\u53e3\u7684<code>document<\/code>\u5bf9\u8c61\u7ed1\u5b9a\u7684\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u662f<strong>\u4f1a\u8bdd(session)\u7ea7<\/strong>\u7684\uff0c\u6240\u4ee5\u4f60\u5728\u65b0\u7684tab\u6253\u5f00\u4e5f\u5c31\u65e0\u6548\u4e86<\/li>\n<li>\u518d\u6765\u4e86\u89e3\u4e0bBLOB\uff0c\u4ed6\u7684\u5168\u79f0\u4e3abig binary large object\uff0c\u4e8c\u8fdb\u5236\u5927\u5bf9\u8c61\u3002\u5982\u679c\u628a\u4e00\u4e2a\u89c6\u9891\u6587\u4ef6\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u5bf9\u8c61\uff0c\u5176\u5927\u5c0f\u80af\u5b9a\u5f88\u5927\uff0c\u8fd9\u6837\u7406\u89e3\u5c31\u6e05\u695a\u591a\u4e86\u3002\u5728\u6d4f\u89c8\u5668\u7aef\u4e5f\u63d0\u4f9b\u4e86BLOB\u76f8\u5173\u7684<a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Blob\" target=\"_blank\" rel=\"noopener\">API<\/a>\uff0c\u901a\u8fc7<code>new Blog(...)<\/code>\u751f\u6210blog\u5bf9\u8c61\u3002<\/li>\n<li>\u62ff\u5230blog\u5bf9\u8c61\u540e\uff0c\u518d\u901a\u8fc7<code>URL.createObjectURL<\/code>\u751f\u6210\u4e34\u65f6\u5730\u5740\uff0c\u8d4b\u503c\u7ed9video\u6807\u7b7e\u7684src\u5c5e\u6027\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u4e86\u3002\u4f46\u5176\u5b9e\u53ef\u4ee5\u76f4\u63a5\u4ece\u670d\u52a1\u7aef\u63a5\u6536\u4e8c\u8fdb\u5236\u5bf9\u8c61\uff0c\u5c31\u662f\u670d\u52a1\u7aef\u628a\u89c6\u9891\u6587\u4ef6\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u5bf9\u8c61\uff0c\u901a\u8fc7\u63a5\u53e3\u7ed9\u5230\u524d\u7aef\uff0c\u524d\u7aef\u518d\u751f\u6210<code>dom string<\/code>\u3002<\/li>\n<\/ul>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u524d\u516c\u53f8\u662f\u505a\u7ebf\u4e0a\u89c6\u9891\u6559\u80b2\u7684\uff0c\u6559\u80b2\u89c6\u9891\u8d44\u6e90\u4e00\u76f4\u88ab\u76d7\u53d6\uff0c\u7248\u6743\u88ab\u4fb5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1346,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1340","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/1340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1340"}],"version-history":[{"count":1,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/1340\/revisions"}],"predecessor-version":[{"id":1347,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/posts\/1340\/revisions\/1347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media\/1346"}],"wp:attachment":[{"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nonot.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}