From 3af29eb0ee88f6427ef87d249afcdd05e436f366 Mon Sep 17 00:00:00 2001 From: long <18782126717@163.com> Date: Mon, 29 Aug 2022 17:13:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E5=8A=9F=E8=83=BD=20=E6=B7=BB=E5=8A=A0=E8=A7=86?= =?UTF-8?q?=E9=A2=91=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nft/service/impl/ConfluxServiceImpl.java | 49 +++++++++++++++++++ .../impl/TencentUploadServiceImpl.java | 19 ++++++- .../common/service/TencentUploadService.java | 2 + 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/ConfluxServiceImpl.java b/conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/ConfluxServiceImpl.java index ac0140d..c1961f8 100644 --- a/conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/ConfluxServiceImpl.java +++ b/conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/ConfluxServiceImpl.java @@ -331,6 +331,12 @@ public class ConfluxServiceImpl implements ConfluxService { return ajaxResult; } try { + //判断文件是否为MP4文件 + boolean mp4Video = isMp4Video(file); + if(mp4Video){ + Map map = tencentUploadService.uploadVideo(file); + return AjaxResult.success(map); + } Map upload = tencentUploadService.upload(file, 4);//类型4为nft图片上传 return AjaxResult.success(upload); } catch (Exception e) { @@ -338,6 +344,49 @@ public class ConfluxServiceImpl implements ConfluxService { return AjaxResult.error("上传失败!"); } } + /** + * 根据文件字节流,判断文件是否为mp4视频文件 + * @param file 上传的文件 + * @throws RuntimeException 如果不是mp4文件则抛出异常 + * */ + private boolean isMp4Video(MultipartFile file) throws RuntimeException{ + if(!file.isEmpty()){ + InputStream in = null; + try { + in = file.getInputStream(); + StringBuilder stringBuilder = new StringBuilder(); + byte [] bb = new byte[3]; + in.read(bb,0,bb.length); + for(int j=0 ; j uploadVideo(MultipartFile file) throws IOException { + String originalFilename = FileUploadUtils.extractFilename(file); + String substring = originalFilename.substring(originalFilename.lastIndexOf(".")).toLowerCase(); + Random random = new Random(); + String name = random.nextInt(10000) + System.currentTimeMillis() + substring; + InputStream inputStream = file.getInputStream(); + client.createClient().putObject(tencentConfig.getPhotoBucket(),name,inputStream,null); + String imgUrl = getImgUrl(name); + Map map=new HashMap<>(); + map.put("url", imgUrl); + return map; + } + @Override public String uploadImg2Cos(String url) throws Exception { File fileOnServer = new File(url); @@ -285,6 +299,7 @@ public class TencentUploadServiceImpl implements TencentUploadService { } return null; } + public String getNftUrl(String key) { // 设置URL过期时间为10年 3600l* 1000*24*365*10 Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 10); diff --git a/conflux-common/src/main/java/com/conflux/common/service/TencentUploadService.java b/conflux-common/src/main/java/com/conflux/common/service/TencentUploadService.java index 114bf13..2c7af87 100644 --- a/conflux-common/src/main/java/com/conflux/common/service/TencentUploadService.java +++ b/conflux-common/src/main/java/com/conflux/common/service/TencentUploadService.java @@ -21,6 +21,8 @@ public interface TencentUploadService { */ public Map upload(MultipartFile file, Integer type) throws Exception; + + public Map uploadVideo(MultipartFile file) throws Exception; /** * 根据图片路径上传图片 *