{"id":2981,"date":"2022-12-30T15:12:40","date_gmt":"2022-12-30T07:12:40","guid":{"rendered":"https:\/\/qaqaq.top\/?p=2981"},"modified":"2022-12-30T15:12:40","modified_gmt":"2022-12-30T07:12:40","slug":"%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e5%92%8c%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=2981","title":{"rendered":"\u6587\u4ef6\u4e0a\u4f20\u548c\u4e0b\u8f7d"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1\u3001\u6587\u4ef6\u4e0b\u8f7d<\/h2>\n\n\n\n<p>\u4f7f\u7528ResponseEntity\u5b9e\u73b0\u4e0b\u8f7d\u6587\u4ef6\u7684\u529f\u80fd<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @RequestMapping(\"\/testDown\")\r\n    public ResponseEntity&lt;byte&#91;]> testResponseEntity(HttpSession session) throws IOException {\r\n        \/\/\u83b7\u53d6ServletContext\u5bf9\u8c61\r\n        ServletContext servletContext = session.getServletContext();\r\n        \/\/\u83b7\u53d6\u670d\u52a1\u5668\u4e2d\u6587\u4ef6\u7684\u771f\u5b9e\u8def\u5f84\r\n        String realPath = servletContext.getRealPath(\"\/static\/img\/Eclipse.jpg\");\r\n        System.out.println(realPath);\r\n        \/\/\u521b\u5efa\u8f93\u5165\u6d41\r\n        InputStream is = new FileInputStream(realPath);\r\n        \/\/\u521b\u5efa\u5b57\u8282\u6570\u7ec4\r\n        byte&#91;] bytes = new byte&#91;is.available()];\r\n        \/\/\u5c06\u6d41\u8bfb\u5230\u5b57\u8282\u6570\u7ec4\u4e2d\r\n        is.read(bytes);\r\n        \/\/\u521b\u5efaHttpHeaders\u5bf9\u8c61\u8bbe\u7f6e\u54cd\u5e94\u5934\u4fe1\u606f\r\n        MultiValueMap&lt;String, String> headers = new HttpHeaders();\r\n        \/\/\u8bbe\u7f6e\u8981\u4e0b\u8f7d\u65b9\u5f0f\u4ee5\u53ca\u4e0b\u8f7d\u6587\u4ef6\u7684\u540d\u5b57\r\n        headers.add(\"Content-Disposition\", \"attachment;filename=Eclipse.jpg\");\r\n        \/\/\u8bbe\u7f6e\u54cd\u5e94\u72b6\u6001\u7801\r\n        HttpStatus statusCode = HttpStatus.OK;\r\n        \/\/\u521b\u5efaResponseEntity\u5bf9\u8c61\r\n        ResponseEntity&lt;byte&#91;]> responseEntity = new ResponseEntity&lt;byte&#91;]>(bytes, headers, statusCode);\r\n        \/\/\u5173\u95ed\u8f93\u5165\u6d41\r\n        is.close();\r\n        return responseEntity;\r\n    }<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">2\u3001\u6587\u4ef6\u4e0a\u4f20<\/h2>\n\n\n\n<p>\u6587\u4ef6\u4e0a\u4f20\u8981\u6c42form\u8868\u5355\u7684\u8bf7\u6c42\u65b9\u5f0f\u5fc5\u987b\u4e3apost\uff0c\u5e76\u4e14\u6dfb\u52a0\u5c5e\u6027enctype=&#8221;multipart\/form-data&#8221;<\/p>\n\n\n\n<p>SpringMVC\u4e2d\u5c06\u4e0a\u4f20\u7684\u6587\u4ef6\u5c01\u88c5\u5230MultipartFile\u5bf9\u8c61\u4e2d\uff0c\u901a\u8fc7\u6b64\u5bf9\u8c61\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u76f8\u5173\u4fe1\u606f<\/p>\n\n\n\n<p>\u4e0a\u4f20\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<p>a>\u6dfb\u52a0\u4f9d\u8d56\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    &lt;!-- https:\/\/mvnrepository.com\/artifact\/commons-fileupload\/commons-fileupload -->\r\n    &lt;dependency>\r\n      &lt;groupId>commons-fileupload&lt;\/groupId>\r\n      &lt;artifactId>commons-fileupload&lt;\/artifactId>\r\n      &lt;version>1.3.1&lt;\/version>\r\n    &lt;\/dependency><\/code><\/pre>\n\n\n\n<p>b>\u5728SpringMVC\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u914d\u7f6e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- \u914d\u7f6e\u6587\u4ef6\u4e0a\u4f20\u89e3\u6790\u5668\uff0c\u5c06\u4e0a\u4f20\u7684\u6587\u4ef6\u5c01\u88c5\u4e3aMultipartFile -->\n&lt;!--\u5fc5\u987b\u901a\u8fc7\u6587\u4ef6\u89e3\u6790\u5668\u7684\u89e3\u6790\u624d\u80fd\u5c06\u6587\u4ef6\u8f6c\u6362\u4e3aMultipartFile\u5bf9\u8c61-->\r\n    &lt;bean id=\"multipartResolver\" class=\"org.springframework.web.multipart.commons.CommonsMultipartResolver\">&lt;\/bean><\/code><\/pre>\n\n\n\n<p>c>\u63a7\u5236\u5668\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @RequestMapping(\"\/testUp\")\r\n    public String testUp(MultipartFile photo, HttpSession session) throws IOException {\r\n        \/\/\u83b7\u53d6\u4e0a\u4f20\u7684\u6587\u4ef6\u7684\u6587\u4ef6\u540d\r\n        String fileName = photo.getOriginalFilename();\r\n        \/\/\u5904\u7406\u6587\u4ef6\u91cd\u540d\u95ee\u9898\r\n        String hzName = fileName.substring(fileName.lastIndexOf(\".\"));\r\n        fileName = UUID.randomUUID().toString() + hzName;\r\n        \/\/\u83b7\u53d6\u670d\u52a1\u5668\u4e2dphoto\u76ee\u5f55\u7684\u8def\u5f84\r\n        ServletContext servletContext = session.getServletContext();\r\n        String photoPath = servletContext.getRealPath(\"photo\");\r\n        File file = new File(photoPath);\r\n        if (!file.exists()) {\r\n            file.mkdir();\r\n        }\r\n        String finalPath = photoPath + File.separator + fileName;\r\n        \/\/\u5b9e\u73b0\u4e0a\u4f20\u529f\u80fd\r\n        photo.transferTo(new File(finalPath));\r\n        return \" success \";\r\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u6587\u4ef6\u4e0b\u8f7d \u4f7f\u7528ResponseEntity\u5b9e\u73b0\u4e0b\u8f7d\u6587\u4ef6\u7684\u529f\u80fd 2\u3001\u6587\u4ef6\u4e0a\u4f20 \u6587\u4ef6\u4e0a\u4f20\u8981\u6c42form\u8868\u5355\u7684\u8bf7\u6c42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[112],"class_list":["post-2981","post","type-post","status-publish","format-standard","hentry","category-springmvc-code","tag-springmvc"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2981"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2981"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2981\/revisions"}],"predecessor-version":[{"id":2982,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/2981\/revisions\/2982"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}