{"id":10020588,"date":"2025-01-03T16:50:42","date_gmt":"2025-01-03T08:50:42","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020588"},"modified":"2025-01-03T16:50:42","modified_gmt":"2025-01-03T08:50:42","slug":"%e6%96%87%e4%bb%b6%e6%b5%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020588","title":{"rendered":"\u6587\u4ef6\u6d41"},"content":{"rendered":"<p>\u524d\u7aef\u8bf7\u6c42 vue<\/p>\n<pre><code>Axios({\n          method: 'get',\n          url: 'http:\/\/IP\u5730\u5740:\u7aef\u53e3\/previewFileStream',\n          params: {\n            filePath: '\/home\/NSY1312-02-06-00-00-06A.PDF',\n          },\n          headers:{\n            'Content-Type': 'application\/json;charset=UTF-8',\n          },\n          responseType: 'arraybuffer',\n        }).then(function (res) {\n          console.log(res);\n        });<\/code><\/pre>\n<p>\u540e\u7aef java<\/p>\n<pre><code>     @GetMapping(&quot;\/previewFileStream&quot;)\n    public ResponseEntity&lt;byte[]&gt; previewFileStream(@RequestParam String filePath) {\n        try {\n            File file = new File(filePath);\n            if (!file.exists()) {\n                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);\n            }\n\n            FileInputStream fileInputStream = new FileInputStream(file);\n            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();\n            byte[] buffer = new byte[1024];\n            int bytesRead;\n\n            while ((bytesRead = fileInputStream.read(buffer)) != -1) {\n                byteArrayOutputStream.write(buffer, 0, bytesRead);\n            }\n\n            byte[] fileContent = byteArrayOutputStream.toByteArray();\n\n            HttpHeaders headers = new HttpHeaders();\n            headers.add(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment; filename=&quot; + file.getName());\n            headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);\n\/\/            headers.add(&quot;content-type&quot;, &quot;application\/pdf;chartset=UTF-8&quot;);\n\/\/            headers.add(&quot;Content-Disposition&quot;, &quot;fileName=&quot; + URLEncoder.encode(file.getName(), &quot;utf-8&quot;));\n            headers.setContentLength(fileContent.length);\n\n            return ResponseEntity.ok()\n                    .headers(headers)\n                    .body(fileContent);\n        } catch (FileNotFoundException e) {\n            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);\n        } catch (IOException e) {\n            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);\n        }\n    }<\/code><\/pre>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000045700145\">https:\/\/segmentfault.com\/a\/1190000045700145<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7aef\u8bf7\u6c42 vue Axios({ method: &#8216;get&#8217;, url: &#8216;http:\/\/IP\u5730\u5740:\u7aef\u53e3\/pr [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020588","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020588","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020588"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020588\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}