{"id":476,"date":"2021-06-15T21:43:59","date_gmt":"2021-06-15T13:43:59","guid":{"rendered":"http:\/\/moyisuiying.com\/?p=476"},"modified":"2021-06-15T21:43:59","modified_gmt":"2021-06-15T13:43:59","slug":"%e5%9c%a8win10%e6%9c%ac%e5%9c%b0%e5%bc%80%e5%8f%91springboot%e9%a1%b9%e7%9b%ae%e8%83%bd%e4%b8%8a%e4%bc%a0%e5%9b%be%e7%89%87%ef%bc%8c%e5%b9%b6%e8%83%bd%e9%80%9a%e8%bf%87url%e7%9b%b4%e6%8e%a5%e4%bb%8e","status":"publish","type":"post","link":"http:\/\/moyisuiying.com\/index.php\/javastudy\/springboot\/476.html","title":{"rendered":"\u5728win10\u672c\u5730\u5f00\u53d1springboot\u9879\u76ee\u80fd\u4e0a\u4f20\u56fe\u7247\uff0c\u5e76\u80fd\u901a\u8fc7URL\u76f4\u63a5\u4ece\u6d4f\u89c8\u5668\u8bbf\u95ee\uff0c\u4f46\u662f\u90e8\u7f72\u5230\u670d\u52a1\u5668\u4e0a\u540e\u80fd\u4e0a\u4f20\u6587\u4ef6\uff0c\u4f46\u662f\u901a\u8fc7\u6d4f\u89c8\u5668\u65e0\u6cd5\u8bbf\u95ee\u56fe\u7247"},"content":{"rendered":"\n<h2>1.\u9996\u5148springboot\u9879\u76ee\u5728Window\u548cLinux\u670d\u52a1\u5668\u7684\u9879\u76ee\u8d44\u6e90\u8def\u5f84\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u9700\u8981\u5206\u5f00\u6765\u8bbe\u7f6e\u8def\u5f84\uff1a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code> @Override\n    @Transactional(readOnly = false, rollbackFor = Exception.class)\n    public String uploadImage(MultipartFile image, Comment comment, String rootUrl) throws Exception {\n        if (comment == null) {\n            return null;\n        }\n\n        String imgRequestUrl = null;\n        if (!image.isEmpty()){\n            File imagePath;  \/\/\u56fe\u7247\u5b58\u653e\u5730\u5740\n            \/\/\u83b7\u53d6\u6587\u4ef6\u540d\u79f0\n            String  imageName = image.getOriginalFilename();\n            String os = System.getProperty(\"os.name\");\n            if (os.toLowerCase().startsWith(\"win\")) {  \/\/windows\u7cfb\u7edf\n                String path = System.getProperty(\"user.dir\");  \/\/\u83b7\u53d6\u9879\u76ee\u76f8\u5bf9\u8def\u5f84\n                imagePath = new File(path+file_path);\n            } else {\/\/linux\u7cfb\u7edf\n                \/\/\u83b7\u53d6\u6839\u76ee\u5f55\n                \/\/\u5982\u679c\u662f\u5728\u672c\u5730windows\u73af\u5883\u4e0b\uff0c\u76ee\u5f55\u4e3a\u9879\u76ee\u7684target\\classes\u4e0b\n                \/\/\u5982\u679c\u662flinux\u73af\u5883\u4e0b\uff0c\u76ee\u5f55\u4e3ajar\u5305\u540c\u7ea7\u76ee\u5f55\n                File rootPath = new File(ResourceUtils.getURL(\"classpath:\").getPath());\n                if (!rootPath.exists()) {\n                    rootPath = new File(\"\");\n                }\n                imagePath = new File(rootPath.getAbsolutePath()+file_path);\n            }\n            if (!imagePath.exists()) {\n                \/\/\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\n                imagePath.mkdirs();\n            }\n            \/\/\u4f7f\u7528\u5de5\u5177\u7c7b\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684\u6587\u4ef6\u540d\u9632\u6b62\u6587\u4ef6\u540d\u91cd\u590d\n            String newImageName = IDUtils.getFilename(imageName);\n            \/\/\u521b\u5efa\u56fe\u7247\u5b58\u653e\u5730\u5740\n            File imageResultFile = new File(imagePath + \"\/\" + newImageName);\n           imgRequestUrl = rootUrl + \"\/images\/\" + newImageName;\n            if (imageResultFile.exists()) {\n                log.info(\"\u56fe\u7247\u5df2\u7ecf\u5b58\u5728\uff01\u8be5\u56fe\u7247\u7684\u8bbf\u95ee\u8bf7\u6c42\u5730\u5740\uff1a&#91;{}]\", imgRequestUrl);\n            } else {\n                \/\/\u56fe\u7247\u5c1a\u672a\u5b58\u5728\uff0c\u5c06\u56fe\u7247\u4fdd\u5b58\u5230\u6307\u5b9a\u7684\u8def\u5f84\u4e2d\n                image.transferTo(imageResultFile);\n            }\n            \/\/\u5c06\u8be5\u56fe\u7247\u7684\u5730\u5740\u8bbe\u7f6e\u5230comment\u4e2d\n            comment.setCommentImagePath(imgRequestUrl);\n            \/\/\u56fe\u7247\u5728\u78c1\u76d8\u4e2d\u7684\u5b9e\u9645\u5730\u5740\n\/\/        String imageResultPath = imageResultFile.getCanonicalPath();\n            log.info(\"\u8be5\u56fe\u7247\u7684\u8bbf\u95ee\u8bf7\u6c42\u5730\u5740\uff1a&#91;{}]\", imgRequestUrl);\n        }\n        \/\/\u8bbe\u7f6ecomment\n        comment.setIsDeleted(Comment.DEFAULT_ISDELETED_FALSE);\n        comment.setStatus(Comment.DEFAULT_STATUS_TRUE);\n        comment.setCollection_count(Long.valueOf(0L));\n        comment.setLike_count(Long.valueOf(0L));\n        int fla = commentMapper.insert(comment);\n        if (fla == 1){\n            \/\/\u63d2\u5165\u6210\u529f\u8fd4\u56de\u56fe\u7247\u7684\u5730\u5740\n            return imgRequestUrl;\n        }else{\n            \/\/\u63d2\u5165\u5931\u8d25\u8fd4\u56deNULL\n            return  null;\n        }\n    }<\/code><\/pre>\n\n\n\n<h2>2.\u7136\u540e\u914d\u7f6e\u4e00\u4e2aWebMvcConfigurer\u7528\u4e8e\u89e3\u6790\u9759\u6001\u8d44\u6e90\u56fe\u7247,\u5c06\u670d\u52a1\u5668\u7684\u9759\u6001\u8d44\u6e90\u76ee\u5f55\u901a\u8fc7\u6620\u5c04\u5230\u66b4\u9732\u7684\u8bbf\u95ee\u8def\u5f84\uff1a<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Configuration\npublic class FileConfig implements WebMvcConfigurer {\n    @Value(\"${images.url-path}\")\n    private String file_path ;\n    @Override\n    public void addResourceHandlers(ResourceHandlerRegistry registry) {\n        \/\/\u5728windows\u73af\u5883\u4e0b\u7684\u56fe\u7247\u5b58\u653e\u8d44\u6e90\u8def\u5f84\n        String winPath = System.getProperty(\"user.dir\")+file_path;\n        \/\/\u5728Linux\u73af\u5883\u4e0b\u7684\u56fe\u7247\u5b58\u653e\u8d44\u6e90\u8def\u5f84\n\/\/        String linuxPath = \"\/usr\/local\/my_project\/images\/\";\n        String os = System.getProperty(\"os.name\");\n        if (os.toLowerCase().startsWith(\"win\")) {  \/\/windows\u7cfb\u7edf\n            System.out.println(winPath);\n            \/\/\u7b2c\u4e00\u4e2a\u65b9\u6cd5\u8bbe\u7f6e\u8bbf\u95ee\u8def\u5f84\u524d\u7f00\uff0c\u7b2c\u4e8c\u4e2a\u65b9\u6cd5\u8bbe\u7f6e\u8d44\u6e90\u8def\u5f84\n            registry.addResourceHandler(\"\/images\/**\").\n                    addResourceLocations(\"file:\"+winPath);\n        }else{\/\/linux\u7cfb\u7edf\n            File rootPath = null;\n            try {\n                rootPath = new File(ResourceUtils.getURL(\"classpath:\").getPath());\n            } catch (FileNotFoundException e) {\n                e.printStackTrace();\n            }\n            if(!rootPath.exists()){\n                rootPath = new File(\"\");\n            }\n            System.out.println(rootPath.getAbsolutePath()+file_path);\n\n           File  imagePath = new File(rootPath.getAbsolutePath()+file_path);\n            if(!imagePath.exists()){\n                \/\/\u4e0d\u5b58\u5728\uff0c\u521b\u5efa\n                imagePath.mkdirs();\n            }\n            registry.addResourceHandler(\"\/images\/**\").\n                    addResourceLocations(\"file:\"+rootPath.getAbsolutePath()+file_path);\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<ul><li>addResoureHandler\uff1a\u6307\u7684\u662f\u5bf9\u5916\u66b4\u9732\u7684\u8bbf\u95ee\u8def\u5f84<\/li><li>addResourceLocations\uff1a\u6307\u7684\u662f\u5185\u90e8\u6587\u4ef6\u653e\u7f6e\u7684\u76ee\u5f55<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/428ee59c0eae7d83c594dd0f2d0874ea.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210221104716273\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/428ee59c0eae7d83c594dd0f2d0874ea.png\" alt=\"image-20210221104716273\"\/><\/figure><\/noscript>\n\n\n\n<figure class=\"wp-block-image\"><img   class=\"lazyload\" data-src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/2571d9bdcb2df6f411bf8a082d0f20af.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"image-20210221104630602\"\/><\/figure >\n<noscript><img src=\"https:\/\/img-blog.csdnimg.cn\/img_convert\/2571d9bdcb2df6f411bf8a082d0f20af.png\" alt=\"image-20210221104630602\"\/><\/figure><\/noscript>\n","protected":false},"excerpt":{"rendered":"<p>1.\u9996\u5148springboot\u9879\u76ee\u5728Window\u548cLinux\u670d\u52a1\u5668\u7684\u9879\u76ee\u8d44\u6e90\u8def\u5f84\u662f\u4e0d\u4e00\u6837\u7684\uff0c\u9700\u8981\u5206\u5f00\u6765\u8bbe\u7f6e\u8def\u5f84\uff1a 2.\u7136\u540e\u914d\u7f6e\u4e00\u4e2aW &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[202],"tags":[212,215,213,214],"_links":{"self":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/476"}],"collection":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/comments?post=476"}],"version-history":[{"count":1,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/476\/revisions"}],"predecessor-version":[{"id":477,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/posts\/476\/revisions\/477"}],"wp:attachment":[{"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/media?parent=476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/categories?post=476"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/moyisuiying.com\/index.php\/wp-json\/wp\/v2\/tags?post=476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}