mirror of
https://github.com/Hello-hao/Tbed.git
synced 2024-04-21 12:32:10 +00:00
update:优化上传逻辑,减少性能占用
This commit is contained in:
@@ -15,13 +15,17 @@ import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.model.PutObjectResult;
|
||||
import com.qcloud.cos.region.Region;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class COSImageupload {
|
||||
private static Logger logger = LoggerFactory.getLogger(COSImageupload.class);
|
||||
static COSClient cosClient;
|
||||
static Keys key;
|
||||
|
||||
@@ -29,16 +33,18 @@ public class COSImageupload {
|
||||
Map<Map<String, String>, File> fileMap, String username, Integer keyID) {
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
File file = null;
|
||||
FileInputStream stream = null;
|
||||
try {
|
||||
for (Map.Entry<Map<String, String>, File> entry : fileMap.entrySet()) {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
stream = new FileInputStream(file);
|
||||
try {
|
||||
String bucketName = key.getBucketname();
|
||||
String userkey = username + "/" + ShortUIDName + "." + prefix;
|
||||
PutObjectRequest putObjectRequest =
|
||||
new PutObjectRequest(bucketName, userkey, file);
|
||||
new PutObjectRequest(bucketName, userkey, stream,null);
|
||||
PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
|
||||
} catch (CosServiceException serverException) {
|
||||
returnImage.setCode("400");
|
||||
@@ -47,10 +53,12 @@ public class COSImageupload {
|
||||
returnImage.setCode("400");
|
||||
clientException.printStackTrace();
|
||||
}
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
}
|
||||
returnImage.setCode("200");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
logger.error("COS协议上传时发生异常:",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -28,7 +28,6 @@ import java.util.*;
|
||||
* @date 2021/10/28 16:38
|
||||
*/
|
||||
@Service
|
||||
@EnableAsync
|
||||
public class ClientService {
|
||||
private static Logger logger = LoggerFactory.getLogger(ClientService.class);
|
||||
@Autowired private ConfdataService confdataService;
|
||||
@@ -41,7 +40,6 @@ public class ClientService {
|
||||
@Autowired private GetSource getSource;
|
||||
@Autowired private ImgViolationJudgeServiceImpl imgViolationJudgeService;
|
||||
|
||||
@Async("taskExecutor")
|
||||
public Msg uploadImg(
|
||||
HttpServletRequest request, MultipartFile multipartFile, String email, String pass,Integer setday) {
|
||||
Msg msg = new Msg();
|
||||
@@ -62,31 +60,70 @@ public class ClientService {
|
||||
}
|
||||
imageFileName = multipartFile.getOriginalFilename();
|
||||
File file = SetFiles.changeFile_new(multipartFile);
|
||||
FileInputStream stream = new FileInputStream(file);
|
||||
User u2 = new User();
|
||||
if (!file.exists() || email == null || pass == null) {
|
||||
msg.setCode("4005");
|
||||
msg.setInfo("必要参数不能为空");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
if (uploadConfig.getBlacklist() != null) {
|
||||
String[] iparr = uploadConfig.getBlacklist().split(";");
|
||||
for (String s : iparr) {
|
||||
if (s.equals(userip)) {
|
||||
msg.setCode("4010");
|
||||
msg.setInfo("你暂时不能上传");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
u2.setEmail(email);
|
||||
u2.setPassword(Base64.getEncoder().encodeToString(pass.getBytes()));
|
||||
User u = userMapper.getUsers(u2);
|
||||
if (null == u || u.getIsok() != 1) {
|
||||
msg.setCode("4006");
|
||||
msg.setInfo("用户信息不正确,账号异常");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
String imguid = UUID.randomUUID().toString().replace("-", "");
|
||||
Msg msg1 = updateImgCheck(u, uploadConfig);
|
||||
if (!msg1.getCode().equals("300")) {
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg1;
|
||||
}
|
||||
JSONObject updateJson = JSONObject.parseObject(msg1.getData().toString());
|
||||
final JSONObject groupJson = updateJson.getJSONObject("group");
|
||||
final Group group = groupJson.toJavaObject(Group.class);
|
||||
Long memory = updateJson.getLong("memory");
|
||||
Long UsedTotleMemory = updateJson.getLong("UsedTotleMemory");
|
||||
Long TotleMemory = updateJson.getLong("TotleMemory");
|
||||
String updatePath = updateJson.getString("updatePath");
|
||||
|
||||
sourceKeyId = group.getKeyid();
|
||||
Keys key = keysMapper.selectKeys(sourceKeyId);
|
||||
Long tmp = (memory == -1 ? -2 : UsedTotleMemory);
|
||||
if (tmp >= memory) {
|
||||
msg.setCode("4007");
|
||||
msg.setInfo(u == null ? "游客空间已用尽" : "您的可用空间不足");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
if (file.length() > TotleMemory) {
|
||||
@@ -94,43 +131,43 @@ public class ClientService {
|
||||
System.err.println("最大限制:" + TotleMemory);
|
||||
msg.setCode("4008");
|
||||
msg.setInfo("图像超出系统限制大小");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
md5key = DigestUtils.md5Hex(fis);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("未获取到图片的MD5,成成UUID");
|
||||
}finally{
|
||||
try {
|
||||
if (null != fis)
|
||||
fis.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
Msg fileMiME = TypeDict.FileMiME(file);
|
||||
md5key = DigestUtils.md5Hex(stream);
|
||||
// FileInputStream fis = null;
|
||||
// try {
|
||||
// fis = new FileInputStream(file);
|
||||
// md5key = DigestUtils.md5Hex(fis);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("未获取到图片的MD5,成成UUID异常:",e);
|
||||
// }finally{
|
||||
// try {
|
||||
// if (null != fis)
|
||||
// fis.close();
|
||||
// } catch (Exception e) {
|
||||
// logger.error("ClientService-110异常",e);
|
||||
// }
|
||||
// }
|
||||
Msg fileMiME = TypeDict.FileMiME(stream);
|
||||
if (!fileMiME.getCode().equals("200")) {
|
||||
msg.setCode("4009");
|
||||
msg.setInfo(fileMiME.getInfo());
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
if (md5key == null || md5key.equals("")) {
|
||||
md5key = UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
String prefix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
|
||||
if (uploadConfig.getBlacklist() != null) {
|
||||
String[] iparr = uploadConfig.getBlacklist().split(";");
|
||||
for (String s : iparr) {
|
||||
if (s.equals(userip)) {
|
||||
msg.setCode("4010");
|
||||
msg.setInfo("你暂时不能上传");
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
|
||||
Images imgObj = new Images();
|
||||
String imgnameEd = null;
|
||||
Map<Map<String, String>, File> map = new HashMap<>();
|
||||
@@ -154,6 +191,7 @@ public class ClientService {
|
||||
}else{
|
||||
msg.setInfo("未获取到指定图像:110503");
|
||||
msg.setCode("110503");
|
||||
file.delete();
|
||||
return msg;
|
||||
}
|
||||
imgObj.setUpdatetime(df.format(new Date()));
|
||||
@@ -177,6 +215,7 @@ public class ClientService {
|
||||
imgObj.setIdname(imageFileName);
|
||||
Msg insertRet = imgServiceImpl.insertImgDataForCheck(imgObj,u,confdata,file.getName());
|
||||
if (insertRet.getCode().equals("000")) {
|
||||
file.delete();
|
||||
return insertRet;
|
||||
}
|
||||
long stime = System.currentTimeMillis();
|
||||
@@ -192,43 +231,48 @@ public class ClientService {
|
||||
imgServiceImpl.deleimgForImgUid(imgObj.getImguid());
|
||||
msg.setCode("5001");
|
||||
msg.setInfo("上传服务内部错误");
|
||||
file.delete();
|
||||
return msg;
|
||||
}
|
||||
file.delete();
|
||||
msg.setData(jsonObject);
|
||||
return msg;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
msg.setCode("5001");
|
||||
msg.setInfo("Error for server:500");
|
||||
logger.error("ClientService:Error for server-202行异常",e);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
public static Group group;
|
||||
public static Long memory;
|
||||
public static Long TotleMemory;
|
||||
public static Long UsedTotleMemory;
|
||||
public static String updatePath = "tourist";
|
||||
// public static Group group;
|
||||
// public static Long memory;
|
||||
// public static Long TotleMemory;
|
||||
// public static Long UsedTotleMemory;
|
||||
// public static String updatePath = "tourist";
|
||||
|
||||
private Msg updateImgCheck(User user, UploadConfig uploadConfig) {
|
||||
Msg msg = new Msg();
|
||||
java.text.DateFormat dateFormat = null;
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
String updatePath = "tourist";
|
||||
try {
|
||||
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||
Group group = GetCurrentSource.GetSource(user == null ? null : user.getId());
|
||||
jsonObject.put("group",group);
|
||||
if (user == null) {
|
||||
if (uploadConfig.getIsupdate() != 1) {
|
||||
msg.setCode("1000");
|
||||
msg.setInfo("系统已禁用游客上传");
|
||||
return msg;
|
||||
}
|
||||
group = GetCurrentSource.GetSource(null);
|
||||
memory = Long.valueOf(uploadConfig.getVisitormemory()); // 单位 B 游客设置总量
|
||||
TotleMemory = Long.valueOf(uploadConfig.getFilesizetourists()); // 单位 B 游客单文件大小
|
||||
UsedTotleMemory =
|
||||
Long memory = Long.valueOf(uploadConfig.getVisitormemory()); // 单位 B 游客设置总量
|
||||
Long TotleMemory = Long.valueOf(uploadConfig.getFilesizetourists()); // 单位 B 游客单文件大小
|
||||
Long UsedTotleMemory =
|
||||
imgServiceImpl.getusermemory(0) == null
|
||||
? 0L
|
||||
: imgServiceImpl.getusermemory(0); // 单位 B
|
||||
jsonObject.put("memory",memory);
|
||||
jsonObject.put("TotleMemory",TotleMemory);
|
||||
jsonObject.put("UsedTotleMemory",UsedTotleMemory);
|
||||
} else {
|
||||
// 判断用户能不能上传
|
||||
if (uploadConfig.getUserclose() != 1) {
|
||||
@@ -237,21 +281,24 @@ public class ClientService {
|
||||
return msg;
|
||||
}
|
||||
updatePath = user.getUsername();
|
||||
group = GetCurrentSource.GetSource(user.getId());
|
||||
memory = Long.valueOf(user.getMemory()); // 单位 B
|
||||
TotleMemory = Long.valueOf(uploadConfig.getFilesizeuser()); // 单位 B
|
||||
UsedTotleMemory =
|
||||
Long memory = Long.valueOf(user.getMemory()); // 单位 B
|
||||
Long TotleMemory = Long.valueOf(uploadConfig.getFilesizeuser()); // 单位 B
|
||||
Long UsedTotleMemory =
|
||||
imgServiceImpl.getusermemory(user.getId()) == null
|
||||
? 0L
|
||||
: imgServiceImpl.getusermemory(user.getId()); // 单位 B
|
||||
jsonObject.put("memory",memory);
|
||||
jsonObject.put("TotleMemory",TotleMemory);
|
||||
jsonObject.put("UsedTotleMemory",UsedTotleMemory);
|
||||
}
|
||||
// 判断上传的图片目录结构类型
|
||||
if (uploadConfig.getUrltype() == 2) {
|
||||
updatePath = dateFormat.format(new Date());
|
||||
jsonObject.put("updatePath",updatePath);
|
||||
msg.setData(jsonObject);
|
||||
}
|
||||
msg.setCode("300");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.error("ClientService-异常",e);
|
||||
msg.setCode("500");
|
||||
}
|
||||
return msg;
|
||||
|
||||
@@ -7,15 +7,19 @@ import cn.hutool.extra.ftp.Ftp;
|
||||
import cn.hutool.extra.ftp.FtpMode;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.net.ftp.FTP;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class FtpServiceImpl {
|
||||
private static Logger logger = LoggerFactory.getLogger(FtpServiceImpl.class);
|
||||
private Keys key;
|
||||
|
||||
public static int Initialize(Keys k) {
|
||||
@@ -66,22 +70,26 @@ public class FtpServiceImpl {
|
||||
return returnImage;
|
||||
}
|
||||
File file = null;
|
||||
FileInputStream stream = null;
|
||||
try {
|
||||
for (Map.Entry<Map<String, String>, File> entry : fileMap.entrySet()) {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
stream = new FileInputStream(file);
|
||||
boolean isUpload = ftp.upload(
|
||||
"/" + username,
|
||||
ShortUIDName + "." + prefix,
|
||||
file);
|
||||
stream);
|
||||
if (!isUpload) {
|
||||
returnImage.setCode("400");
|
||||
}
|
||||
try {if(stream!=null){stream.close();}} catch (Exception e) { }
|
||||
}
|
||||
returnImage.setCode("200");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
logger.error("FTP发生异常:",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -14,13 +14,17 @@ import com.qiniu.storage.model.DefaultPutRet;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import com.qiniu.util.Auth;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class KODOImageupload {
|
||||
private static Logger logger = LoggerFactory.getLogger(KODOImageupload.class);
|
||||
static String upToken;
|
||||
static BucketManager bucketManager;
|
||||
static Keys key;
|
||||
@@ -33,15 +37,15 @@ public class KODOImageupload {
|
||||
Auth auth = Auth.create(key.getAccessKey(), key.getAccessSecret());
|
||||
String upToken = auth.uploadToken(key.getBucketname(), null, 7200, null);
|
||||
File file = null;
|
||||
FileInputStream stream = null;
|
||||
try {
|
||||
for (Map.Entry<Map<String, String>, File> entry : fileMap.entrySet()) {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
stream = new FileInputStream(file);
|
||||
try {
|
||||
Response response =
|
||||
uploadManager.put(
|
||||
file, username + "/" + ShortUIDName + "." + prefix, upToken);
|
||||
Response response = uploadManager.put(stream,username + "/" + ShortUIDName + "." + prefix, upToken, null, null);
|
||||
DefaultPutRet putRet =
|
||||
new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
|
||||
|
||||
@@ -54,10 +58,12 @@ public class KODOImageupload {
|
||||
} catch (QiniuException ex2) {
|
||||
}
|
||||
}
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
}
|
||||
returnImage.setCode("200");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
logger.error("KODO发生异常:",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -9,6 +9,8 @@ import com.netease.cloud.auth.Credentials;
|
||||
import com.netease.cloud.services.nos.NosClient;
|
||||
import com.netease.cloud.services.nos.model.ObjectListing;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@@ -11,13 +11,17 @@ import com.aliyun.oss.model.ObjectListing;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class OSSImageupload {
|
||||
private static Logger logger = LoggerFactory.getLogger(OSSImageupload.class);
|
||||
static OSS ossClient;
|
||||
static Keys key;
|
||||
|
||||
@@ -25,6 +29,7 @@ public class OSSImageupload {
|
||||
Map<Map<String, String>, File> fileMap, String username, Integer keyID) {
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
File file = null;
|
||||
FileInputStream stream = null;
|
||||
ObjectMetadata meta = new ObjectMetadata();
|
||||
meta.setHeader("Content-Disposition", "inline");
|
||||
try {
|
||||
@@ -32,19 +37,22 @@ public class OSSImageupload {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
Msg fileMiME = TypeDict.FileMiME(file);
|
||||
stream = new FileInputStream(file);
|
||||
Msg fileMiME = TypeDict.FileMiME(stream);
|
||||
meta.setHeader("content-type", fileMiME.getData().toString());
|
||||
PutObjectRequest putObjectRequest =
|
||||
new PutObjectRequest(
|
||||
key.getBucketname(),
|
||||
username + "/" + ShortUIDName + "." + prefix,
|
||||
file,
|
||||
stream,
|
||||
meta);
|
||||
ossClient.putObject(putObjectRequest);
|
||||
try {if(stream!=null){stream.close();}} catch (Exception e) { }
|
||||
}
|
||||
returnImage.setCode("200");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
logger.error("OSS上传发生异常:",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -51,21 +52,25 @@ public class S3Imageupload {
|
||||
return returnImage;
|
||||
}
|
||||
File file = null;
|
||||
FileInputStream stream = null;
|
||||
try {
|
||||
for (Map.Entry<Map<String, String>, File> entry : fileMap.entrySet()) {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
stream = new FileInputStream(file);
|
||||
AS3.putObject(
|
||||
new PutObjectRequest(
|
||||
KEY.getBucketname(),
|
||||
username + "/" + ShortUIDName + "." + prefix,
|
||||
file));
|
||||
stream,null));
|
||||
// S3Object object = s3.getObject( new GetObjectRequest( key.getBucketname(),
|
||||
try {if(stream!=null){stream.close();}} catch (Exception e) { }
|
||||
}
|
||||
returnImage.setCode("200");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
logger.error("S3协议上传发生异常:",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -8,14 +8,18 @@ import cn.hellohao.utils.TypeDict;
|
||||
import com.UpYun;
|
||||
import com.aliyun.oss.model.ObjectMetadata;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class USSImageupload {
|
||||
private static Logger logger = LoggerFactory.getLogger(USSImageupload.class);
|
||||
static UpYun upyun;
|
||||
static Keys key;
|
||||
|
||||
@@ -23,6 +27,7 @@ public class USSImageupload {
|
||||
Map<Map<String, String>, File> fileMap, String username, Integer keyID) {
|
||||
ReturnImage returnImage = new ReturnImage();
|
||||
File file = null;
|
||||
FileInputStream stream = null;
|
||||
ObjectMetadata meta = new ObjectMetadata();
|
||||
meta.setHeader("Content-Disposition", "inline");
|
||||
try {
|
||||
@@ -30,21 +35,24 @@ public class USSImageupload {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
Msg fileMiME = TypeDict.FileMiME(file);
|
||||
stream = new FileInputStream(file);
|
||||
Msg fileMiME = TypeDict.FileMiME(stream);
|
||||
meta.setHeader("content-type", fileMiME.getData().toString());
|
||||
upyun.setContentMD5(UpYun.md5(file));
|
||||
boolean result =
|
||||
upyun.writeFile(username + "/" + ShortUIDName + "." + prefix, file, true);
|
||||
upyun.writeFile(username + "/" + ShortUIDName + "." + prefix, stream, true, null);
|
||||
if (result) {
|
||||
|
||||
} else {
|
||||
System.err.println("上传失败");
|
||||
returnImage.setCode("400");
|
||||
}
|
||||
try {if(stream!=null){stream.close();}} catch (Exception e) { }
|
||||
}
|
||||
returnImage.setCode("200");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {if(stream!=null){stream.close();}} catch (Exception ex) { }
|
||||
logger.error("OSS上传发生异常:",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -25,7 +25,10 @@ import org.springframework.stereotype.Service;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@@ -35,7 +38,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
* @date 2020/1/9 15:46
|
||||
*/
|
||||
@Service
|
||||
@EnableAsync
|
||||
public class UploadServicel {
|
||||
private static Logger logger = LoggerFactory.getLogger(UploadServicel.class);
|
||||
@Autowired
|
||||
@@ -51,7 +53,6 @@ public class UploadServicel {
|
||||
@Autowired deleImages deleimages;
|
||||
@Autowired ImgViolationJudgeServiceImpl imgViolationJudgeService;
|
||||
|
||||
@Async("taskExecutor")
|
||||
public Msg uploadForLoc(
|
||||
HttpServletRequest request,
|
||||
File file,
|
||||
@@ -81,47 +82,23 @@ public class UploadServicel {
|
||||
return imgData;
|
||||
}
|
||||
}
|
||||
FileInputStream stream = new FileInputStream(file);
|
||||
String imguid = UUID.randomUUID().toString().replace("-", "");
|
||||
Msg msg1 = updateImgCheck(u, uploadConfig);
|
||||
if (!msg1.getCode().equals("300")) {
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg1;
|
||||
}
|
||||
sourceKeyId = group.getKeyid();
|
||||
Keys key = keysMapper.selectKeys(sourceKeyId);
|
||||
Long tmp = (memory == -1 ? -2 : UsedTotleMemory);
|
||||
if (tmp >= memory) {
|
||||
msg.setCode("4005");
|
||||
msg.setInfo(u == null ? "游客空间已用尽" : "您的可用空间不足");
|
||||
return msg;
|
||||
}
|
||||
if (file.length() > TotleMemory) {
|
||||
msg.setCode("4006");
|
||||
msg.setInfo("图像超出系统限制大小");
|
||||
return msg;
|
||||
}
|
||||
Msg fileMiME = TypeDict.FileMiME(file);
|
||||
if (!fileMiME.getCode().equals("200")) {
|
||||
msg.setCode("4000");
|
||||
msg.setInfo(fileMiME.getInfo());
|
||||
return msg;
|
||||
}
|
||||
if (md5key == null || md5key.equals("")) {
|
||||
FileInputStream fis = null;
|
||||
try {
|
||||
fis = new FileInputStream(file);
|
||||
md5key = DigestUtils.md5Hex(fis);
|
||||
} catch (Exception e) {
|
||||
logger.error("获取MD5错误",e);
|
||||
}finally{
|
||||
try {
|
||||
if (null != fis)
|
||||
fis.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
String prefix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
|
||||
JSONObject updateJson = JSONObject.parseObject(msg1.getData().toString());
|
||||
final JSONObject groupJson = updateJson.getJSONObject("group");
|
||||
final Group group = groupJson.toJavaObject(Group.class);
|
||||
Long memory = updateJson.getLong("memory");
|
||||
Long UsedTotleMemory = updateJson.getLong("UsedTotleMemory");
|
||||
Long TotleMemory = updateJson.getLong("TotleMemory");
|
||||
String updatePath = updateJson.getString("updatePath");
|
||||
if (uploadConfig.getBlacklist() != null) {
|
||||
String[] iparr = uploadConfig.getBlacklist().split(";");
|
||||
for (String s : iparr) {
|
||||
@@ -129,10 +106,66 @@ public class UploadServicel {
|
||||
file.delete();
|
||||
msg.setCode("4003");
|
||||
msg.setInfo("你暂时不能上传");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
sourceKeyId = group.getKeyid();
|
||||
Keys key = keysMapper.selectKeys(sourceKeyId);
|
||||
Long tmp = (memory == -1 ? -2 : UsedTotleMemory);
|
||||
if (tmp >= memory) {
|
||||
msg.setCode("4005");
|
||||
msg.setInfo(u == null ? "游客空间已用尽" : "您的可用空间不足");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
if (file.length() > TotleMemory) {
|
||||
msg.setCode("4006");
|
||||
msg.setInfo("图像超出系统限制大小");
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
Msg fileMiME = TypeDict.FileMiME(stream);
|
||||
if (!fileMiME.getCode().equals("200")) {
|
||||
msg.setCode("4000");
|
||||
msg.setInfo(fileMiME.getInfo());
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
file.delete();
|
||||
} catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
if (md5key == null || md5key.equals("")) {
|
||||
md5key = DigestUtils.md5Hex(stream);
|
||||
// FileInputStream fis = null;
|
||||
// try {
|
||||
// fis = new FileInputStream(file);
|
||||
// md5key = DigestUtils.md5Hex(fis);
|
||||
// } catch (Exception e) {
|
||||
// logger.error("获取MD5错误",e);
|
||||
// }finally{
|
||||
// try {
|
||||
// if (null != fis)
|
||||
// fis.close();
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
}
|
||||
String prefix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
|
||||
try {
|
||||
if(stream!=null){stream.close();}
|
||||
} catch (Exception e) { }
|
||||
// 判断路径格式 处理一下,去掉最前面的/
|
||||
String RootPath = key.getRootPath().replace(" ", "");
|
||||
if (!RootPath.equals("/")) {
|
||||
@@ -162,6 +195,7 @@ public class UploadServicel {
|
||||
}else{
|
||||
msg.setInfo("未获取到指定图像:110503");
|
||||
msg.setCode("110503");
|
||||
try { file.delete(); } catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
imgObj.setUpdatetime(df.format(new Date()));
|
||||
@@ -186,6 +220,7 @@ public class UploadServicel {
|
||||
// Integer insertRet = imgMapper.insertImgData(imgObj);
|
||||
Msg insertRet = imgServiceImpl.insertImgDataForCheck(imgObj,u,confdata,file.getName());
|
||||
if (insertRet.getCode().equals("000")) {
|
||||
try { file.delete(); } catch (Exception e) { }
|
||||
return insertRet;
|
||||
}
|
||||
|
||||
@@ -199,24 +234,21 @@ public class UploadServicel {
|
||||
jsonObject.put("name", imgObj.getImgname());
|
||||
jsonObject.put("imguid", imgObj.getImguid());
|
||||
imgViolationJudgeService.LegalImageCheck(imgObj,key);
|
||||
// new Thread(
|
||||
// () -> {
|
||||
// LegalImageCheck(imgObj);
|
||||
// })
|
||||
// .start();
|
||||
} else {
|
||||
imgServiceImpl.deleimgForImgUid(imgObj.getImguid());
|
||||
msg.setCode("5001");
|
||||
msg.setInfo("上传服务内部错误");
|
||||
try { file.delete(); } catch (Exception e) { }
|
||||
return msg;
|
||||
}
|
||||
file.delete();
|
||||
msg.setData(jsonObject);
|
||||
file.delete();
|
||||
return msg;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
msg.setInfo("上传时发生了一些错误");
|
||||
msg.setCode("110500");
|
||||
file.delete();
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
@@ -316,18 +348,20 @@ public class UploadServicel {
|
||||
return msg;
|
||||
}
|
||||
|
||||
private static Group group; // 上传用户或游客的所属分组
|
||||
private static Long memory; // 上传用户或者游客的分配容量 memory
|
||||
private static Long TotleMemory; // 用户或者游客下可使用的总容量 //maxsize
|
||||
private static Long UsedTotleMemory; // 用户或者游客已经用掉的总容量 //usermemory
|
||||
private static String updatePath = "tourist";
|
||||
// private static Group group; // 上传用户或游客的所属分组
|
||||
// private static Long memory; // 上传用户或者游客的分配容量 memory
|
||||
// private static Long TotleMemory; // 用户或者游客下可使用的总容量 //maxsize
|
||||
// private static Long UsedTotleMemory; // 用户或者游客已经用掉的总容量 //usermemory
|
||||
// private static String updatePath = "tourist";
|
||||
|
||||
// 判断用户 或 游客 当前上传图片的一系列校验
|
||||
private Msg updateImgCheck(User user, UploadConfig uploadConfig) {
|
||||
Msg msg = new Msg();
|
||||
java.text.DateFormat dateFormat = null;
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
String updatePath = "tourist";
|
||||
try {
|
||||
dateFormat = new SimpleDateFormat("yyyy/MM/dd");
|
||||
Group group = GetCurrentSource.GetSource(user == null ? null : user.getId());
|
||||
jsonObject.put("group",group);
|
||||
if (user == null) {
|
||||
if (uploadConfig.getIsupdate() != 1) {
|
||||
msg.setCode("1000");
|
||||
@@ -335,13 +369,15 @@ public class UploadServicel {
|
||||
return msg;
|
||||
}
|
||||
updatePath = "tourist";
|
||||
group = GetCurrentSource.GetSource(null);
|
||||
memory = Long.valueOf(uploadConfig.getVisitormemory()); // 单位 B 游客设置总量
|
||||
TotleMemory = Long.valueOf(uploadConfig.getFilesizetourists()); // 单位 B 游客单文件大小
|
||||
UsedTotleMemory =
|
||||
Long memory = Long.valueOf(uploadConfig.getVisitormemory()); // 单位 B 游客设置总量
|
||||
Long TotleMemory = Long.valueOf(uploadConfig.getFilesizetourists()); // 单位 B 游客单文件大小
|
||||
Long UsedTotleMemory =
|
||||
imgServiceImpl.getusermemory(0) == null
|
||||
? 0L
|
||||
: imgServiceImpl.getusermemory(0); // 单位 B
|
||||
jsonObject.put("memory",memory);
|
||||
jsonObject.put("TotleMemory",TotleMemory);
|
||||
jsonObject.put("UsedTotleMemory",UsedTotleMemory);
|
||||
} else {
|
||||
if (uploadConfig.getUserclose() != 1) {
|
||||
msg.setCode("1001");
|
||||
@@ -349,20 +385,26 @@ public class UploadServicel {
|
||||
return msg;
|
||||
}
|
||||
updatePath = user.getUsername();
|
||||
group = GetCurrentSource.GetSource(user.getId());
|
||||
memory = Long.valueOf(user.getMemory()); // 单位 B *1024*1024
|
||||
TotleMemory = Long.valueOf(uploadConfig.getFilesizeuser()); // 单位 B
|
||||
UsedTotleMemory =
|
||||
Long memory = Long.valueOf(user.getMemory()); // 单位 B *1024*1024
|
||||
Long TotleMemory = Long.valueOf(uploadConfig.getFilesizeuser()); // 单位 B
|
||||
Long UsedTotleMemory =
|
||||
imgServiceImpl.getusermemory(user.getId()) == null
|
||||
? 0L
|
||||
: imgServiceImpl.getusermemory(user.getId()); // 单位 B
|
||||
jsonObject.put("memory",memory);
|
||||
jsonObject.put("TotleMemory",TotleMemory);
|
||||
jsonObject.put("UsedTotleMemory",UsedTotleMemory);
|
||||
}
|
||||
if (uploadConfig.getUrltype() == 2) {
|
||||
updatePath = dateFormat.format(new Date());
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||
updatePath = currentDate.format(formatter);
|
||||
}
|
||||
jsonObject.put("updatePath",updatePath);
|
||||
msg.setData(jsonObject);
|
||||
msg.setCode("300");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
logger.error("updateImgCheck异常:",e);
|
||||
msg.setCode("500");
|
||||
}
|
||||
return msg;
|
||||
|
||||
@@ -32,6 +32,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -66,9 +67,7 @@ public class WebDAVImageupload {
|
||||
String prefix = entry.getKey().get("prefix");
|
||||
String ShortUIDName = entry.getKey().get("name");
|
||||
file = entry.getValue();
|
||||
Msg fileMiME = TypeDict.FileMiME(file);
|
||||
createDirectories(httpClient, key.getEndpoint(), "/" + username);
|
||||
System.out.println("WebDAV地址:"+key.getEndpoint() + "/" + username + "/" + ShortUIDName + "." + prefix);
|
||||
HttpPut httpPut = new HttpPut(key.getEndpoint() + "/" + username + "/" + ShortUIDName + "." + prefix);
|
||||
FileEntity fileEntity = new FileEntity(file, ContentType.DEFAULT_BINARY);
|
||||
httpPut.setEntity(fileEntity);
|
||||
@@ -84,7 +83,7 @@ public class WebDAVImageupload {
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
logger.error("ImageuploadWebDAV:92异常",e);
|
||||
logger.error("ImageuploadWebDAV:87异常",e);
|
||||
returnImage.setCode("500");
|
||||
}
|
||||
return returnImage;
|
||||
|
||||
@@ -65,7 +65,7 @@ public class ImgUrlUtil {
|
||||
httpUrlConnection.setConnectTimeout(300000);
|
||||
httpUrlConnection.setReadTimeout(300000);
|
||||
httpUrlConnection.connect();
|
||||
String code = new Integer(httpUrlConnection.getResponseCode()).toString();
|
||||
String code = Integer.toString(httpUrlConnection.getResponseCode());
|
||||
String message = httpUrlConnection.getResponseMessage();
|
||||
System.out.println("getResponseCode code ="+ code);
|
||||
System.out.println("getResponseMessage message ="+ message);
|
||||
@@ -85,7 +85,7 @@ public class ImgUrlUtil {
|
||||
|
||||
|
||||
public static Map<String ,Object> downLoadFromUrl(String urlStr,String referer,String fileName,String savePath){
|
||||
|
||||
FileInputStream fileInputStream = null;
|
||||
Map<String ,Object> resmap = new HashMap<>();
|
||||
Map<String ,String> map = checkURLStatusCode(urlStr);
|
||||
if(map.size()==0 || map.get("Check").equals("false")){
|
||||
@@ -120,7 +120,8 @@ public class ImgUrlUtil {
|
||||
}
|
||||
//下载并且保存成功后 判断格式 如果不是图像格式 就删除
|
||||
if(new File(saveDir+File.separator+fileName).exists()){
|
||||
Msg msg = TypeDict.FileMiME(file);
|
||||
fileInputStream = new FileInputStream(file);
|
||||
Msg msg = TypeDict.FileMiME(fileInputStream);
|
||||
if(msg.getCode().equals("200")){
|
||||
File f = new File(saveDir+File.separator+fileName);
|
||||
String imgPath = saveDir+File.separator+fileName+"."+(msg.getData().toString().replace("image/",""));
|
||||
@@ -137,9 +138,16 @@ public class ImgUrlUtil {
|
||||
resmap.put("res",false);
|
||||
resmap.put("StatusCode","500");
|
||||
}
|
||||
if(fileInputStream!=null){
|
||||
fileInputStream.close();
|
||||
}
|
||||
return resmap;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
try{
|
||||
if(fileInputStream!=null){
|
||||
fileInputStream.close();
|
||||
}
|
||||
}catch (Exception e2){ }
|
||||
resmap.put("res",false);
|
||||
resmap.put("StatusCode","500");
|
||||
return resmap;
|
||||
|
||||
@@ -2,6 +2,9 @@ package cn.hellohao.utils;
|
||||
|
||||
import cn.hellohao.pojo.Chunk;
|
||||
import cn.hellohao.pojo.Msg;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.*;
|
||||
@@ -13,6 +16,7 @@ import java.text.DecimalFormat;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class SetFiles {
|
||||
private static Logger logger = LoggerFactory.getLogger(SetFiles.class);
|
||||
// 转换文件方法
|
||||
public static File changeFile(MultipartFile multipartFile) {
|
||||
// 获取文件名
|
||||
@@ -47,17 +51,16 @@ public class SetFiles {
|
||||
|
||||
// 转换文件方法
|
||||
public static File changeFile_new(MultipartFile multipartFile) {
|
||||
// 获取文件名
|
||||
String fileName = multipartFile.getOriginalFilename();//getOriginalFilename
|
||||
// 获取文件后缀
|
||||
String prefix = fileName.substring(fileName.lastIndexOf("."));
|
||||
// todo 修改临时文件文件名
|
||||
File file = null;
|
||||
try {
|
||||
file = File.createTempFile(fileName, prefix);
|
||||
multipartFile.transferTo(file);
|
||||
String new_FileName = System.getProperty("java.io.tmpdir")+"hellohao_tmp_upload"+File.separator+fileName;
|
||||
file = new File(new_FileName);
|
||||
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), file);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("SetFiles.changeFile_new", e);
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
@@ -7,15 +7,16 @@ import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class TypeDict {
|
||||
|
||||
//apache大法
|
||||
public static Msg FileMiME(File file ){
|
||||
public static Msg FileMiME(InputStream stream){
|
||||
final Msg msg = new Msg();
|
||||
try {
|
||||
Tika tika = new Tika();
|
||||
String fileType = tika.detect(file);
|
||||
String fileType = tika.detect(stream);
|
||||
if (fileType != null && fileType.contains("/")) {
|
||||
if(fileType.contains("image/")){
|
||||
msg.setData(fileType);
|
||||
|
||||
Reference in New Issue
Block a user