8 changed files with 465 additions and 14 deletions
@ -0,0 +1,9 @@ |
|||||
|
package com.bnyer.file.service; |
||||
|
|
||||
|
import org.springframework.web.multipart.MultipartFile; |
||||
|
|
||||
|
import java.io.File; |
||||
|
|
||||
|
public interface IQiniuService { |
||||
|
String userUpload(MultipartFile file); |
||||
|
} |
||||
@ -0,0 +1,248 @@ |
|||||
|
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.service.IQiniuService; |
||||
|
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; |
||||
|
|
||||
|
/** |
||||
|
* @Author: Yeman |
||||
|
* @Date: 2022-06-08-10:51 |
||||
|
* @Description: |
||||
|
*/ |
||||
|
@Service("qiniuService") |
||||
|
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; |
||||
|
|
||||
|
private String token = null; |
||||
|
private UploadManager uploadManager; |
||||
|
private static Zone zone = new Zone.Builder(Zone.autoZone()) |
||||
|
.upHttp("http://upload.qiniup.com") |
||||
|
.upHttps("http://upload.qiniup.com") |
||||
|
.upBackupHttp("http://upload.qiniup.com") |
||||
|
.upBackupHttps("http://upload.qiniup.com") |
||||
|
.rsHttp("http://rs.qiniu.com") |
||||
|
.rsfHttp("http://rsf.qiniu.com") |
||||
|
.apiHttp("http://api.qiniu.com") |
||||
|
.iovipHttp("http://iovip.qbox.me").build(); |
||||
|
@Override |
||||
|
public String userUpload(MultipartFile file) { |
||||
|
String s = updloadFile(file); |
||||
|
return s; |
||||
|
} |
||||
|
public static String updloadFile(MultipartFile file){ |
||||
|
// 获取文件的名称
|
||||
|
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); |
||||
|
// 使用工具类根据上传文件生成唯一图片名称
|
||||
|
String imgName = StringUtil.getRandomImgName(fileName); |
||||
|
if (!file.isEmpty()) { |
||||
|
FileInputStream inputStream =null; |
||||
|
try { |
||||
|
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; |
||||
|
//System.out.println(putRet.hash);
|
||||
|
inputStream.close(); |
||||
|
} catch (QiniuException ex) { |
||||
|
ex.printStackTrace(); |
||||
|
return "error"; |
||||
|
} catch (JsonSyntaxException e) { |
||||
|
e.printStackTrace(); |
||||
|
return "error"; |
||||
|
}catch (IOException ioe){ |
||||
|
ioe.printStackTrace(); |
||||
|
return "error"; |
||||
|
} |
||||
|
//String path = qiniuService.uploadFile(file)
|
||||
|
// System.out.print("七牛云返回的图片链接:" + path);
|
||||
|
return url; |
||||
|
} |
||||
|
return "error"; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 检查图片内容是否符合规定 |
||||
|
* @param imageUrl 图片的url地址或者图片Base64编码(Base64编码请求时应在开头加上data:application/octet-stream;base64,) |
||||
|
* @return JSONObject |
||||
|
*/ |
||||
|
public static JSONObject checkImageContent(String imageUrl) { |
||||
|
|
||||
|
//基础参数拼接
|
||||
|
String url = "http://ai.qiniuapi.com/v3/image/censor"; |
||||
|
String host = "ai.qiniuapi.com"; |
||||
|
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"; |
||||
|
Auth auth = Auth.create(accessKey, secretKey); |
||||
|
String qiniuToken = "Qiniu " + auth.signRequestV2(url, method, body.getBytes(), contentType); |
||||
|
//log.info("url={},body={},qiniuToken={}",url,body,qiniuToken);
|
||||
|
//头部部分
|
||||
|
StringMap header = new StringMap(); |
||||
|
header.put("Host",host); |
||||
|
header.put("Authorization",qiniuToken); |
||||
|
header.put("Content-Type", contentType); |
||||
|
Configuration c = new Configuration(zone); |
||||
|
Client client = new Client(c); |
||||
|
try { |
||||
|
Response response = client.post(url, body.getBytes(), header, contentType); |
||||
|
//System.out.println(response.bodyString());
|
||||
|
//log.info("response result={}",response.bodyString());
|
||||
|
JSONObject checkResult = JSON.parseObject(response.bodyString()); |
||||
|
String checkMsg = JSON.parseObject(checkResult.get("result").toString()).get("suggestion").toString(); |
||||
|
if (checkMsg.equals("pass")){ |
||||
|
//可以通过
|
||||
|
} |
||||
|
if (checkMsg.equals("review")){ |
||||
|
//人工审核
|
||||
|
} |
||||
|
if (checkMsg.equals("block")){ |
||||
|
//不可通过
|
||||
|
} |
||||
|
return checkResult; |
||||
|
} catch (QiniuException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 检查图片的格式 |
||||
|
* @param multipartFiles |
||||
|
* @return 格式正确的文件 |
||||
|
*/ |
||||
|
public static ArrayList<MultipartFile> checkImageFormat(ArrayList<MultipartFile> multipartFiles){ |
||||
|
ArrayList<MultipartFile> afterCheckFiles = new ArrayList<>(); |
||||
|
for (MultipartFile multipartFile : multipartFiles) { |
||||
|
ArrayList<String> imageSuffixList = new ArrayList<>(); |
||||
|
String suffix = getSuffix(multipartFile); |
||||
|
//图片后缀
|
||||
|
imageSuffixList.add("jpg"); |
||||
|
imageSuffixList.add("png"); |
||||
|
imageSuffixList.add("avi"); |
||||
|
imageSuffixList.add("flv"); |
||||
|
imageSuffixList.add("mpg"); |
||||
|
imageSuffixList.add("mpeg"); |
||||
|
imageSuffixList.add("mpe"); |
||||
|
imageSuffixList.add("m1v"); |
||||
|
imageSuffixList.add("m2v"); |
||||
|
imageSuffixList.add("mpv2"); |
||||
|
imageSuffixList.add("mp2v"); |
||||
|
imageSuffixList.add("dat"); |
||||
|
imageSuffixList.add("ts"); |
||||
|
imageSuffixList.add("tp"); |
||||
|
imageSuffixList.add("tpr"); |
||||
|
imageSuffixList.add("pva"); |
||||
|
imageSuffixList.add("pss"); |
||||
|
imageSuffixList.add("mp4"); |
||||
|
imageSuffixList.add("m4v"); |
||||
|
imageSuffixList.add("m4p"); |
||||
|
imageSuffixList.add("m4b"); |
||||
|
imageSuffixList.add("3gp"); |
||||
|
imageSuffixList.add("3gpp"); |
||||
|
imageSuffixList.add("3g2"); |
||||
|
imageSuffixList.add("3gp2"); |
||||
|
imageSuffixList.add("ogg"); |
||||
|
imageSuffixList.add("mov"); |
||||
|
imageSuffixList.add("qt"); |
||||
|
imageSuffixList.add("amr"); |
||||
|
imageSuffixList.add("rm"); |
||||
|
imageSuffixList.add("ram"); |
||||
|
imageSuffixList.add("rmvb"); |
||||
|
imageSuffixList.add("rpm"); |
||||
|
//判断视频的后缀 MP4、MOV、WMV
|
||||
|
ArrayList<String> videoSuffixList = new ArrayList<>(); |
||||
|
videoSuffixList.add("mp4"); |
||||
|
videoSuffixList.add("mov"); |
||||
|
videoSuffixList.add("wmv"); |
||||
|
// imageSuffix.add("jpg");
|
||||
|
// imageSuffix.add("jpg");
|
||||
|
if (suffix != null&&imageSuffixList.contains(suffix)&&multipartFile.getSize() / 1024 < 50000&&multipartFile.getSize() / 1024 > 0) { |
||||
|
afterCheckFiles.add(multipartFile); |
||||
|
} |
||||
|
} |
||||
|
return afterCheckFiles; |
||||
|
} |
||||
|
|
||||
|
public static String getSuffix(MultipartFile file) { |
||||
|
String originalFilename = file.getOriginalFilename(); |
||||
|
//System.out.println(originalFilename);
|
||||
|
int dot = originalFilename.lastIndexOf('.'); |
||||
|
int fileNameLength = originalFilename.length(); |
||||
|
if ((dot > -1) && (dot < (originalFilename.length()))) { |
||||
|
String suffix = originalFilename.substring(dot + 1, fileNameLength); |
||||
|
return suffix.toLowerCase(); |
||||
|
} |
||||
|
return null; |
||||
|
} |
||||
|
public static String getImageString(String imageFile){ |
||||
|
InputStream is = null; |
||||
|
try { |
||||
|
byte[] data = null; |
||||
|
is = new FileInputStream(new File(imageFile)); |
||||
|
data = new byte[is.available()]; |
||||
|
is.read(data); |
||||
|
return new String(Base64.encodeBase64(data)); |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} finally { |
||||
|
if (null != is) { |
||||
|
try { |
||||
|
is.close(); |
||||
|
is = null; |
||||
|
} catch (Exception e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,162 @@ |
|||||
|
package com.bnyer.file.utils; |
||||
|
|
||||
|
import cn.hutool.core.date.DateUtil; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.util.Map; |
||||
|
import java.util.UUID; |
||||
|
import java.util.regex.Matcher; |
||||
|
import java.util.regex.Pattern; |
||||
|
|
||||
|
/** |
||||
|
* @Author: Yeman |
||||
|
* @Date: 2022-06-08-11:08 |
||||
|
* @Description: |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
public class StringUtil { |
||||
|
/** |
||||
|
* 数值类型前面补零(共13位) |
||||
|
* @param num |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String supplementZeroGenerateThirteen(int num){ |
||||
|
String str = String.format("%013d", num); |
||||
|
|
||||
|
return str; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 数值类型前面补零(共16位) |
||||
|
* @param num |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String supplementZeroGenerateSixteen(int num){ |
||||
|
String str = String.format("%016d", num); |
||||
|
|
||||
|
return str; |
||||
|
} |
||||
|
/** |
||||
|
* 数值类型前面补零(共3位) |
||||
|
* @param num |
||||
|
* @return |
||||
|
*/ |
||||
|
public static String supplementZeroGenerateThree(int num){ |
||||
|
String str = String.format("%03d", num); |
||||
|
|
||||
|
return str; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 判断字符串是不是double型 |
||||
|
* @param str |
||||
|
* @return |
||||
|
*/ |
||||
|
public static boolean isNumeric(String str){ |
||||
|
Pattern pattern = Pattern.compile("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}"); |
||||
|
Matcher isNum = pattern.matcher(str); |
||||
|
if( !isNum.matches() ){ |
||||
|
return false; |
||||
|
} |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
public static String trim(String str, boolean nullFlag){ |
||||
|
String tempStr = null; |
||||
|
|
||||
|
if (str != null) |
||||
|
{ |
||||
|
tempStr = str.trim(); |
||||
|
} |
||||
|
|
||||
|
if (nullFlag) |
||||
|
{ |
||||
|
if ("".equals(tempStr) || "null".equals(tempStr)) |
||||
|
{ |
||||
|
tempStr = null; |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
if (tempStr == null) |
||||
|
{ |
||||
|
tempStr = ""; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return tempStr; |
||||
|
} |
||||
|
public static String replace(String strSource, String strFrom, String strTo) { |
||||
|
if(strSource==null){ |
||||
|
return null; |
||||
|
} |
||||
|
int i = 0; |
||||
|
if ((i = strSource.indexOf(strFrom, i)) >= 0) { |
||||
|
char[] cSrc = strSource.toCharArray(); |
||||
|
char[] cTo = strTo.toCharArray(); |
||||
|
int len = strFrom.length(); |
||||
|
StringBuffer buf = new StringBuffer(cSrc.length); |
||||
|
buf.append(cSrc, 0, i).append(cTo); |
||||
|
i += len; |
||||
|
int j = i; |
||||
|
while ((i = strSource.indexOf(strFrom, i)) > 0) { |
||||
|
buf.append(cSrc, j, i - j).append(cTo); |
||||
|
i += len; |
||||
|
j = i; |
||||
|
} |
||||
|
buf.append(cSrc, j, cSrc.length - j); |
||||
|
return buf.toString(); |
||||
|
} |
||||
|
return strSource; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public static String deal(String str) { |
||||
|
str = replace(str, "\\", "\\\\"); |
||||
|
str = replace(str, "'", "\\'"); |
||||
|
str = replace(str, "\r", "\\r"); |
||||
|
str = replace(str, "\n", "\\n"); |
||||
|
str = replace(str, "\"", "\\\""); |
||||
|
return str; |
||||
|
} |
||||
|
|
||||
|
public static String GetMapToXML(Map<String,String> param){ |
||||
|
StringBuffer sb = new StringBuffer(); |
||||
|
sb.append("<xml>"); |
||||
|
for (Map.Entry<String,String> entry : param.entrySet()) { |
||||
|
sb.append("<"+ entry.getKey() +">"); |
||||
|
sb.append(entry.getValue()); |
||||
|
sb.append("</"+ entry.getKey() +">"); |
||||
|
} |
||||
|
sb.append("</xml>"); |
||||
|
return sb.toString(); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args){ |
||||
|
//String a = StringUtil.supplementZeroGenerateThirteen(1000);
|
||||
|
double a = 32.; |
||||
|
System.out.println(StringUtil.isNumeric("32.")); |
||||
|
System.out.println(a); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* @Description: 生成唯一图片名称 |
||||
|
* @Param: fileName |
||||
|
* @return: 云服务器fileName |
||||
|
*/ |
||||
|
public static String getRandomImgName(String fileName) { |
||||
|
|
||||
|
int index = fileName.lastIndexOf("."); |
||||
|
|
||||
|
if ((fileName == null || fileName.isEmpty()) || index == -1){ |
||||
|
throw new IllegalArgumentException(); |
||||
|
} |
||||
|
// 获取文件后缀
|
||||
|
String suffix = fileName.substring(index); |
||||
|
// 生成UUID
|
||||
|
String uuid = UUID.randomUUID().toString().replaceAll("-", ""); |
||||
|
// 生成上传至云服务器的路径
|
||||
|
String path = "code/duck/" + DateUtil.today() + "-" + uuid + suffix; |
||||
|
return path; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue