mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
重构版功能实现
This commit is contained in:
@@ -2,125 +2,66 @@ package cn.hellohao.service.impl;
|
||||
|
||||
import cn.hellohao.pojo.Keys;
|
||||
import cn.hellohao.pojo.ReturnImage;
|
||||
import cn.hellohao.pojo.UploadConfig;
|
||||
import cn.hellohao.utils.*;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClient;
|
||||
import com.aliyun.oss.model.OSSObjectSummary;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.ObjectListing;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
public class OSSImageupload {
|
||||
static String BarrelName;
|
||||
static OSSClient ossClient;
|
||||
static Keys key;
|
||||
|
||||
public Map<ReturnImage, Integer> ImageuploadOSS(Map<String, MultipartFile> fileMap, String username,
|
||||
Map<String, String> fileMap2,Integer setday){
|
||||
if(fileMap2==null){
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
ObjectMetadata meta = new ObjectMetadata();
|
||||
meta.setHeader("Content-Disposition", "inline");
|
||||
try {
|
||||
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 = SetFiles.changeFile(entry.getValue());
|
||||
String head = "";
|
||||
if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
|
||||
head = "image/jpeg";
|
||||
}else if(entry.getKey().equals("png")){
|
||||
head = "image/png";
|
||||
}else if(entry.getKey().equals("bmp")){
|
||||
head = "image/bmp";
|
||||
}else if(entry.getKey().equals("gif")){
|
||||
head = "image/gif";
|
||||
}else{
|
||||
//System.err.println("位置格式文件,无法定义header头。");
|
||||
head = "image/"+entry.getKey();
|
||||
}
|
||||
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(username + "/" + uuid+times + "." + entry.getKey());//entry.getValue().getOriginalFilename()
|
||||
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
|
||||
if(setday>0) {
|
||||
String deleimg = DateUtils.plusDay(setday);
|
||||
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "2");
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
ImgUrl.put(null, 500);
|
||||
public ReturnImage ImageuploadOSS(Map<String, File> fileMap, String username,Integer keyID){
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
File file = null;
|
||||
ObjectMetadata meta = new ObjectMetadata();
|
||||
meta.setHeader("Content-Disposition", "inline");
|
||||
try {
|
||||
for (Map.Entry<String, File> entry : fileMap.entrySet()) {
|
||||
String ShortUID = SetText.getShortUuid();//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 = entry.getValue();
|
||||
System.out.println("待上传的图片:"+username + "/" + ShortUID + "." + entry.getKey());
|
||||
ossClient.putObject(key.getBucketname(), username + "/" + ShortUID + "." + entry.getKey(),file);
|
||||
returnImage.setUid(ShortUID);
|
||||
returnImage.setImgname(username + "/" + ShortUID + "." + entry.getKey());//entry.getValue().getOriginalFilename()
|
||||
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + ShortUID + "." + entry.getKey());
|
||||
returnImage.setImgSize(file.length());
|
||||
returnImage.setCode("200");
|
||||
}
|
||||
return ImgUrl;
|
||||
}else{
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
ObjectMetadata meta = new ObjectMetadata();
|
||||
meta.setHeader("Content-Disposition", "inline");
|
||||
try {
|
||||
for (Map.Entry<String, String> entry : fileMap2.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());
|
||||
String imgurl = entry.getValue();
|
||||
String head = "";
|
||||
if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
|
||||
head = "image/jpeg";
|
||||
}else if(entry.getKey().equals("png")){
|
||||
head = "image/png";
|
||||
}else if(entry.getKey().equals("bmp")){
|
||||
head = "image/bmp";
|
||||
}else if(entry.getKey().equals("gif")){
|
||||
head = "image/gif";
|
||||
}else{
|
||||
//System.err.println("位置格式文件,无法定义header头。");
|
||||
head = "image/"+entry.getKey();
|
||||
}
|
||||
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)));
|
||||
new File(imgurl).delete();
|
||||
if(setday>0) {
|
||||
String deleimg = DateUtils.plusDay(setday);
|
||||
DeleImg.charu(username + "/" + uuid + times + "." + entry.getKey() + "|" + deleimg + "|" + "2");
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
ImgUrl.put(null, 500);
|
||||
}
|
||||
return ImgUrl;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//初始化
|
||||
public static Integer Initialize(Keys k) {
|
||||
|
||||
int ret = -1;
|
||||
ObjectListing objectListing = null;
|
||||
if(k.getEndpoint()!=null && k.getAccessSecret()!=null && k.getEndpoint()!=null
|
||||
if(k.getEndpoint()!=null && k.getAccessSecret()!=null && k.getAccessKey()!=null && k.getEndpoint()!=null
|
||||
&& k.getBucketname()!=null && k.getRequestAddress()!=null ) {
|
||||
if(!k.getEndpoint().equals("") && !k.getAccessSecret().equals("") && !k.getEndpoint().equals("")
|
||||
if(!k.getEndpoint().equals("") && !k.getAccessSecret().equals("") && !k.getAccessKey().equals("") && !k.getEndpoint().equals("")
|
||||
&& !k.getBucketname().equals("") && !k.getRequestAddress().equals("") ) {
|
||||
ossClient = new OSSClient(k.getEndpoint(), k.getAccessKey(), k.getAccessSecret());
|
||||
OSS ossClient = new OSSClientBuilder().build(k.getEndpoint(), k.getAccessKey(), k.getAccessSecret());
|
||||
try {
|
||||
objectListing = ossClient.listObjects(k.getBucketname());
|
||||
key = k;
|
||||
ret=1;
|
||||
ossClient = ossClient;
|
||||
key = k;
|
||||
} catch (Exception e) {
|
||||
System.out.println("OSS - Waiting for configuration");
|
||||
System.out.println("OSS Object Is null");
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
@@ -128,49 +69,16 @@ public class OSSImageupload {
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 客户端接口
|
||||
* */
|
||||
public Map<ReturnImage, Integer> clientuploadOSS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception {
|
||||
File file = null;
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
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 = SetFiles.changeFile_c(entry.getValue());
|
||||
String head = "";
|
||||
if(entry.getKey().equals("jpg")||entry.getKey().equals("jpeg")){
|
||||
head = "image/jpeg";
|
||||
}else if(entry.getKey().equals("png")){
|
||||
head = "image/png";
|
||||
}else if(entry.getKey().equals("bmp")){
|
||||
head = "image/bmp";
|
||||
}else if(entry.getKey().equals("gif")){
|
||||
head = "image/gif";
|
||||
}else{
|
||||
//System.err.println("位置格式文件,无法定义header头。");
|
||||
head = "image/"+entry.getKey();
|
||||
}
|
||||
meta.setHeader("Content-Type", head);//image/jpeg
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
|
||||
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()));
|
||||
}else{
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl("文件超出系统设定大小,不得超过");
|
||||
ImgUrl.put(returnImage, -1);
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
|
||||
public boolean delOSS(Integer keyID, String fileName){
|
||||
boolean b =true;
|
||||
try {
|
||||
ossClient.deleteObject(key.getBucketname(), fileName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
b = false;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user