mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
88 lines
3.1 KiB
Java
88 lines
3.1 KiB
Java
package cn.hellohao.service.impl;
|
|
|
|
import cn.hellohao.pojo.Keys;
|
|
import cn.hellohao.pojo.Msg;
|
|
import cn.hellohao.pojo.ReturnImage;
|
|
import cn.hellohao.pojo.UploadConfig;
|
|
import cn.hellohao.utils.*;
|
|
import com.UpYun;
|
|
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 UFileImageupload {
|
|
static UpYun uFile;
|
|
static Keys key;
|
|
|
|
public ReturnImage ImageuploadUFile(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();
|
|
file = entry.getValue();
|
|
Msg fileMiME = TypeDict.FileMiME(file);
|
|
meta.setHeader("content-type", fileMiME.getData().toString());
|
|
uFile.setContentMD5(UpYun.md5(file));
|
|
boolean result = uFile.writeFile(username + "/" + ShortUID + "." + entry.getKey(), file, true);
|
|
if(result){
|
|
returnImage.setImgname(username + "/" + ShortUID + "." + entry.getKey());
|
|
returnImage.setImgurl(key.getRequestAddress() + "/" +username + "/" + ShortUID + "." + entry.getKey());
|
|
returnImage.setImgSize(entry.getValue().length());
|
|
returnImage.setCode("200");
|
|
}else{
|
|
returnImage.setCode("400");
|
|
System.err.println("上传失败");
|
|
}
|
|
}
|
|
}catch(Exception e){
|
|
returnImage.setCode("500");
|
|
}
|
|
return returnImage;
|
|
|
|
|
|
}
|
|
|
|
//ufile初始化
|
|
public static Integer Initialize(Keys k) {
|
|
int ret = -1;
|
|
if(k.getAccessSecret()!=null && k.getAccessKey()!=null
|
|
&& k.getBucketname()!=null && k.getRequestAddress()!=null ) {
|
|
if(!k.getAccessSecret().equals("") && !k.getAccessKey().equals("")
|
|
&& !k.getBucketname().equals("") && !k.getRequestAddress().equals("") ) {
|
|
// 创建UpYun实例。
|
|
UpYun ufObj = new UpYun(k.getBucketname(), k.getAccessKey(), k.getAccessSecret());
|
|
List<UpYun.FolderItem> items = null;
|
|
try {
|
|
items = ufObj.readDir("/",null);
|
|
ret = 1;
|
|
uFile = ufObj;
|
|
key = k;
|
|
} catch (Exception e) {
|
|
System.out.println("UFile Object Is null");
|
|
ret = -1;
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
public Boolean delUFile(Integer keyID, String fileName) {
|
|
boolean b = true;
|
|
try {
|
|
boolean result = uFile.deleteFile(fileName, null);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
b = false;
|
|
}
|
|
return b;
|
|
}
|
|
|
|
}
|