2019-12-13更新

This commit is contained in:
Hellohao
2019-12-15 17:55:12 +08:00
parent cb4a2839c2
commit ee52957d31
71 changed files with 547 additions and 1986 deletions
@@ -3,10 +3,7 @@ package cn.hellohao.service.impl;
import cn.hellohao.pojo.Keys;
import cn.hellohao.pojo.ReturnImage;
import cn.hellohao.pojo.UploadConfig;
import cn.hellohao.utils.DateUtils;
import cn.hellohao.utils.DeleImg;
import cn.hellohao.utils.ImgUrlUtil;
import cn.hellohao.utils.Print;
import cn.hellohao.utils.*;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import org.springframework.stereotype.Service;
@@ -29,15 +26,13 @@ public class OSSImageupload {
if(fileMap2==null){
File file = null;
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
//设置Header
ObjectMetadata meta = new ObjectMetadata();
meta.setHeader("Content-Disposition", "inline");
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
String times = format1.format(new Date());
file = changeFile(entry.getValue());
file = SetFiles.changeFile(entry.getValue());
String head = "";
if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
head = "image/jpeg";
@@ -51,10 +46,8 @@ public class OSSImageupload {
System.err.println("位置格式文件,无法定义header头。");
}
meta.setHeader("Content-Type", head);//image/jpeg
// 上传文件流。
System.out.println("待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey());
ossClient.putObject(key.getBucketname(), username + "/" + uuid+times + "." + entry.getKey(),file,meta);
ReturnImage returnImage = new ReturnImage();
returnImage.setImgname(entry.getValue().getOriginalFilename());
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
@@ -64,11 +57,9 @@ public class OSSImageupload {
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "2");
}
}
//ossClient.shutdown();
return ImgUrl;
}else{
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
//设置Header
ObjectMetadata meta = new ObjectMetadata();
meta.setHeader("Content-Disposition", "inline");
for (Map.Entry<String, String> entry : fileMap2.entrySet()) {
@@ -76,7 +67,6 @@ public class OSSImageupload {
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
String times = format1.format(new Date());
String imgurl = entry.getValue();
String head = "";
if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
head = "image/jpeg";
@@ -90,10 +80,8 @@ public class OSSImageupload {
System.err.println("位置格式文件,无法定义header头。");
}
meta.setHeader("Content-Type", head);//image/jpeg
// 上传文件流。
System.out.println("待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey());
ossClient.putObject(key.getBucketname(), username + "/" + uuid+times + "." + entry.getKey(),new File(imgurl),meta);
ReturnImage returnImage = new ReturnImage();
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
ImgUrl.put(returnImage, ImgUrlUtil.getFileSize2(new File(imgurl)));
@@ -103,24 +91,8 @@ public class OSSImageupload {
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "2");
}
}
//ossClient.shutdown();
return ImgUrl;
}
}
// 转换文件方法
private File changeFile(MultipartFile multipartFile) throws Exception {
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String prefix = fileName.substring(fileName.lastIndexOf("."));
// todo 修改临时文件文件名
File file = File.createTempFile(fileName, prefix);
// MultipartFile to File
multipartFile.transferTo(file);
return file;
}
//初始化
@@ -143,18 +115,15 @@ public class OSSImageupload {
* 客户端接口
* */
public Map<ReturnImage, Integer> clientuploadOSS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception {
File file = null;
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
//设置Header
ObjectMetadata meta = new ObjectMetadata();
meta.setHeader("Content-Disposition", "inline");
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase().substring(0,5);//生成一个没有-的uuid,然后取前5位
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
String times = format1.format(new Date());
file = changeFile(entry.getValue());
file = SetFiles.changeFile(entry.getValue());
String head = "";
if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
head = "image/jpeg";
@@ -170,14 +139,10 @@ public class OSSImageupload {
meta.setHeader("Content-Type", head);//image/jpeg
ReturnImage returnImage = new ReturnImage();
if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
// 上传文件流。
System.out.println("待上传的图片:"+username + "/" + uuid+times + "." + entry.getKey());
ossClient.putObject(key.getBucketname(), username + "/" + uuid+times + "." + entry.getKey(),file,meta);
returnImage.setImgname(entry.getValue().getOriginalFilename());
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
//ImgUrl.put(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize()/1024));
}else{
returnImage.setImgname(entry.getValue().getOriginalFilename());
returnImage.setImgurl("文件超出系统设定大小,不得超过");
@@ -188,6 +153,4 @@ public class OSSImageupload {
}
}