From da7d619d1b2ce29c0282d41b756fa601c70bbe6c Mon Sep 17 00:00:00 2001 From: "Yangkai.Shen" <237497819@qq.com> Date: Tue, 6 Nov 2018 18:00:39 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20spring-boot-demo-upload=20?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring-boot-demo-upload/pom.xml | 6 + .../upload/controller/UploadController.java | 106 +++++++++++ .../upload/service/IQiNiuService.java | 30 +++ .../upload/service/impl/QiNiuServiceImpl.java | 80 ++++++++ .../src/main/resources/application.yml | 10 +- .../src/main/resources/templates/index.html | 171 +++++++++++++++--- 6 files changed, 371 insertions(+), 32 deletions(-) create mode 100644 spring-boot-demo-upload/src/main/java/com/xkcoding/upload/controller/UploadController.java create mode 100644 spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/IQiNiuService.java create mode 100644 spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/impl/QiNiuServiceImpl.java diff --git a/spring-boot-demo-upload/pom.xml b/spring-boot-demo-upload/pom.xml index 042defe..8d9b5ec 100644 --- a/spring-boot-demo-upload/pom.xml +++ b/spring-boot-demo-upload/pom.xml @@ -23,6 +23,12 @@ + + org.projectlombok + lombok + true + + org.springframework.boot spring-boot-starter-web diff --git a/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/controller/UploadController.java b/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/controller/UploadController.java new file mode 100644 index 0000000..e31dd42 --- /dev/null +++ b/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/controller/UploadController.java @@ -0,0 +1,106 @@ +package com.xkcoding.upload.controller; + +import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.Dict; +import cn.hutool.core.util.StrUtil; +import cn.hutool.json.JSONObject; +import cn.hutool.json.JSONUtil; +import com.qiniu.http.Response; +import com.xkcoding.upload.service.IQiNiuService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +import java.io.File; +import java.io.IOException; + +/** + *

+ * 文件上传 Controller + *

+ * + * @package: com.xkcoding.upload.controller + * @description: 文件上传 Controller + * @author: yangkai.shen + * @date: Created in 2018/11/6 16:33 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@RestController +@Slf4j +@RequestMapping("/upload") +public class UploadController { + @Value("${spring.servlet.multipart.location}") + private String fileTempPath; + + @Value("${qiniu.prefix}") + private String prefix; + + private final IQiNiuService qiNiuService; + + @Autowired + public UploadController(IQiNiuService qiNiuService) { + this.qiNiuService = qiNiuService; + } + + @PostMapping(value = "/local", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public Dict local(@RequestParam("file") MultipartFile file) { + if (file.isEmpty()) { + return Dict.create().set("code", 400).set("message", "文件内容为空"); + } + String fileName = file.getOriginalFilename(); + String rawFileName = StrUtil.subBefore(fileName, ".", true); + String fileType = StrUtil.subAfter(fileName, ".", true); + String localFilePath = StrUtil.appendIfMissing(fileTempPath, "/") + rawFileName + "-" + DateUtil.current(false) + "." + fileType; + try { + file.transferTo(new File(localFilePath)); + } catch (IOException e) { + log.error("【文件上传至本地】失败,绝对路径:{}", localFilePath); + return Dict.create().set("code", 500).set("message", "文件上传失败"); + } + + log.info("【文件上传至本地】绝对路径:{}", localFilePath); + return Dict.create().set("code", 200).set("message", "上传成功").set("data", Dict.create().set("fileName", fileName).set("filePath", localFilePath)); + } + + @PostMapping(value = "/yun", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public Dict yun(@RequestParam("file") MultipartFile file) { + if (file.isEmpty()) { + return Dict.create().set("code", 400).set("message", "文件内容为空"); + } + String fileName = file.getOriginalFilename(); + String rawFileName = StrUtil.subBefore(fileName, ".", true); + String fileType = StrUtil.subAfter(fileName, ".", true); + String localFilePath = StrUtil.appendIfMissing(fileTempPath, "/") + rawFileName + "-" + DateUtil.current(false) + "." + fileType; + try { + file.transferTo(new File(localFilePath)); + Response response = qiNiuService.uploadFile(new File(localFilePath)); + if (response.isOK()) { + JSONObject jsonObject = JSONUtil.parseObj(response.bodyString()); + + String yunFileName = jsonObject.getStr("key"); + String yunFilePath = StrUtil.appendIfMissing(prefix, "/") + yunFileName; + + FileUtil.del(new File(localFilePath)); + + log.info("【文件上传至七牛云】绝对路径:{}", yunFilePath); + return Dict.create().set("code", 200).set("message", "上传成功").set("data", Dict.create().set("fileName", yunFileName).set("filePath", yunFilePath)); + } else { + log.error("【文件上传至七牛云】失败,{}", JSONUtil.toJsonStr(response)); + FileUtil.del(new File(localFilePath)); + return Dict.create().set("code", 500).set("message", "文件上传失败"); + } + } catch (IOException e) { + log.error("【文件上传至七牛云】失败,绝对路径:{}", localFilePath); + return Dict.create().set("code", 500).set("message", "文件上传失败"); + } + } +} diff --git a/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/IQiNiuService.java b/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/IQiNiuService.java new file mode 100644 index 0000000..15a15be --- /dev/null +++ b/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/IQiNiuService.java @@ -0,0 +1,30 @@ +package com.xkcoding.upload.service; + +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; + +import java.io.File; + +/** + *

+ * 七牛云上传Service + *

+ * + * @package: com.xkcoding.upload.service + * @description: 七牛云上传Service + * @author: yangkai.shen + * @date: Created in 2018/11/6 17:21 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +public interface IQiNiuService { + /** + * 七牛云上传文件 + * + * @param file 文件 + * @return 七牛上传Response + * @throws QiniuException 七牛异常 + */ + Response uploadFile(File file) throws QiniuException; +} diff --git a/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/impl/QiNiuServiceImpl.java b/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/impl/QiNiuServiceImpl.java new file mode 100644 index 0000000..8993e64 --- /dev/null +++ b/spring-boot-demo-upload/src/main/java/com/xkcoding/upload/service/impl/QiNiuServiceImpl.java @@ -0,0 +1,80 @@ +package com.xkcoding.upload.service.impl; + +import com.qiniu.common.QiniuException; +import com.qiniu.http.Response; +import com.qiniu.storage.UploadManager; +import com.qiniu.util.Auth; +import com.qiniu.util.StringMap; +import com.xkcoding.upload.service.IQiNiuService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.File; + +/** + *

+ * 七牛云上传Service + *

+ * + * @package: com.xkcoding.upload.service.impl + * @description: 七牛云上传Service + * @author: yangkai.shen + * @date: Created in 2018/11/6 17:22 + * @copyright: Copyright (c) 2018 + * @version: V1.0 + * @modified: yangkai.shen + */ +@Service +@Slf4j +public class QiNiuServiceImpl implements IQiNiuService, InitializingBean { + private final UploadManager uploadManager; + + private final Auth auth; + + @Value("${qiniu.bucket}") + private String bucket; + + private StringMap putPolicy; + + @Autowired + public QiNiuServiceImpl(UploadManager uploadManager, Auth auth) { + this.uploadManager = uploadManager; + this.auth = auth; + } + + /** + * 七牛云上传文件 + * + * @param file 文件 + * @return 七牛上传Response + * @throws QiniuException 七牛异常 + */ + @Override + public Response uploadFile(File file) throws QiniuException { + Response response = this.uploadManager.put(file, file.getName(), getUploadToken()); + int retry = 0; + while (response.needRetry() && retry < 3) { + response = this.uploadManager.put(file, file.getName(), getUploadToken()); + retry++; + } + return response; + } + + @Override + public void afterPropertiesSet() { + this.putPolicy = new StringMap(); + putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"width\":$(imageInfo.width), \"height\":${imageInfo.height}}"); + } + + /** + * 获取上传凭证 + * + * @return 上传凭证 + */ + private String getUploadToken() { + return this.auth.uploadToken(bucket, null, 3600, putPolicy); + } +} diff --git a/spring-boot-demo-upload/src/main/resources/application.yml b/spring-boot-demo-upload/src/main/resources/application.yml index fc01525..828d856 100644 --- a/spring-boot-demo-upload/src/main/resources/application.yml +++ b/spring-boot-demo-upload/src/main/resources/application.yml @@ -3,8 +3,14 @@ server: servlet: context-path: /demo qiniu: - accessKey: ## 此处填写你自己的七牛云 access key - secretKey: ## 此处填写你自己的七牛云 secret key + ## 此处填写你自己的七牛云 access key + accessKey: + ## 此处填写你自己的七牛云 secret key + secretKey: + ## 此处填写你自己的七牛云 bucket + bucket: + ## 此处填写你自己的七牛云 域名 + prefix: spring: servlet: multipart: diff --git a/spring-boot-demo-upload/src/main/resources/templates/index.html b/spring-boot-demo-upload/src/main/resources/templates/index.html index 446c77b..72b2327 100644 --- a/spring-boot-demo-upload/src/main/resources/templates/index.html +++ b/spring-boot-demo-upload/src/main/resources/templates/index.html @@ -7,64 +7,175 @@ spring-boot-demo-upload - + - + - +
- - 选择文件 - - - {{ localUpload.loadingStatus ? '上传中' : '上传' }} - + + + +

+ + 本地上传 +

+
+ + 选择文件 + + + {{ local.loadingStatus ? '本地文件上传中' : '本地上传' }} + +
+
+
状态:{{local.log.message}}
+
文件名:{{local.log.fileName}}
+
文件路径:{{local.log.filePath}}
+
+
+
+ + +

+ + 七牛云上传 +

+
+ + 选择文件 + + + {{ yun.loadingStatus ? '七牛云文件上传中' : '七牛云上传' }} + +
+
+
状态:{{yun.log.message}}
+
文件名:{{yun.log.fileName}}
+
文件路径:{{yun.log.filePath}}
+
+
+
+