﻿{"id":652,"date":"2020-03-11T20:40:58","date_gmt":"2020-03-11T12:40:58","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=652"},"modified":"2020-03-11T20:40:58","modified_gmt":"2020-03-11T12:40:58","slug":"%e5%9c%a8j2ee%e4%b8%ad%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e6%97%b6%e4%bd%bf%e7%94%a8%e4%b8%ad%e6%96%87%e5%90%8d%e5%ad%97%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=652","title":{"rendered":"\u5728J2EE\u4e2d\u4e0b\u8f7d\u6587\u4ef6\u65f6\u4f7f\u7528\u4e2d\u6587\u540d\u5b57\u7684\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"<pre class=\"lang:default decode:true \">    @RequestMapping(path = { \"\/downloadFromArchive\" }, method={RequestMethod.GET})\r\n    public ResponseEntity&lt;byte[]&gt; downloadFromArchive(HttpServletRequest request, HttpServletResponse response,\r\n    \t\t\t@RequestParam(required=false) String id\r\n    \t\t) throws IOException {\r\n    \t  \r\n        HttpHeaders headers = new HttpHeaders();\t\/\/\u8bbe\u7f6ehttp\u534f\u8bae\u5934\u90e8\r\n        \r\n        if(id==null||id.equals(\"\")){\r\n        \treturn null;\r\n        }\r\n        \r\n        Archive archive = this.archiveService.get(id);\r\n        if(archive==null){\r\n        \treturn null;\r\n        }\r\n        \r\n        if(archive.getLink().indexOf(\".\")&lt;0){\r\n        \tresponse.sendRedirect(archive.getLink());\r\n        \treturn null;\r\n        }\r\n        String extension = archive.getLink().substring(archive.getLink().lastIndexOf(\".\"));\r\n        \r\n        \/\/System.out.println(\"archive.getTitle()\"+archive.getTitle());\r\n        \/\/System.out.println(\"archive.getLink()\"+archive.getLink());\r\n        \r\n        String fileName = archive.getTitle()+extension;\r\n        \/\/System.out.println(\"fileName = \"+fileName);\r\n        \r\n        \/\/\u83b7\u53d6\u6587\u4ef6\u5728\u670d\u52a1\u5668\u4e0a\u7684\u7edd\u5bf9\u8def\u5f84\r\n\t\tString filePath = archive.getLink();\r\n\t\t\r\n\t\t\/\/\u5047\u5982\u8def\u5f84\u4e3a\u963f\u65b0\u5236\u4f5c\u7684\u4e0b\u8f7dcontroller,\u5219\u6267\u884c\u7279\u6b8a\u7684\u64cd\u4f5c,filePath\u4f1a\u6839\u636e\u963f\u65b0\u7684\u903b\u8f91\u53bb\u83b7\u53d6\r\n\t\t\r\n\t\t\t\r\n\t\tPattern pattern = Pattern.compile(\".*\\\\?archiveId=(.*?)&amp;fileName=(.*)&amp;randomName=(.*)\");\r\n\t\tMatcher\tmatcher = pattern.matcher(archive.getLink());\r\n\t\tif(matcher.find()){\r\n\t\t\tSystem.out.println(\"\u6b63\u5219\u5339\u914d\u6210\u529f1\");\r\n\t\t\tString archiveId = matcher.group(1);\r\n\t\t\tfileName = matcher.group(2);\r\n\t\t\tString randomName = matcher.group(3);\r\n\t\t\tfilePath = \"\/uploads\/archiveFile\/\"+archiveId+\"\/\"+randomName;\r\n\t\t\tSystem.out.println(\"filePath\" + filePath);\r\n\t\t}\r\n\t\telse{\r\n\t\t\tpattern = Pattern.compile(\".*\\\\?archiveId=(.*?)&amp;fileName=(.*)\");\r\n\t\t\tmatcher = pattern.matcher(archive.getLink());\r\n\t\t\tif(matcher.find()){\r\n\t\t\t\tSystem.out.println(\"\u6b63\u5219\u5339\u914d\u6210\u529f2\");\r\n\t\t\t\tString archiveId = matcher.group(1);\r\n\t\t\t\tfileName = matcher.group(2);\r\n\t\t\t\tfilePath = \"\/uploads\/archiveFile\/\"+archiveId+\"\/\"+fileName;\r\n\t\t\t\tSystem.out.println(\"filePath\" + filePath);\r\n\t\t\t}else{}\r\n\t\t}\r\n\t\t\r\n\t\t\r\n\t\t\r\n        File file = new File(this.servletContext.getRealPath(\"\/\") + filePath);\r\n        \r\n        if(!file.exists()){\r\n        \tSystem.out.println(\"\u76ee\u6807\u4e0b\u8f7d\u6587\u4ef6\u4e0d\u5b58\u5728\");\r\n        \tResponseStaticUtil.write(response, \"\u76ee\u6807\u4e0b\u8f7d\u6587\u4ef6\u4e0d\u5b58\u5728\");\r\n    \t\treturn null;\r\n        }\r\n        \/\/response.sendRedirect(filePath);\r\n        \r\n        \r\n        \/\/\u5934\u90e8\u8bbe\u7f6e\u6587\u4ef6\u7c7b\u578b\r\n        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);  \r\n        \/\/headers.setContentDispositionFormData(\"attachment\", upload.getName());\r\n\t\tif(request.getHeader(\"User-Agent\").toUpperCase().indexOf(\"MSIE\") &gt; 0) {\t\/\/IE\u6d4f\u89c8\u5668UTF-8\r\n\t\t\tSystem.out.println(\"IE\u6d4f\u89c8\u5668\");\r\n\t\t\theaders.setContentDispositionFormData(\"attachment\", fileName);\r\n\t\t}\/*else if(request.getHeader(\"User-Agent\").contains(\"Firefox\")){\t\/\/\u706b\u72d0\u6d4f\u89c8\u5668\r\n\t\t\tfileName=\"=?utf-8?b?\"+new BASE64Encoder().encode(fileName.getBytes(\"utf-8\"))+\"?=\";\r\n\t\t}*\/else{\t\/\/ \u5176\u4ed6\u6d4f\u89c8\u5668attachment;filename*=utf-8'zh_cn\r\n\t\t\tSystem.out.println(\"\u5176\u4ed6\u6d4f\u89c8\u5668\");\r\n\t\t\tresponse.setHeader(\"Content-Disposition\", \"attachment;fileName=\"+URLEncoder.encode(fileName,\"utf-8\")+\"\");\/\/ \u91cd\u70b9\r\n\t\t}\r\n        \r\n        \r\n\t\t\/\/\u8fd4\u56de\u6587\u4ef6\u5b57\u8282\u6570\u7ec4\r\n        \/*try {\r\n            return new ResponseEntity&lt;byte[]&gt;(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n            return null;    \r\n        }*\/\r\n\t\tInputStream in = new FileInputStream(file);  \r\n        OutputStream out = response.getOutputStream();  \r\n        byte[] b = new byte[1024];\r\n        int length = 0;\r\n        while((length = in.read(b)) != -1)  {  \r\n            out.write(b,0,length);  \r\n        }  \r\n        in.close();  \r\n        out.close();\r\n        return null;\r\n    }<\/pre>\n<p>\u6709\u5fc5\u8981\u8bf4\u4e00\u4e0b\uff0c\u4e3a\u4ec0\u4e48\u6ca1\u6709\u4f7f\u7528FileUtils\u7684readFileToByteArray\u65b9\u6cd5\uff0c\u800c\u662f\u4f7f\u7528\u539f\u59cb\u7684out.write\uff0c\u56e0\u4e3a\u524d\u8005\u5728\u79fb\u52a8\u7aef\u4e2d\u4e0b\u8f7d\u6587\u4ef6\u662f\u4e0d\u6210\u529f\u7684\uff08\u4f46\u5947\u602a\u7684\u662f\u5728\u7535\u8111\u7aef\u4e0a\u5374\u662f\u6b63\u5e38\u7684\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>@RequestMapping(path = { &#8220;\/downloadFromArchive&#8221; }, meth [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[199,200],"class_list":["post-652","post","type-post","status-publish","format-standard","hentry","category-java","tag-j2ee","tag-200"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/652","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=652"}],"version-history":[{"count":1,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions"}],"predecessor-version":[{"id":653,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/652\/revisions\/653"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=652"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=652"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=652"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}