
{"id":3079,"date":"2021-03-29T15:08:00","date_gmt":"2021-03-29T07:08:00","guid":{"rendered":"http:\/\/127.0.0.2\/?p=3079"},"modified":"2021-03-29T15:08:37","modified_gmt":"2021-03-29T07:08:37","slug":"springboot%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0%e4%b8%8b%e8%bd%bd","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=3079","title":{"rendered":"SpringBoot\u6587\u4ef6\u4e0a\u4f20\u4e0b\u8f7d"},"content":{"rendered":"<p>\u63a7\u5236\u5c42\uff1a<\/p>\n<pre>@Controller\r\n@RequestMapping(\"\/\")\r\npublic class UploadController {\r\n    @RequestMapping(\"\/\")\r\n    public String index(){return \"upload\";}\r\n    \/\/ \u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u8def\u5f84\r\n    public String realPath(String path){\r\n        SimpleDateFormat y=new SimpleDateFormat(\"yyyy\");\r\n        SimpleDateFormat m=new SimpleDateFormat(\"MM\");\r\n        SimpleDateFormat d=new SimpleDateFormat(\"dd\");\r\n        Date date=new Date(System.currentTimeMillis());\r\n        String year = y.format(date);\r\n        String month = m.format(date);\r\n        String day = d.format(date);\r\n        String realPath=path+ File.separator+year+File.separator+month+File.separator+day;\r\n        File file=new File(realPath);\r\n        if(!file.exists()) file.mkdirs();\r\n        return realPath;\r\n    }<!--more-->\r\n\r\n\r\n    \/\/ \u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u5e26\u6709\u7528\u6237\u540d\u7684\u6587\u4ef6\u540d\r\n    public String newFilename(String username,String filename){\r\n        Date date=new Date();\r\n        SimpleDateFormat time=new SimpleDateFormat(\"yyyyMMddHHmmss\");\r\n        String times = time.format(date);\r\n        return times+\"_\"+username+\"_\"+filename;\r\n    }\r\n    \/\/ \u6587\u4ef6\u4e0a\u4f20\r\n    @RequestMapping(\"\/upload\")\r\n    public void upload(String username, MultipartFile file, HttpServletRequest request, HttpServletResponse response)throws Exception{\r\n        request.setCharacterEncoding(\"utf-8\");\r\n        response.setCharacterEncoding(\"utf-8\");\r\n        response.setContentType(\"text\/html;charset=utf-8\");\r\n        \/\/ \u83b7\u5f97\u6587\u4ef6\u540d\u79f0\r\n        String filename = file.getOriginalFilename();\r\n        System.out.println(filename);\r\n        \/\/ \u5224\u65ad\u662f\u5426\u4e0a\u4f20\u6587\u4ef6\r\n        if (file != null &amp;&amp; filename != null) {\r\n            \/\/ \u83b7\u53d6\u4e0a\u4f20\u76ee\u5f55\r\n            String realPath = request.getServletContext().getRealPath(\"\/WEB-INF\/upload\");\r\n            String newFilePath = realPath(realPath);\r\n            String newFilename = newFilename(username,filename);\r\n            \/\/ \u5224\u65ad\u4e0a\u4f20\u76ee\u5f55\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u53cd\u4e4b\u76f4\u63a5\u5b58\r\n            File file1=new File(newFilePath);\r\n            if(!file1.exists())file1.mkdirs();\r\n            file.transferTo(new File(newFilePath+File.separator+newFilename));\r\n        }\r\n    }\r\n    \/\/ \u6587\u4ef6\u4e0b\u8f7d\r\n    @RequestMapping(\"\/download\")\r\n    public void download(String filename,HttpServletRequest request,HttpServletResponse response) throws IOException {\r\n        String realPath = request.getServletContext().getRealPath(\"\/WEB-INF\/upload\");\r\n        String newFilePath = realPath(realPath);\r\n        response.setHeader(\"content-disposition\",\"attachment;filename=\"+filename);\r\n        IOUtils.copy(new FileInputStream(newFilePath+File.separator+filename),response.getOutputStream());\r\n    }\r\n    \/\/ \u83b7\u53d6\u6587\u4ef6\u5217\u8868\r\n    public void getFileList(String path, HashMap&lt;String,String&gt; map){\r\n        File file=new File(path);\r\n        File[] files = file.listFiles();\r\n        if (files != null) {\r\n            for (File file1 : files) {\r\n                if (file1.isDirectory()) {\r\n                    getFileList(file1.getPath(),map);\r\n                }else{\r\n                    String name = file1.getName();\r\n                    String sub=name.substring(name.lastIndexOf(\"_\")+1);\r\n                    map.put(name,sub);\r\n                }\r\n            }\r\n        }\r\n    }\r\n    \/\/ \u5c55\u793a\u6587\u4ef6\u5217\u8868\r\n    @RequestMapping(\"\/fileList\")\r\n    public String fileList(HttpServletRequest request){\r\n        String realPath = request.getServletContext().getRealPath(\"\/WEB-INF\/upload\");\r\n        HashMap&lt;String,String&gt; map=new HashMap&lt;&gt;();\r\n        getFileList(realPath,map);\r\n        request.setAttribute(\"map\",map);\r\n        return \"showFileLists\";\r\n    }\r\n}<\/pre>\n<p>\u6587\u4ef6\u5217\u8868\u7684jsp<\/p>\n<pre>&lt;%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %&gt;\r\n&lt;%@ page contentType=\"text\/html;charset=UTF-8\" language=\"java\" %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;\u4e0b\u8f7d\u9875\u9762&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;h3&gt;\u4e0b\u8f7d\u5217\u8868&lt;\/h3&gt;\r\n    &lt;table border=\"1\"&gt;\r\n        &lt;tr&gt;\r\n            &lt;th&gt;\u6587\u4ef6\u540d&lt;\/th&gt;\r\n            &lt;th&gt;\u64cd\u4f5c&lt;\/th&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;c:forEach var=\"files\" items=\"${requestScope.map}\"&gt;\r\n            &lt;tr&gt;\r\n                &lt;td&gt;\u6587\u4ef6\u540d:${files.value}&lt;\/td&gt;\r\n                &lt;td&gt;\r\n                    &lt;a href=\"${pageContext.request.contextPath}\/download?filename=${files.key}\"&gt;\u4e0b\u8f7d&lt;\/a&gt;\r\n                &lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n        &lt;\/c:forEach&gt;\r\n    &lt;\/table&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\u53ea\u5c55\u793a\u767b\u5f55\u7528\u6237\u7684\u6587\u4ef6\u5217\u8868\uff0c\u4fee\u6539\u6587\u4ef6\u5217\u8868\u7684jsp\uff0c\u53ef\u4ee5\u628a\u738b\u4e94\u6362\u6210session\u7684\u7528\u6237\u4f5c\u7528\u57df\u91cc\u7684\u7528\u6237\u540d<\/p>\n<pre>&lt;%@ taglib prefix=\"c\" uri=\"http:\/\/java.sun.com\/jsp\/jstl\/core\" %&gt;\r\n&lt;%@ page contentType=\"text\/html;charset=UTF-8\" language=\"java\" %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;\u4e0b\u8f7d\u9875\u9762&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n    &lt;h3&gt;\u4e0b\u8f7d\u5217\u8868&lt;\/h3&gt;\r\n    &lt;table border=\"1\"&gt;\r\n        &lt;tr&gt;\r\n            &lt;th&gt;\u6587\u4ef6\u540d&lt;\/th&gt;\r\n            &lt;th&gt;\u64cd\u4f5c&lt;\/th&gt;\r\n        &lt;\/tr&gt;\r\n        &lt;c:forEach var=\"files\" items=\"${requestScope.map}\"&gt;\r\n            &lt;c:if test=\"${files.key.contains('\u738b\u4e94')}\"&gt;\r\n                &lt;tr&gt;\r\n                    &lt;td&gt;\u6587\u4ef6\u540d:${files.value}&lt;\/td&gt;\r\n                    &lt;td&gt;\r\n                        &lt;a href=\"${pageContext.request.contextPath}\/download?filename=${files.key}\"&gt;\u4e0b\u8f7d&lt;\/a&gt;\r\n                    &lt;\/td&gt;\r\n                &lt;\/tr&gt;\r\n            &lt;\/c:if&gt;\r\n        &lt;\/c:forEach&gt;\r\n    &lt;\/table&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a7\u5236\u5c42\uff1a @Controller @RequestMapping(&#8220;\/&#8221;) public class Uplo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-3079","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/3079","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3079"}],"version-history":[{"count":1,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/3079\/revisions"}],"predecessor-version":[{"id":3080,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/3079\/revisions\/3080"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3079"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3079"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3079"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}