|
|
|
@ -1,10 +1,9 @@ |
|
|
|
package com.bnyer.file.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.net.multipart.UploadFile; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.nacos.common.codec.Base64; |
|
|
|
import com.bnyer.common.core.domain.R; |
|
|
|
import com.bnyer.file.config.QiniuConfig; |
|
|
|
import com.bnyer.file.service.IQiniuService; |
|
|
|
import com.bnyer.file.utils.StringUtil; |
|
|
|
import com.google.gson.Gson; |
|
|
|
@ -18,7 +17,7 @@ import com.qiniu.storage.UploadManager; |
|
|
|
import com.qiniu.storage.model.DefaultPutRet; |
|
|
|
import com.qiniu.util.Auth; |
|
|
|
import com.qiniu.util.StringMap; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
@ -33,23 +32,15 @@ import java.util.ArrayList; |
|
|
|
* @Date: 2022-06-08-10:51 |
|
|
|
* @Description: |
|
|
|
*/ |
|
|
|
@Service("qiniuService") |
|
|
|
@Service |
|
|
|
public class QiniuServiceImpl implements IQiniuService { |
|
|
|
@Value("${qiniu.accessKey}") |
|
|
|
private static String accessKey; |
|
|
|
|
|
|
|
@Value("${qiniu.secretKey}") |
|
|
|
private static String secretKey; |
|
|
|
|
|
|
|
@Value("${qiniu.bucketName}") |
|
|
|
private static String bucketName; |
|
|
|
|
|
|
|
@Value("${qiniu.url}") |
|
|
|
private static String url; |
|
|
|
@Autowired |
|
|
|
private QiniuConfig qiniuConfig; |
|
|
|
|
|
|
|
private String token = null; |
|
|
|
private UploadManager uploadManager; |
|
|
|
private static Zone zone = new Zone.Builder(Zone.autoZone()) |
|
|
|
private Zone zone = new Zone.Builder(Zone.autoZone()) |
|
|
|
.upHttp("http://upload.qiniup.com") |
|
|
|
.upHttps("http://upload.qiniup.com") |
|
|
|
.upBackupHttp("http://upload.qiniup.com") |
|
|
|
@ -60,18 +51,18 @@ public class QiniuServiceImpl implements IQiniuService { |
|
|
|
.iovipHttp("http://iovip.qbox.me").build(); |
|
|
|
@Override |
|
|
|
public String userUpload(MultipartFile file) { |
|
|
|
String s = updloadFile(file); |
|
|
|
return s; |
|
|
|
return this.updloadFile(file); |
|
|
|
} |
|
|
|
public static String updloadFile(MultipartFile file){ |
|
|
|
public String updloadFile(MultipartFile file){ |
|
|
|
String url = null; |
|
|
|
// 获取文件的名称
|
|
|
|
String fileName = file.getOriginalFilename(); |
|
|
|
//构造一个带指定 Region 对象的配置类
|
|
|
|
Configuration cfg = new Configuration(zone); |
|
|
|
cfg.useHttpsDomains=false; |
|
|
|
UploadManager uploadManager = new UploadManager(cfg); |
|
|
|
Auth auth = Auth.create(accessKey, secretKey); |
|
|
|
String token = auth.uploadToken(bucketName); |
|
|
|
Auth auth = Auth.create(qiniuConfig.getAccessKey(), qiniuConfig.getSecretKey()); |
|
|
|
String token = auth.uploadToken(qiniuConfig.getBucketName()); |
|
|
|
// 使用工具类根据上传文件生成唯一图片名称
|
|
|
|
String imgName = StringUtil.getRandomImgName(fileName); |
|
|
|
if (!file.isEmpty()) { |
|
|
|
@ -80,7 +71,7 @@ public class QiniuServiceImpl implements IQiniuService { |
|
|
|
inputStream=(FileInputStream) file.getInputStream(); |
|
|
|
Response response = uploadManager.put(inputStream, imgName, token,null,null); |
|
|
|
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class); |
|
|
|
url=url+"/"+putRet.key; |
|
|
|
url=qiniuConfig.getUrl()+"/"+putRet.key; |
|
|
|
//System.out.println(putRet.hash);
|
|
|
|
inputStream.close(); |
|
|
|
} catch (QiniuException ex) { |
|
|
|
@ -105,16 +96,15 @@ public class QiniuServiceImpl implements IQiniuService { |
|
|
|
* @param imageUrl 图片的url地址或者图片Base64编码(Base64编码请求时应在开头加上data:application/octet-stream;base64,) |
|
|
|
* @return JSONObject |
|
|
|
*/ |
|
|
|
public static JSONObject checkImageContent(String imageUrl) { |
|
|
|
|
|
|
|
public JSONObject checkImageContent(String imageUrl) { |
|
|
|
//基础参数拼接
|
|
|
|
String url = "http://ai.qiniuapi.com/v3/image/censor"; |
|
|
|
String host = "ai.qiniuapi.com"; |
|
|
|
String url = qiniuConfig.getQiniuCheckImgUrl(); |
|
|
|
String host = qiniuConfig.getQiniuHostUrl(); |
|
|
|
String body = "{ \"data\": { \"uri\": \""+imageUrl+"\" }, \"params\": { \"scenes\": [ \"pulp\", \"terror\", \"politician\" ] } }"; |
|
|
|
String contentType = "application/json"; |
|
|
|
String method = "POST"; |
|
|
|
String accessKey="p53_UbfvTkc_EsRZU-neo7FN6dK1TpEhH9xpHf1q"; |
|
|
|
String secretKey="1W3byAEOKcsz7fKdE_ndCDJy6u_IGojH4A_fwuQx"; |
|
|
|
String accessKey= qiniuConfig.getAccessKey(); |
|
|
|
String secretKey= qiniuConfig.getSecretKey(); |
|
|
|
Auth auth = Auth.create(accessKey, secretKey); |
|
|
|
String qiniuToken = "Qiniu " + auth.signRequestV2(url, method, body.getBytes(), contentType); |
|
|
|
//log.info("url={},body={},qiniuToken={}",url,body,qiniuToken);
|
|
|
|
@ -239,10 +229,11 @@ public class QiniuServiceImpl implements IQiniuService { |
|
|
|
} |
|
|
|
return ""; |
|
|
|
} |
|
|
|
public static void main(String[] args) { |
|
|
|
String imageString = getImageString("C:\\Users\\ASUS\\Documents\\WeChat Files\\wxid_vzk0z5ghy6q922\\FileStorage\\File\\2020-09\\20220608134731.png"); |
|
|
|
JSONObject jsonObject = checkImageContent("data:application/octet-stream;base64," + imageString); |
|
|
|
System.out.println(JSON.parseObject(jsonObject.get("result").toString()).get("suggestion").toString()); |
|
|
|
System.out.println(jsonObject); |
|
|
|
} |
|
|
|
// public static void main(String[] args) {
|
|
|
|
//
|
|
|
|
// String imageString = getImageString("C:\\Users\\ASUS\\Documents\\WeChat Files\\wxid_vzk0z5ghy6q922\\FileStorage\\File\\2020-09\\20220608134731.png");
|
|
|
|
// JSONObject jsonObject = checkImageContent("data:application/octet-stream;base64," + imageString);
|
|
|
|
// System.out.println(JSON.parseObject(jsonObject.get("result").toString()).get("suggestion").toString());
|
|
|
|
// System.out.println(jsonObject);
|
|
|
|
// }
|
|
|
|
} |
|
|
|
|