diff --git a/pom.xml b/pom.xml index 4d4456b..0f2a73f 100644 --- a/pom.xml +++ b/pom.xml @@ -100,6 +100,10 @@ com.vaadin.external.google android-json + + asm + org.ow2.asm + test @@ -224,6 +228,12 @@ org.apache.httpcomponents httpclient 4.5.1 + + + httpcore + org.apache.httpcomponents + + @@ -236,12 +246,35 @@ com.github.qcloudsms qcloudsms 1.0.5 + + + httpclient + org.apache.httpcomponents + + com.alibaba fastjson 1.2.62 + + + com.qcloud + cos_api + 5.6.54 + + + httpclient + org.apache.httpcomponents + + + okio + com.squareup.okio + + + + diff --git a/ptos_tall/pom.xml b/ptos_tall/pom.xml index cba9d41..c0465f2 100644 --- a/ptos_tall/pom.xml +++ b/ptos_tall/pom.xml @@ -23,6 +23,28 @@ util_cloud + + + bcprov-jdk15on + org.bouncycastle + + + httpcore + org.apache.httpcomponents + + + netty-all + io.netty + + + httpclient + org.apache.httpcomponents + + + jackson-dataformat-xml + com.fasterxml.jackson.dataformat + + com.ccsensptos 1.0-SNAPSHOT @@ -31,10 +53,62 @@ util 1.0-SNAPSHOT compile + + + fastjson + com.alibaba + + + sdk-core-java + com.aliyun.api.gateway + + + bcprov-jdk14 + org.bouncycastle + + + bcmail-jdk14 + org.bouncycastle + + + httpcore + org.apache.httpcomponents + + + netty-all + io.netty + + + httpclient + org.apache.httpcomponents + + + jackson-dataformat-xml + com.fasterxml.jackson.dataformat + + util_wechat + + + httpcore + org.apache.httpcomponents + + + netty-all + io.netty + + + httpclient + org.apache.httpcomponents + + + jackson-dataformat-xml + com.fasterxml.jackson.dataformat + + com.ccsensptos 1.0-SNAPSHOT diff --git a/ptos_tall/src/main/java/com/ccsens/ptos_tall/api/DebugController.java b/ptos_tall/src/main/java/com/ccsens/ptos_tall/api/DebugController.java index 265667b..703c152 100644 --- a/ptos_tall/src/main/java/com/ccsens/ptos_tall/api/DebugController.java +++ b/ptos_tall/src/main/java/com/ccsens/ptos_tall/api/DebugController.java @@ -1,6 +1,8 @@ package com.ccsens.ptos_tall.api; import com.ccsens.util.JsonResponse; +import com.ccsens.util.PropUtil; +import com.ccsens.util.TxCosUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; @@ -10,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.Part; /** * @author 逗 @@ -29,4 +32,22 @@ public class DebugController { 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); + } } diff --git a/util/pom.xml b/util/pom.xml index e7c1b41..3382280 100644 --- a/util/pom.xml +++ b/util/pom.xml @@ -21,6 +21,16 @@ org.xhtmlrenderer flying-saucer-pdf 9.1.19 + + + bcmail-jdk14 + org.bouncycastle + + + bcprov-jdk14 + org.bouncycastle + + com.itextpdf @@ -43,6 +53,16 @@ com.aliyun.api.gateway sdk-core-java 1.1.6 + + + httpclient + org.apache.httpcomponents + + + fastjson + com.alibaba + + diff --git a/util/src/main/java/com/ccsens/util/TxCosUtil.java b/util/src/main/java/com/ccsens/util/TxCosUtil.java new file mode 100644 index 0000000..943a0bf --- /dev/null +++ b/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; + } +}