Commit 4de57bf2 by yangjinhu

UiBot上传七牛云接口

parent 07e548c6
...@@ -69,6 +69,13 @@ ...@@ -69,6 +69,13 @@
<version>3.17</version> <version>3.17</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId> <artifactId>poi-ooxml-schemas</artifactId>
...@@ -86,7 +93,12 @@ ...@@ -86,7 +93,12 @@
<groupId>io.springfox</groupId> <groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId> <artifactId>springfox-swagger2</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency> <dependency>
<groupId>io.swagger</groupId> <groupId>io.swagger</groupId>
......
...@@ -5,6 +5,7 @@ import com.blt.other.common.annotation.LoginIgnore; ...@@ -5,6 +5,7 @@ import com.blt.other.common.annotation.LoginIgnore;
import com.blt.other.common.exception.BizRuntimeException; import com.blt.other.common.exception.BizRuntimeException;
import com.blt.other.common.util.PathUtil; import com.blt.other.common.util.PathUtil;
import com.blt.other.module.cost.utils.CostFileUtil; import com.blt.other.module.cost.utils.CostFileUtil;
import com.blt.other.module.cost.utils.QNUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -37,4 +38,16 @@ public class FileController { ...@@ -37,4 +38,16 @@ public class FileController {
} }
throw new BizRuntimeException("文件不能为空"); throw new BizRuntimeException("文件不能为空");
} }
@LoginIgnore
@ApiOperation("七牛云UiBot上传文件接口")
@PostMapping("uiBot/uploadXlsx")
public CostResult<String> xUpload(MultipartFile file) {
if (file != null) {
String url = QNUtils.upLoad(file);
return CostResult.success(url);
}
throw new BizRuntimeException("文件不能为空");
}
} }
package com.blt.other.module.cost.utils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
public class QNUtils {
/**
* Key
*/
private static final String ACCESS_KEY = "QSvtvN4Ons1CiNzaMGqx8XmDaiM1L0ZqSwJ2YoTn";
/**
* 秘钥
*/
private static final String SECRET_KEY = "yagRd-cBOVhkRGGT-o_reMqNVjI8_k7YwoTXkhrm";
/**
* 存储桶
*/
private static final String BUCKET = "uibot";
private static final String BASE_URL = "qv3ist3f9.hn-bkt.clouddn.com";
/**
* 上传管理
*/
private static UploadManager uploadManager;
private QNUtils() {
//构造一个带指定 Region 对象的配置类
Configuration config = new Configuration(Region.region2());
uploadManager = new UploadManager(config);
}
private static UploadManager getInstance() {
if (uploadManager == null) {
new QNUtils();
}
return uploadManager;
}
/**
* 生成上传凭证Token
*
* @return
*/
public static String getToken() {
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String upToken = auth.uploadToken(BUCKET);
System.out.println(upToken);
return upToken;
}
public static String upLoad(MultipartFile file) {
// 默认不指定key的情况下,以文件内容的hash值作为文件名
String key = file.getOriginalFilename();
String url="";
try {
InputStream inputStream = file.getInputStream();
// byte[] byteData = IOUtils.toByteArray(inputStream);//把流转字节数组
Response response = getInstance().put(inputStream, key, getToken(), null, null);
// Response response = getInstance().put(byteData, key, getToken());
//解析上传成功的结果
try {
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
url = BASE_URL+putRet.key;
} catch (QiniuException e) {
Response r = e.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
} catch (IOException e) {
e.printStackTrace();
}
return url;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment