mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
2019-08-07更新 增加API接口
This commit is contained in:
@@ -8,7 +8,9 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import cn.hellohao.exception.StorageSourceInitException;
|
||||
import cn.hellohao.pojo.ReturnImage;
|
||||
import cn.hellohao.pojo.UploadConfig;
|
||||
import cn.hellohao.utils.ImgUrlUtil;
|
||||
import cn.hellohao.utils.Print;
|
||||
import com.netease.cloud.services.nos.model.ObjectMetadata;
|
||||
@@ -50,15 +52,12 @@ public class NOSImageupload {
|
||||
java.text.DateFormat format1 = new java.text.SimpleDateFormat("MMddhhmmss");
|
||||
String times = format1.format(new Date());
|
||||
file = changeFile(entry.getValue());
|
||||
try {
|
||||
nosClient.putObject(BarrelName, username + "/" + uuid+times + "." + entry.getKey(), file);
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
|
||||
} catch (Exception e) {
|
||||
System.out.println("上传报错:" + e.getMessage());
|
||||
}
|
||||
nosClient.putObject(BarrelName, username + "/" + uuid+times + "." + entry.getKey(), file);
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
returnImage.setImgname(entry.getValue().getOriginalFilename());
|
||||
returnImage.setImgurl(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey());
|
||||
ImgUrl.put(returnImage, (int) (entry.getValue().getSize()));
|
||||
|
||||
}
|
||||
return ImgUrl;
|
||||
}else{
|
||||
@@ -82,7 +81,7 @@ public class NOSImageupload {
|
||||
}else if(entry.getKey().equals("gif")){
|
||||
head = "image/gif";
|
||||
}else{
|
||||
System.err.println("位置格式文件,无法定义header头。");
|
||||
System.err.println("未知格式文件,无法定义header头。");
|
||||
}
|
||||
meta.setHeader("Content-Type", head);//image/jpeg
|
||||
File file = new File(imgurl);
|
||||
@@ -126,53 +125,71 @@ public class NOSImageupload {
|
||||
|
||||
|
||||
//初始化网易NOS对象存储
|
||||
public static void Initialize(Keys k) {
|
||||
public static Integer Initialize(Keys k) {
|
||||
int ret = -1;
|
||||
if(k.getEndpoint()!=null && k.getAccessSecret()!=null && k.getEndpoint()!=null
|
||||
&& k.getBucketname()!=null && k.getRequestAddress()!=null ){
|
||||
// 初始化
|
||||
Credentials credentials = new BasicCredentials(k.getAccessKey(), k.getAccessSecret());
|
||||
nosClient = new NosClient(credentials);
|
||||
nosClient.setEndpoint(k.getEndpoint());
|
||||
// 初始化TransferManager
|
||||
TransferManager transferManager = new TransferManager(nosClient);
|
||||
// 列举桶
|
||||
ArrayList bucketList = new ArrayList();
|
||||
for (Bucket bucket : nosClient.listBuckets()) {
|
||||
bucketList.add(bucket.getName());
|
||||
}
|
||||
for (Object object : bucketList) {
|
||||
if (object.toString().equals(k.getBucketname())) {
|
||||
BarrelName = object.toString();
|
||||
}
|
||||
}
|
||||
key = k;
|
||||
}
|
||||
if(!k.getEndpoint().equals("") && !k.getAccessSecret().equals("") && !k.getEndpoint().equals("")
|
||||
&& !k.getBucketname().equals("") && !k.getRequestAddress().equals("") ){
|
||||
// 初始化
|
||||
Credentials credentials = new BasicCredentials(k.getAccessKey(), k.getAccessSecret());
|
||||
nosClient = new NosClient(credentials);
|
||||
nosClient.setEndpoint(k.getEndpoint());
|
||||
// 列举桶
|
||||
ArrayList bucketList = new ArrayList();
|
||||
//加入传入的key值不正确,这里会报异常,页面500,如何捕捉异常处理。
|
||||
BarrelName = k.getBucketname();
|
||||
// for (Bucket bucket : nosClient.listBuckets()) {
|
||||
// bucketList.add(bucket.getName());
|
||||
// }
|
||||
|
||||
// for (Object object : bucketList) {
|
||||
// if (object.toString().equals(k.getBucketname())) {
|
||||
// BarrelName = object.toString();
|
||||
// }
|
||||
// }
|
||||
key = k;
|
||||
ret = 1;
|
||||
}else{
|
||||
ret = -1;
|
||||
}
|
||||
}else{
|
||||
ret = -1;
|
||||
//throw new StorageSourceInitException("当前数据源配置不完整,请管理员前往后台配置。");
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户端接口
|
||||
* */
|
||||
public Map<String, Integer> clientuploadNOS(Map<String, MultipartFile> fileMap, String username) throws Exception {
|
||||
|
||||
public Map<ReturnImage, Integer> clientuploadNOS(Map<String, MultipartFile> fileMap, String username, UploadConfig uploadConfig) throws Exception {
|
||||
File file = null;
|
||||
Map<String, Integer> ImgUrl = new HashMap<>();
|
||||
Map<ReturnImage, Integer> ImgUrl = new HashMap<>();
|
||||
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());
|
||||
try {
|
||||
nosClient.putObject(BarrelName, username + "/" + uuid+times + "." + entry.getKey(), file);
|
||||
ImgUrl.put(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize()));
|
||||
|
||||
Print.warning(entry.getValue().getSize());
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
if(entry.getValue().getSize()/1024<=uploadConfig.getFilesizeuser()*1024){
|
||||
nosClient.putObject(BarrelName, username + "/" + uuid+times + "." + entry.getKey(), file);
|
||||
//ImgUrl.put(key.getRequestAddress() + "/" + username + "/" + uuid+times + "." + entry.getKey(), (int) (entry.getValue().getSize()));
|
||||
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);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("上传报错:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user