controller层获取图片数据调用upload方法
String imagesUrl = null;
//获取图片信息
String imageBaseStr = reqData.getImageBaseStr();
//获取上传路径(我这里用到的数据字典取得服务器路径)
SysDictElement ele = rpcSysDictQuery.getSysDictElementByDictKeyAndEleKey("file_path", "file_path");
//上传图片
if (StringUtils.isNotBlank(imageBaseStr)) {
String id = UUIDUtils.id(20);
String fileKey = id;
//图片路径 和nginx配置统一一下
fileKey = "/"+ "guinness"+"/" +"hb"+ "/" + id + ".jpg";
}
// 拼接图片地址:域名+路径 用于返回的图片地址
imagesUrl = ele.getEleValue() + fileKey;
// 调用图片上传接口 (将data:image/jpeg;base64,替换成"")
String base64Str = imageBaseStr.replaceAll("data:image/jpeg;base64,", "");
log.info("======图片上传后参数[{}]=====",base64Str);
//限制文件大小(根据需要加或者不加)
SysDictElement IsLimit = rpcSysDictQuery.getSysDictElementByDictKeyAndEleKeyAndLang("limit", "limit", "no");
if ("yes".equals(IsLimit.getEleValue())){
boolean isOver = greaterThanSize(base64Str, 5);
if (isOver) {
log.info("App图片==================================该文件超出5M");
return;
}
}
//上传图片调用腾讯上传方法
String str = TencentCOSHelper.uploadFileBase64(base64Str, fileKey);
log.info("App图片上传==================================测试str={}", str);
if (StringUtils.isBlank(str)) {// 图片上传不成功
log.info("图片上传不成功");
code = "400";
message = sysConfigHelper.valueOfSysDictElement(this.getName(), "400", "图片上传不成功");
response.setMeta(AppResponseMeta.builder().code("400").success(false).message(message).build());
return;
}
}
上传方法BASE64Decoder().decodeBuffer解码转换成流 上传
public static String uploadFileBase64(String base64Str,String fileKey) {
String result="";
cosClient=getClient();
if(cosClient==null) {
return result;
}
try {
byte[] bytes = new BASE64Decoder().decodeBuffer(base64Str.trim());
//转化为输入流
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ObjectMetadata objectMetadata = new ObjectMetadata();
// 设置 Content type, 默认是 application/octet-stream
objectMetadata.setContentType("image/jpeg");
//对象键(Key)是对象在存储桶中的唯一标识。
//例如,在对象的访问域名https://ba-189629.cos.ap-beijing.myqcloud.com/app/img/bb/profile_big.jpg
//则把key设为app/img/bb/profile_big.jpg //云控制台目录//ba-189629-->app-->img-->bb
PutObjectResult putObjectResult = cosClient.putObject(bucketName, "/app"+fileKey, inputStream, objectMetadata);
result = putObjectResult.getETag(); // 获取文件的 etag
log.info("uploadFileBase64==result=======result:{}",result);
//System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
//上传失败
}finally {
//关闭客户端(关闭后台线程)
cosClient.shutdown();
}
return result;
}
获取客户端信息
private static COSClient cosClient =null;
public static Properties properties;
public static String bucketName;
public static COSClient getClient() {
if (null == cosClient) {
try {
if(null==properties) {
properties = PropertiesLoaderUtils.loadAllProperties("application.properties");
}
String secretId=properties.getProperty("tencent.secretId");
String secretKey=properties.getProperty("tencent.secretKey");
String region=properties.getProperty("tencent.region");//"ap-beijing"
// 存储桶名称
bucketName =properties.getProperty("tencent.bucketName");
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
ClientConfig clientConfig = new ClientConfig(new Region(region));
cosClient = new COSClient(cred, clientConfig);
} catch (IOException e) {
e.printStackTrace();
}
}
return cosClient;
}
每次都忘记--记录一下