5 changed files with 162 additions and 0 deletions
@ -0,0 +1,12 @@ |
|||||
|
package com.bnyer.file.service; |
||||
|
|
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
/** |
||||
|
* @Author: qyh |
||||
|
* @Date: 2022-06-08-18:44 |
||||
|
* @Description: |
||||
|
*/ |
||||
|
public interface ITikTokImage { |
||||
|
Boolean checkImageContent(MultipartFile file); |
||||
|
} |
||||
@ -0,0 +1,68 @@ |
|||||
|
package com.bnyer.file.service.impl; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.alibaba.nacos.common.codec.Base64; |
||||
|
import com.bnyer.file.service.IQiniuService; |
||||
|
import com.bnyer.file.service.ITikTokImage; |
||||
|
import com.bnyer.file.utils.HttpUtils; |
||||
|
import com.bnyer.file.utils.StringUtil; |
||||
|
import com.google.gson.Gson; |
||||
|
import com.google.gson.JsonSyntaxException; |
||||
|
import com.qiniu.common.QiniuException; |
||||
|
import com.qiniu.common.Zone; |
||||
|
import com.qiniu.http.Client; |
||||
|
import com.qiniu.http.Response; |
||||
|
import com.qiniu.storage.Configuration; |
||||
|
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.stereotype.Service; |
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.io.File; |
||||
|
import java.io.FileInputStream; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStream; |
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashMap; |
||||
|
|
||||
|
/** |
||||
|
* @Author: Yeman |
||||
|
* @Date: 2022-06-08-10:51 |
||||
|
* @Description: |
||||
|
*/ |
||||
|
@Service("tiktokImage") |
||||
|
public class TikTokImageServiceImpl implements ITikTokImage { |
||||
|
@Value("${tiktok.appId}") |
||||
|
private static String appId; |
||||
|
|
||||
|
@Value("${tiktok.secret}") |
||||
|
private static String secret; |
||||
|
|
||||
|
@Value("${tiktok.grant_type}") |
||||
|
private static String grant_type; |
||||
|
|
||||
|
@Value("${qiniu.url}") |
||||
|
private String getTokenUrl="https://developer.toutiao.com/api/apps/v2/token"; |
||||
|
public static void main(String[] args) { |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Boolean checkImageContent(MultipartFile file) { |
||||
|
String token=""; |
||||
|
HashMap<String, Object> param = new HashMap<>(); |
||||
|
param.put("appId", appId); |
||||
|
param.put("secret", secret); |
||||
|
param.put("grant_type", grant_type); |
||||
|
String body = JSON.toJSONString(param); |
||||
|
String res = HttpUtils.sendPost(getTokenUrl, body); |
||||
|
String err_tips = JSON.parseObject(res).get("err_tips").toString(); |
||||
|
if (err_tips.equals("success")){ |
||||
|
token=JSON.parseObject(err_tips).get("access_token").toString(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,77 @@ |
|||||
|
package com.bnyer.file.utils; |
||||
|
|
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.io.BufferedReader; |
||||
|
import java.io.IOException; |
||||
|
import java.io.InputStreamReader; |
||||
|
import java.io.PrintWriter; |
||||
|
import java.net.URL; |
||||
|
import java.net.URLConnection; |
||||
|
|
||||
|
/** |
||||
|
* @Author: qyh |
||||
|
* @Date: 2022-06-08-18:52 |
||||
|
* @Description: |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class HttpUtils { |
||||
|
/** |
||||
|
* 向指定 URL 发送POST方法的请求 |
||||
|
* |
||||
|
* @param url 发送请求的 URL |
||||
|
* @param param 请求参数 |
||||
|
* @return 所代表远程资源的响应结果 |
||||
|
*/ |
||||
|
public static String sendPost(String url, String param) { |
||||
|
PrintWriter out = null; |
||||
|
BufferedReader in = null; |
||||
|
String result = ""; |
||||
|
try { |
||||
|
URL realUrl = new URL(url); |
||||
|
|
||||
|
log.info("post"+url+param); |
||||
|
// 打开和URL之间的连接
|
||||
|
URLConnection conn = realUrl.openConnection(); |
||||
|
// 设置通用的请求属性
|
||||
|
conn.setRequestProperty("content-type", "application/json"); |
||||
|
// 发送POST请求必须设置如下两行
|
||||
|
|
||||
|
conn.setDoOutput(true); |
||||
|
conn.setDoInput(true); |
||||
|
conn.setConnectTimeout(5000); |
||||
|
conn.setReadTimeout(5000); |
||||
|
// 获取URLConnection对象对应的输出流
|
||||
|
out = new PrintWriter(conn.getOutputStream()); |
||||
|
// 发送请求参数
|
||||
|
out.print(param); |
||||
|
// flush输出流的缓冲
|
||||
|
out.flush(); |
||||
|
// 定义BufferedReader输入流来读取URL的响应
|
||||
|
in = new BufferedReader( |
||||
|
new InputStreamReader(conn.getInputStream())); |
||||
|
String line; |
||||
|
while ((line = in.readLine()) != null) { |
||||
|
result += line; |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
log.info("发送 POST 请求出现异常!" + e); |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
//使用finally块来关闭输出流、输入流
|
||||
|
finally { |
||||
|
try { |
||||
|
if (out != null) { |
||||
|
out.close(); |
||||
|
} |
||||
|
if (in != null) { |
||||
|
in.close(); |
||||
|
} |
||||
|
} catch (IOException ex) { |
||||
|
ex.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
log.info(result); |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue