mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
Hellohao图床6-12更新
This commit is contained in:
@@ -25,7 +25,7 @@ import cn.hellohao.pojo.Keys;
|
||||
|
||||
@Service
|
||||
public class NOSImageupload {
|
||||
//直接读取properties文件的值
|
||||
//直接读取properties文件的值
|
||||
// @Value("${AccessKey}")
|
||||
// private String AccessKey;
|
||||
// @Value("${AccessSecret}")
|
||||
@@ -36,60 +36,68 @@ public class NOSImageupload {
|
||||
// private String Bucketname;
|
||||
// @Value("${RequestAddress}")
|
||||
// private String RequestAddress;
|
||||
static String BarrelName;
|
||||
static NosClient nosClient;
|
||||
static Keys key;
|
||||
static String BarrelName;
|
||||
static NosClient nosClient;
|
||||
static Keys key;
|
||||
|
||||
public Map<String, Integer> Imageupload(Map<String, MultipartFile> fileMap) throws Exception {
|
||||
// 要上传文件的路径
|
||||
Map<String, Integer> ImgUrl = new HashMap<>();
|
||||
for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
|
||||
String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
|
||||
//System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
|
||||
File file = changeFile(entry.getValue());
|
||||
try {
|
||||
nosClient.putObject(BarrelName, uuid + "." + entry.getKey(), file);
|
||||
ImgUrl.put(key.getRequestAddress() + "/" + uuid + "." + entry.getKey(), (int) (entry.getValue().getSize()));
|
||||
} catch (Exception e) {
|
||||
System.out.println("上传报错==" + e.getMessage());
|
||||
}
|
||||
}
|
||||
return ImgUrl;
|
||||
}
|
||||
public Map<String, Integer> Imageupload(Map<String, MultipartFile> fileMap, String username) throws Exception {
|
||||
// String Catalog = "";//默认游客目录
|
||||
// if(username.equals("tourist")){
|
||||
// Catalog = "tourist";
|
||||
// }else{
|
||||
// Catalog = username;
|
||||
// }
|
||||
// 要上传文件的路径
|
||||
File file = null;
|
||||
Map<String, 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()));
|
||||
} catch (Exception e) {
|
||||
System.out.println("上传报错:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
// 转换文件方法
|
||||
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;
|
||||
}
|
||||
|
||||
//初始化对象存储
|
||||
public static void Initialize(Keys k){
|
||||
// 初始化
|
||||
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;
|
||||
}
|
||||
//初始化网易NOS对象存储
|
||||
public static void Initialize(Keys k) {
|
||||
// 初始化
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user