{"id":1146,"date":"2020-08-30T21:37:24","date_gmt":"2020-08-30T13:37:24","guid":{"rendered":"http:\/\/diuut.com\/?p=1146"},"modified":"2020-08-30T22:05:24","modified_gmt":"2020-08-30T14:05:24","slug":"webflux%e4%b8%ad%e4%bd%bf%e7%94%a8%e9%98%bf%e9%87%8c%e4%ba%91oss%e6%96%87%e4%bb%b6%e4%b8%8a%e4%bc%a0","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1146","title":{"rendered":"WebFlux\u4e2d\u4f7f\u7528\u963f\u91cc\u4e91OSS\u6587\u4ef6\u4e0a\u4f20"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42\uff1a\u4e0a\u4f20\u56fe\u7247\u8fd4\u56de\u5730\u5740\u3002\uff08WebFlux\u4e0b\uff09<\/p><\/blockquote>\n\n\n\n<p>\u5bfc\u5165\u4f9d\u8d56\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n  &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-starter-webflux&lt;\/artifactId&gt;\n  &lt;\/dependency&gt;\n  &lt;dependency&gt;\n            &lt;groupId&gt;com.aliyun&lt;\/groupId&gt;\n            &lt;artifactId&gt;aliyun-java-sdk-core&lt;\/artifactId&gt;\n  &lt;\/dependency&gt;\n  &lt;dependency&gt;\n            &lt;groupId&gt;com.aliyun.oss&lt;\/groupId&gt;\n            &lt;artifactId&gt;aliyun-sdk-oss&lt;\/artifactId&gt;\n  &lt;\/dependency&gt;\n<\/pre><\/div>\n\n\n<p>OssUtil\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Component\npublic class OssUtil {\n\n    private String endpoint=&quot;\u4f60\u7684oss\u5730\u5740&quot;;\n    private String accessKeyId=&quot;\u4f60\u7684accessKeyId&quot;;\n    private String accessKeySecret=&quot;\u4f60\u7684accessKeySecret&quot;;\n    private String bucketName=&quot;mrk&quot;;  \/\/\u5bb9\u5668\u540d\n\n    private OSSClient ossClient=new OSSClient(endpoint, accessKeyId, accessKeySecret);\n\n    public String updateHead(InputStream instream, String fileName){\n        try {\n            ossClient.putObject(bucketName,fileName,instream);\n        } catch (Exception e) {\n           return &quot;false&quot;;\n        }\n        return &quot;https:\/\/&quot;+bucketName+&quot;.oss-cn-hangzhou.aliyuncs.com\/&quot;+fileName;\n    }\n}\n<\/pre><\/div>\n\n\n<p>Controller\u5c42\u4e0eService\u5c42\u6839\u636e\u81ea\u5df1\u7684\u4e1a\u52a1\u903b\u8f91\u8fdb\u884c\u7f16\u5199\uff0c\u5728webFlux\u4e0b\u4e3b\u8981\u5c31\u662f\u4f20\u8fc7\u6765\u7684\u63a5\u6536\u65b9\u5f0f\u9700\u8981\u6ce8\u610f\uff0c\u534f\u5546\u524d\u53f0\u901a\u8fc7form-data\u8868\u5355\u8fdb\u884c\u63d0\u4ea4\uff0cOssClent\u63a5\u53d7\u7684\u662finputStrem\u6d41\uff0cController\u5c42\u5728server-web\u5305\u4e0b\u4e00\u822c\u4f1a\u4f7f\u7528@requestParm\u63a5\u6536 MultipartFile  \u6587\u4ef6\u8fdb\u884c\u8f6c\u6d41\u5904\u7406\uff0c\u5728WebFlux\u4e0b\u5219\u91c7\u7528@requestPart\u6765\u63a5\u6536\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/controller\u5c42\n@RequestMapping(value = &quot;\/uploadhead&quot;)\n    public Object requestBodyFlux(@RequestPart(&quot;file&quot;)FilePart file, @RequestPart(&quot;\u8d26\u53f7&quot;)String name,@RequestPart(&quot;token&quot;)String token) {\n        log.info(&quot;files:{}&quot;,file);\n        retruen Service.loadFlie(file);\n}\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/\/service\u5c42\n    \/**\n     * \u8bbe\u7f6e\u5934\u50cf\n     *\n     * @param filePart \u5934\u50cf\u6587\u4ef6\n     * @return \u5934\u50cf\u5730\u5740\n     *\/\n    public Object uploadhead(FilePart filePart) throws IOException {\n        Path tempFile = Files.createTempFile(&quot;head&quot;, filePart.filename());\n        filePart.transferTo(tempFile.toFile()).subscribe();\n        File file = tempFile.toFile();\n        FileInputStream fileInputStream = new FileInputStream(file);\n        String fileName = &quot;headPic&quot; + UUID.randomUUID() + &quot;.png&quot;;\n        String folder = &quot;head&quot;;\n        String url = ossUtil.updateHead(fileInputStream, folder + &quot;\/&quot; + fileName);\n        if (&quot;false&quot;.equals(url)) {\n              return &quot;\u56fe\u7247\u4e0a\u4f20\u5931\u8d25&quot;;\n           } else {\n              user1.setHead(url);\n              userRedisDao.save(user1);\n              return url;\n           }\n    }\n<\/pre><\/div>\n\n\n<p>\u5982\u679c\u4e0d\u51fa\u610f\u5916\u5730\u8bdd\uff0c\u63a5\u6536\u6b63\u5e38\uff0c\u8fd4\u56de\u4e5f\u4f1a\u6b63\u5e38\uff0c\u4f46\u610f\u5916\u4e0d\u51fa\u6765\u7684\u8bdd\u5c31\u4e0d\u53eb\u610f\u5916\u7684\uff0c\u603b\u4f1a\u6709\u610f\u6599\u4e4b\u5916\u7684\u60c5\u51b5\uff0c\u5982\u679c\u6709\u524d\u53f0\u901a\u8fc7@requestParm\u6216\u8005@requestPart\u4f20\u6587\u4ef6\u90fd\u63a5\u6536\u4e0d\u4e86\u7684\u60c5\u51b5\uff0c\u5c31\u5f97\u81ea\u5df1\u4ece\u8bf7\u6c42\u91cc\u9762\u53d6\u4e86\uff0c\u76f8\u5bf9\u5c31\u9ebb\u70e6\u4e00\u70b9\uff0c\u624b\u52a8\u53d6\u53c2\u65b9\u6cd5\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4f1a\u8bf4\u660e\u3002<\/p>\n\n\n\n<p>\u6587\u7ae0\uff1a<a href=\"https:\/\/diuut.com\/?p=1149\">\u5728WebFlux\u4e0b\u4eceServerWebExchange\u4e2d\u83b7\u53d6\u53c2\u6570<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a\u4e0a\u4f20\u56fe\u7247\u8fd4\u56de\u5730\u5740\u3002\uff08WebFlux\u4e0b\uff09 \u5bfc\u5165\u4f9d\u8d56\uff1a OssUtil\uff1a Controller\u5c42\u4e0eServi<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1146\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">WebFlux\u4e2d\u4f7f\u7528\u963f\u91cc\u4e91OSS\u6587\u4ef6\u4e0a\u4f20<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":1147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1146"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1146"}],"version-history":[{"count":4,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":1153,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1146\/revisions\/1153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/1147"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}