Browse Source

上传腾讯cos存储桶的工具类

master
zhangye 3 years ago
parent
commit
0c2fc0028e
  1. 33
      pom.xml
  2. 74
      ptos_tall/pom.xml
  3. 21
      ptos_tall/src/main/java/com/ccsens/ptos_tall/api/DebugController.java
  4. 20
      util/pom.xml
  5. 103
      util/src/main/java/com/ccsens/util/TxCosUtil.java

33
pom.xml

@ -100,6 +100,10 @@
<groupId>com.vaadin.external.google</groupId> <groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId> <artifactId>android-json</artifactId>
</exclusion> </exclusion>
<exclusion>
<artifactId>asm</artifactId>
<groupId>org.ow2.asm</groupId>
</exclusion>
</exclusions> </exclusions>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
@ -224,6 +228,12 @@
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>4.5.1</version> <version>4.5.1</version>
<exclusions>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore --> <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency> <dependency>
@ -236,12 +246,35 @@
<groupId>com.github.qcloudsms</groupId> <groupId>com.github.qcloudsms</groupId>
<artifactId>qcloudsms</artifactId> <artifactId>qcloudsms</artifactId>
<version>1.0.5</version> <version>1.0.5</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
<version>1.2.62</version> <version>1.2.62</version>
</dependency> </dependency>
<!-- 腾讯cos -->
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.54</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>okio</artifactId>
<groupId>com.squareup.okio</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>
<!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 -->

74
ptos_tall/pom.xml

@ -23,6 +23,28 @@
<!--cloud 工具类--> <!--cloud 工具类-->
<dependency> <dependency>
<artifactId>util_cloud</artifactId> <artifactId>util_cloud</artifactId>
<exclusions>
<exclusion>
<artifactId>bcprov-jdk15on</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>netty-all</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-dataformat-xml</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
</exclusion>
</exclusions>
<groupId>com.ccsensptos</groupId> <groupId>com.ccsensptos</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>
@ -31,10 +53,62 @@
<artifactId>util</artifactId> <artifactId>util</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
<exclusion>
<artifactId>sdk-core-java</artifactId>
<groupId>com.aliyun.api.gateway</groupId>
</exclusion>
<exclusion>
<artifactId>bcprov-jdk14</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>bcmail-jdk14</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>netty-all</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-dataformat-xml</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!--微信工具包--> <!--微信工具包-->
<dependency> <dependency>
<artifactId>util_wechat</artifactId> <artifactId>util_wechat</artifactId>
<exclusions>
<exclusion>
<artifactId>httpcore</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>netty-all</artifactId>
<groupId>io.netty</groupId>
</exclusion>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-dataformat-xml</artifactId>
<groupId>com.fasterxml.jackson.dataformat</groupId>
</exclusion>
</exclusions>
<groupId>com.ccsensptos</groupId> <groupId>com.ccsensptos</groupId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
</dependency> </dependency>

21
ptos_tall/src/main/java/com/ccsens/ptos_tall/api/DebugController.java

@ -1,6 +1,8 @@
package com.ccsens.ptos_tall.api; package com.ccsens.ptos_tall.api;
import com.ccsens.util.JsonResponse; import com.ccsens.util.JsonResponse;
import com.ccsens.util.PropUtil;
import com.ccsens.util.TxCosUtil;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
@ -10,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
/** /**
* @author * @author
@ -29,4 +32,22 @@ public class DebugController {
return JsonResponse.newInstance().ok("测试"); return JsonResponse.newInstance().ok("测试");
} }
@ApiOperation(value = "文件上传")
@ApiImplicitParams({
})
@RequestMapping(value = "debug", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
public JsonResponse debug(Part file) throws Exception {
String secretId = "AKIDTr6B7uA2qCRWRDRXyeUAZpkfR2tupVFu";
String secretKey = "wVtKAuXEjXFpxQmz9PM6hWallNEyZ0Na";
String region = "ap-beijing";
String bucketName = "toutiaoxuanchuan-1258473962";
String path = "https://toutiaoxuanchuan-1258473962.cos.ap-beijing.myqcloud.com/";
log.info("文件上传");
TxCosUtil.FileInfo fileInfo = new TxCosUtil.FileInfo(file.getInputStream(),file.getSubmittedFileName(),file.getSize());
//1.上传文件
String s = TxCosUtil.upLoadCos(PropUtil.path, fileInfo, secretId, secretKey, bucketName, region);
return JsonResponse.newInstance().ok(path + s);
}
} }

20
util/pom.xml

@ -21,6 +21,16 @@
<groupId>org.xhtmlrenderer</groupId> <groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId> <artifactId>flying-saucer-pdf</artifactId>
<version>9.1.19</version> <version>9.1.19</version>
<exclusions>
<exclusion>
<artifactId>bcmail-jdk14</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
<exclusion>
<artifactId>bcprov-jdk14</artifactId>
<groupId>org.bouncycastle</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.itextpdf</groupId> <groupId>com.itextpdf</groupId>
@ -43,6 +53,16 @@
<groupId>com.aliyun.api.gateway</groupId> <groupId>com.aliyun.api.gateway</groupId>
<artifactId>sdk-core-java</artifactId> <artifactId>sdk-core-java</artifactId>
<version>1.1.6</version> <version>1.1.6</version>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>fastjson</artifactId>
<groupId>com.alibaba</groupId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!--发送邮件--> <!--发送邮件-->
<dependency> <dependency>

103
util/src/main/java/com/ccsens/util/TxCosUtil.java

@ -0,0 +1,103 @@
package com.ccsens.util;
import cn.hutool.core.util.StrUtil;
import com.ccsens.util.exception.BaseException;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.*;
import com.qcloud.cos.region.Region;
import lombok.Data;
import java.io.*;
/**
* @author
*/
public class TxCosUtil {
@Data
public static class FileInfo{
private InputStream inputStream;
private String fileName;
private Long fileSize;
public FileInfo(InputStream inputStream, String fileName, Long fileSize) {
this.inputStream = inputStream;
this.fileName = fileName;
this.fileSize = fileSize;
}
}
/**
* 上传文件
*/
public static String upLoadCos(String filePath, FileInfo fileInfo, String secretId, String secretKey, String bucketName, String region) throws Exception {
String key = getFileKey(filePath,fileInfo.getFileName());
try {
ObjectMetadata objectMetadata = new ObjectMetadata();
// 上传的流如果能够获取准确的流长度,则推荐一定填写 content-length
// 如果确实没办法获取到,则下面这行可以省略,但同时高级接口也没办法使用分块上传了
objectMetadata.setContentLength(fileInfo.getFileSize());
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, fileInfo.inputStream, objectMetadata);
COSClient cosClient = createCosClient(secretId, secretKey, region);
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
} catch (Exception e) {
throw new BaseException("上传文件至COS失败");
}
return key;
}
/**
* 生成cos客户端
*/
public static COSClient createCosClient(String secretId, String secretKey,String region) {
// 设置用户身份信息。
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// ClientConfig 中包含了后续请求 COS 的客户端设置:
ClientConfig clientConfig = new ClientConfig();
// 设置 bucket 的地域
// COS_REGION 请参照 https://cloud.tencent.com/document/product/436/6224
clientConfig.setRegion(new Region(region));
// 设置请求协议, http 或者 https
// 5.6.53 及更低的版本,建议设置使用 https 协议
// 5.6.54 及更高版本,默认使用了 https
clientConfig.setHttpProtocol(HttpProtocol.https);
// 以下的设置,是可选的:
// // 设置 socket 读取超时,默认 30s
// clientConfig.setSocketTimeout(30*1000);
// // 设置建立连接超时,默认 30s
// clientConfig.setConnectionTimeout(30*1000);
//
// // 如果需要的话,设置 http 代理,ip 以及 port
// clientConfig.setHttpProxyIp("httpProxyIp");
// clientConfig.setHttpProxyPort(80);
// 生成 cos 客户端。
return new COSClient(cred, clientConfig);
}
/**
* 生成文件路径
*/
private static String getFileKey(String filePath, String fileName) {
//1.获取后缀名 2.去除文件后缀 替换所有特殊字符
String fileType;
try {
fileType = fileName.substring(fileName.lastIndexOf("."));
}catch (Exception e){
fileType = "";
}
String fileStr = StrUtil.removeSuffix(fileName, fileType).replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5]", "_");
filePath += System.currentTimeMillis() + "_" + fileStr + fileType;
return filePath;
}
}
Loading…
Cancel
Save