{"id":866,"date":"2018-02-24T22:11:34","date_gmt":"2018-02-24T19:11:34","guid":{"rendered":"http:\/\/leodev.ru\/blog\/?p=866"},"modified":"2018-02-24T22:11:34","modified_gmt":"2018-02-24T19:11:34","slug":"pring-mvc-download-file-controller-example","status":"publish","type":"post","link":"https:\/\/leodev.ru\/blog\/spring\/pring-mvc-download-file-controller-example\/","title":{"rendered":"Spring MVC \u2013 \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432"},"content":{"rendered":"<h2>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438<\/h2>\n<p>\u0412 Spring MVC \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0447\u0442\u043e \u0431\u044b \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0447\u0435\u0440\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f:<\/p>\n<ol>\n<li>\u041d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c, \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435 \u0431\u0443\u0434\u0435\u0442\u00a0<code>void<\/code>\u00a0 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c <code>HttpServletResponse<\/code>\u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u0430.<\/li>\n<li>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u00a0<code>HttpServletResponse<\/code>\u00a0<a href=\"https:\/\/www.iana.org\/assignments\/media-types\/media-types.xhtml\">content type<\/a> (\u0434\u0430\u043b\u0435\u0435 \u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e) \u043d\u0430 \u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e, \u0438\u043b\u0438 \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u043b \u0434\u0438\u0430\u043b\u043e\u0433\u043e\u0432\u043e\u0435 \u043e\u043a\u043d\u043e \u00ab\u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u0430\u043a\u00bb, \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0435\u0433\u043e\u00a0<code>APPLICATION\/OCTET-STREAM<\/code>\u00a0<em>(\u043d\u0435 \u0447\u0443\u0432\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u0435\u043d \u043a \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0443)<\/em><\/li>\n<li>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u043e\u0442\u0432\u0435\u0442\u0430 HTTP \u0441 \u0438\u043c\u0435\u043d\u0435\u043c <strong>Content-Disposition<\/strong> \u0438 \u043f\u0440\u0438\u0441\u0432\u043e\u0439\u0442\u0435 \u0435\u043c\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u00a0<strong>attachment; filename=fileName<\/strong>, \u0433\u0434\u0435\u00a0<strong>fileName <\/strong>&#8212; \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h2>\u041a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u0430<\/h2>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u0430\u0439\u043b\u043e\u0432, \u0432 \u043c\u043e\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 Excel \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b.<\/p>\n<pre class=\"lang:java decode:true\">    @RequestMapping(value = \"\/files\/{file_name:.+}\", method = RequestMethod.GET)\r\n    public void getFile(@PathVariable(\"file_name\") String fileName, HttpServletResponse response) {\r\n        \/\/ \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0438 \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0430\u0432 \u043d\u0430 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u0444\u0430\u0439\u043b\u0430. \u0415\u0441\u043b\u0438 \u043d\u0435\u0442, \u0442\u043e \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u043c \u0437\u0434\u0435\u0441\u044c Exception\r\n\r\n        \/\/\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0441\u043c\u043e\u0433\u0443\u0442 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0444\u0430\u0439\u043b\r\n        Path file = Paths.get(PathUtil.getUploadedFolder(), fileName);\r\n        if (Files.exists(file)){\r\n            response.setHeader(\"Content-disposition\", \"attachment;filename=\" + fileName);\r\n            response.setContentType(\"application\/vnd.ms-excel\");\r\n\r\n            try {\r\n                Files.copy(file, response.getOutputStream());\r\n                response.getOutputStream().flush();\r\n            } catch (IOException e) {\r\n                LOG.info(\"Error writing file to output stream. Filename was '{}'\" + fileName, e);\r\n                throw new RuntimeException(\"IOError writing file to output stream\");\r\n            }\r\n        }\r\n    }<\/pre>\n<p>[stextbox id=&#8217;info&#8217;]\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 \u0438\u043c\u0435\u0435\u0442 \u043b\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u0442\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443, \u0430 \u0437\u0430\u0442\u0435\u043c \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0442\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0432 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0442\u043e\u0438\u0442 \u043f\u0435\u0440\u0435\u043d\u0430\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 \u044d\u043a\u0440\u0430\u043d \u0432\u0445\u043e\u0434\u0430 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0443[\/stextbox]<\/p>\n<h2>\u041f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043d\u044b\u0445 \u0441\u0441\u044b\u043b\u043e\u043a \u043d\u0430 \u0444\u0430\u0439\u043b\u044b<\/h2>\n<p>\u0414\u0440\u0443\u0433\u0438\u0435 \u0441\u0430\u0439\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/en.wikipedia.org\/wiki\/Cross-reference\">\u043f\u0435\u0440\u0435\u043a\u0440\u0435\u0441\u0442\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438<\/a> \u043d\u0430 \u0432\u0430\u0448\u0438 \u0444\u0430\u0439\u043b\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0438\u0445(\u0444\u0430\u0439\u043b\u044b) \u043a\u0430\u043a \u0441\u0432\u043e\u0438. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438. \u0427\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u0434\u043e\u043c\u0435\u043d\u043e\u0432, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043b\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <strong>referer<\/strong>\u00a0\u0432\u0430\u0448\u0435 \u0434\u043e\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u043c\u044f.<\/p>\n<p>\u041d\u0430\u0448 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 <strong>FileController<\/strong> \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442 \u0444\u0430\u0439\u043b\u044b \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440, \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a <strong>referer<\/strong>\u00a0\u043d\u0435 \u0440\u0430\u0432\u0435\u043d <strong>null<\/strong>. \u042d\u0442\u043e \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0442\u0438\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u0432\u0432\u0435\u0434\u044f \u0438\u0445 URL-\u0430\u0434\u0440\u0435\u0441\u0430 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0438\u043b\u0438 \u0437\u0430\u043f\u0440\u043e\u0441, \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0438\u0439 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u0434\u043e\u043c\u0435\u043d\u043e\u0432.<\/p>\n<pre class=\"lang:java decode:true\">    @RequestMapping(value = \"\/files\/{file_name:.+}\", method = RequestMethod.GET)\r\n    public void getFile(@PathVariable(\"file_name\") String fileName, @RequestHeader String referer, HttpServletResponse response) {\r\n        if (referer == null || referer.isEmpty())\r\n            throw new RefererException(\"Missing header 'referer' when try download file: \" + fileName);\r\n\r\n\/\/...\r\n    }<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043e\u0440\u0438\u0438 \u0412 Spring MVC \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438, \u0447\u0442\u043e \u0431\u044b \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0447\u0435\u0440\u0435\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f: \u041d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c, \u0432 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0435 \u0431\u0443\u0434\u0435\u0442\u00a0void\u00a0 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c HttpServletResponse\u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0442\u043e\u0434\u0430. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u00a0HttpServletResponse\u00a0content type (\u0434\u0430\u043b\u0435\u0435 \u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e) \u043d\u0430 \u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043d\u0430\u0448\u0435\u0433\u043e \u0444\u0430\u0439\u043b\u0430. \u0415\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e, \u0438\u043b\u0438 \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u043b \u0434\u0438\u0430\u043b\u043e\u0433\u043e\u0432\u043e\u0435 \u043e\u043a\u043d\u043e &hellip; <a href=\"https:\/\/leodev.ru\/blog\/spring\/pring-mvc-download-file-controller-example\/\" class=\"more-link\">\u0427\u0438\u0442\u0430\u0442\u044c \u0434\u0430\u043b\u0435\u0435<span class=\"screen-reader-text\"> \u00abSpring MVC \u2013 \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0434\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0444\u0430\u0439\u043b\u043e\u0432\u00bb<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93,228],"tags":[233],"class_list":["post-866","post","type-post","status-publish","format-standard","hentry","category-spring","category-spring-mvc","tag-spring-mvc"],"_links":{"self":[{"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/comments?post=866"}],"version-history":[{"count":4,"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/posts\/866\/revisions"}],"predecessor-version":[{"id":870,"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/posts\/866\/revisions\/870"}],"wp:attachment":[{"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/media?parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/categories?post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leodev.ru\/blog\/wp-json\/wp\/v2\/tags?post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}