Browse Source

修改文件上传功能 添加视频上传功能

dev
long 3 years ago
parent
commit
3af29eb0ee
  1. 49
      conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/ConfluxServiceImpl.java
  2. 19
      conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/TencentUploadServiceImpl.java
  3. 2
      conflux-common/src/main/java/com/conflux/common/service/TencentUploadService.java

49
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<String, Object> map = tencentUploadService.uploadVideo(file);
return AjaxResult.success(map);
}
Map<String, Object> 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<bb.length;j++){
int x = bb[j] & 0xFF;
String a = Integer.toHexString(x);
if(a.length()<2){
stringBuilder.append(0);
}
stringBuilder.append(a);
}
if(!stringBuilder.toString().equals("000000")){
String fileName = file.getOriginalFilename();
return false;
//throw new RuntimeException(fileName+"不是正确的mp4文件!");
}
} catch (IOException e) {
e.printStackTrace();
return false;
}finally {
try {
if(in!=null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
/**
* 统一处理nft方法

19
conflux-admin/src/main/java/com/conflux/web/controller/nft/service/impl/TencentUploadServiceImpl.java

@ -49,14 +49,14 @@ public class TencentUploadServiceImpl implements TencentUploadService {
map.put("url" + i, imgUrl);
type = 1;
}
} else if (type.equals(4)){
} else if (type.equals(4)) {
//普通上传
String s = uploadFile2Cos(file, type);
String imgUrl = getNftImgUrl(s);
//String str1=imgUrl.substring(0, imgUrl.indexOf("?"));
// map.put("url0",url );
map.put("url", imgUrl);
}else {
} else {
//普通上传
String s = uploadFile2Cos(file, type);
String imgUrl = getImgUrl(s);
@ -69,6 +69,20 @@ public class TencentUploadServiceImpl implements TencentUploadService {
return map;
}
@Override
public Map<String, Object> 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<String, Object> 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);

2
conflux-common/src/main/java/com/conflux/common/service/TencentUploadService.java

@ -21,6 +21,8 @@ public interface TencentUploadService {
*/
public Map<String, Object> upload(MultipartFile file, Integer type) throws Exception;
public Map<String, Object> uploadVideo(MultipartFile file) throws Exception;
/**
* 根据图片路径上传图片
*

Loading…
Cancel
Save