ecology_maven/weaver/alioss/AliOSSObjectManager.java

369 lines
10 KiB
Java
Raw Normal View History

2021-11-14 15:29:16 +08:00
/*
* Created on 2014-11-04
* Copyright (c) 2001-2014
*
*
*/
package weaver.alioss;
import weaver.alioss.AliOSSObjectUtil;
import weaver.conn.RecordSet;
import weaver.file.AESCoder;
import weaver.file.Prop;
import weaver.general.BaseBean;
import weaver.general.IpUtils;
import weaver.general.Util;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.zip.ZipInputStream;
/**
* Description:
* @author
* @date 2014-11-04
*/
public class AliOSSObjectManager extends BaseBean {
/**
*
*/
public AliOSSObjectManager() {
try {
} catch (Exception ex) {
}
}
/**
*
* @param filerealpath
* @param filename
* @param iszip
* @param isaesencrypt
* @param aescode
*/
public void uploadFile(final String filerealpath,final String filename,final String iszip,final String isaesencrypt,final String aescode){
//判断阿里云工具类能否正常加载
if(!isexistAliOSSObjectUtil()){
return ;
}
//判断是否启用了阿里云
if(!AliOSSObjectUtil.isEnable()){
return ;
}
try {
if(isonlyAliOSS()){
uploadFileThread(filerealpath, filename, iszip, isaesencrypt,aescode);
}else{
new Thread(new Runnable() {
public void run() {
uploadFileThread(filerealpath, filename, iszip, isaesencrypt,aescode);
}
}).start();
}
} catch (Exception ex) {
}
}
/**
* 线
* @param filerealpath
* @param filename
* @param iszip
* @param isaesencrypt
* @param aescode
*/
private void uploadFileThread(String filerealpath,String filename,String iszip,String isaesencrypt,String aescode){
InputStream imagefile = null;
try {
String tokenKey=this.getTokenKeyByFileRealPath(filerealpath);
File thefile = new File(filerealpath) ;
if(iszip.equals("1")) {
ZipInputStream zin = new ZipInputStream(new FileInputStream(thefile)) ;
if(zin.getNextEntry() != null)
imagefile = new BufferedInputStream(zin);
} else{
imagefile = new BufferedInputStream(new FileInputStream(thefile)) ;
}
if(isaesencrypt.equals("1")){
imagefile = AESCoder.decrypt(imagefile, aescode);
}
AliOSSObjectUtil.autoUploadFile(tokenKey, filename,imagefile);
} catch (Exception ex) {
}finally{
try{
if(imagefile!=null){
imagefile.close() ;
}
}catch(Exception ex){
}
}
}
/**
* Key
* @param filerealpath
* @return Key
*/
public static String getTokenKeyByFileRealPath(String filerealpath){
String tokenKey="";
try {
if(filerealpath==null||filerealpath.trim().equals("")){
return tokenKey;
}
int i = filerealpath.lastIndexOf("/");
int j = filerealpath.lastIndexOf("\\");
int index = i >j?i:j;
if(index<0){
return tokenKey;
}
int point = 0;
String tempstr = filerealpath.substring(index,filerealpath.length());
if(tempstr.indexOf(".")!=-1){
point = index + tempstr.lastIndexOf(".");
}else{
point = filerealpath.length();
}
tokenKey = filerealpath.substring(index-8,point)+".wfile";
tokenKey = tokenKey.replaceAll("\\\\", "/");
} catch (Exception ex) {
}
return tokenKey;
}
/**
*
* @param req
* @return
*/
public static boolean isEnableForDsp(HttpServletRequest req){
boolean isEnableForDsp=false;
try {
if(!isexistAliOSSObjectUtil()){
return isEnableForDsp;
}
boolean isEnable=AliOSSObjectUtil.isEnable();
if(!isEnable){
return isEnableForDsp;
}
String ip=Util.getIpAddr(req);
boolean checktmp = checkIpInner(ip);
if(isEnable&&(!checktmp)){
isEnableForDsp=true;
}
} catch (Exception ex) {
}
return isEnableForDsp;
}
/**
*
* @param imageFileId id
* @return
*/
public static boolean isEnableForImageFile(int imageFileId){
boolean isEnableForImageFile=false;
try {
String tokenKey="";
String storageStatus="";
RecordSet rs=new RecordSet();
rs.executeSql("select TokenKey,StorageStatus from ImageFile where imageFileId="+imageFileId);
if(rs.next()){
tokenKey=Util.null2String(rs.getString("TokenKey"));
storageStatus=Util.null2String(rs.getString("StorageStatus"));
}
if(!tokenKey.equals("")&&storageStatus.equals("1")){
isEnableForImageFile=true;
}
} catch (Exception ex) {
}
return isEnableForImageFile;
}
/**
* idtokenKey
* @param imageFileId id
* @return tokenKey storageStatus1tokenKey
*/
public static String getTokenKeyByImageFileId(int imageFileId){
String tokenKey="";
try {
String storageStatus="";
RecordSet rs=new RecordSet();
rs.executeSql("select TokenKey,StorageStatus from ImageFile where imageFileId="+imageFileId);
if(rs.next()){
tokenKey=Util.null2String(rs.getString("TokenKey"));
storageStatus=Util.null2String(rs.getString("StorageStatus"));
}
if(!storageStatus.equals("1")){
tokenKey="";
}
} catch (Exception ex) {
}
return tokenKey;
}
/**
*
* @param ipaddress
* @return boolean true : false:
*/
private static boolean checkIpInner(String ipaddress) {
boolean checktmp = false;
String isInner = Util.null2String(Prop.getPropValue("alioss","isInner"));
if(isInner.equals("false")){
return checktmp;
}
ipaddress=Util.null2String(ipaddress);
long ipNum = IpUtils.ip2number(ipaddress);
long a1=IpUtils.ip2number("10.0.0.0");
long a2=IpUtils.ip2number("10.255.255.255");
long b1=IpUtils.ip2number("172.16.0.0");
long b2=IpUtils.ip2number("172.31.255.255");
long c1=IpUtils.ip2number("192.168.0.0");
long c2=IpUtils.ip2number("192.168.255.255");
if(isInner(ipNum,a1,a2) || isInner(ipNum,b1,b2) || isInner(ipNum,c1,c2) || ipaddress.equals("127.0.0.1")){
checktmp=true;
}
return checktmp;
}
private static boolean isInner(long userIp,long begin,long end){
return (userIp>=begin) && (userIp<=end);
}
/**
* Safari
* @param req
* @return isEnableForDsp
*/
public static boolean isSafari(HttpServletRequest req){
boolean isSafari=false;
try {
String userAgent=Util.null2String(req.getHeader("USER-AGENT"));
if(userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") < 1 && userAgent.indexOf("Edge") < 1){
isSafari=true;
}
} catch (Exception ex) {
}
return isSafari;
}
/**
*
* @return isexist
*/
public static boolean isexistAliOSSObjectUtil(){
boolean isexist= true;
try{
Class.forName("weaver.alioss.AliOSSObjectUtil");
}catch(ExceptionInInitializerError e){
isexist = false; //判断云存储是否正常
}catch(IllegalArgumentException e){
isexist = false; //判断云存储是否正常
}catch(NoClassDefFoundError e){
isexist = false; //判断云存储是否正常
}catch(Exception e){
isexist = false; //判断云存储是否正常
}
return isexist;
}
/**
*
*
* @return isexist
*/
public static boolean isAliOSSToServer(String comefrom){
boolean isAliOSSToServer= false;
if(comefrom==null||comefrom.trim().equals("")){
return isAliOSSToServer;
}
try{
//判断阿里云工具类能否正常加载
if(!isexistAliOSSObjectUtil()){
return isAliOSSToServer;
}
//判断是否启用了阿里云
if(!AliOSSObjectUtil.isEnable()){
return isAliOSSToServer;
}
//判断是否配置的是阿里云的内网地址
if(AliOSSObjectUtil.getAliossServerAddress().contains("internal.aliyuncs")){
isAliOSSToServer = true;
}
String AliOSSToServerComeFrom = Util.null2String(Prop.getPropValue("alioss","AliOSSToServerComeFrom"));
if(AliOSSToServerComeFrom.trim().equals("")){
AliOSSToServerComeFrom="WorkflowToDoc";
}
if((","+AliOSSToServerComeFrom+",").indexOf(","+comefrom+",")>=0){
isAliOSSToServer=true;
}
}catch(Exception e){
isAliOSSToServer = false; //判断云存储是否正常
}
return isAliOSSToServer;
}
/**
*
*
* @return true:
*/
public static boolean isonlyAliOSS(){
boolean isonlyAliOSS= false;
try{
//判断阿里云工具类能否正常加载
if(!isexistAliOSSObjectUtil()){
return isonlyAliOSS;
}
//判断是否启用了阿里云
if(!AliOSSObjectUtil.isEnable()){
return isonlyAliOSS;
}
String onlyAliOSS = Util.null2String(Prop.getPropValue("alioss","onlyAliOSS"));
if(onlyAliOSS.trim().equals("1")){
isonlyAliOSS=true;
}
}catch(Exception e){
isonlyAliOSS = false; //判断云存储是否正常
}
return isonlyAliOSS;
}
/**
* oss
* @return
*/
public static boolean isEnable(){
boolean isEnable=false;
//判断阿里云工具类能否正常加载
if(!isexistAliOSSObjectUtil()){
return isEnable;
}
isEnable=Prop.getPropValue("alioss","status").equalsIgnoreCase("1");
return isEnable;
}
}