ecology_maven/weaver/file/ImageFileManager.java

1149 lines
44 KiB
Java
Raw Normal View History

2021-11-14 15:29:16 +08:00
package weaver.file;
import DBstep.iMsgServer2000;
import com.api.odoc.util.OdocFileUtil;
import weaver.alioss.AliOSSObjectManager;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocManager;
import weaver.docs.docs.ImageFileIdUpdate;
import weaver.file.util.FileDeleteUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.sm.SM4Utils;
import weaver.system.SystemComInfo;
import java.io.*;
import java.util.Date;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
* Title:
* Company: weaver
*
* @author fanggsh
* @version 1.0
*/
public class ImageFileManager extends BaseBean {
private static ImageFileIdUpdate imageFileIdUpdate = new ImageFileIdUpdate();
private int imageFileId; //附件id
private String imageFileName;//文件名称
private String imageFileType; //文件MIME类型
private int imageFileUsed; //文件使用次数
private String fileRealPath; //文件存放目录
private String isZip; //是否压缩 0否 1
private String isencrypt; //是否加密 0否 1是 该字段已经基本不使用
private String filesize; //文件大小 单位为字节
private int downloads; //下载次数
private String miniimgpath; //缩略图存放路径
private String imgsize; //缩略图大小
private String isFTP; //是否存储到FTP 1:是 0或其它
private int FTPConfigId;//FTP服务器配置ID
private int isaesencrypt;//是否使用AES附件加密 1:是 0或其它
private String aescode; //AES加密密码
private String tokenKey; //令牌标记
private String storageStatus; //传输标志 1成功 2文件异常 3传输中 4不需要传到云 其他:未传输
private String comefrom; // 附件来源 WorkflowToDoc流程存为文档
private int secretlevel; //密级
private String secretvalidity; // 保密期限
private byte[] data;
/**
*
*/
public ImageFileManager() {
try {
resetParameter();
} catch (Exception ex) {
writeLog(ex);
}
}
/**
*
*/
public void resetParameter() {
imageFileId=0;
imageFileName="";
imageFileType="";
imageFileUsed=1;
fileRealPath="";
isZip="";
isencrypt="";
filesize="";
downloads=0;
miniimgpath="";
imgsize="";
isFTP="";
FTPConfigId=0;
isaesencrypt=0;
aescode="";
tokenKey="";
storageStatus="";
comefrom="";
secretlevel=DocManager.DEFAILT_SECRET_LEVEL;
secretvalidity="";
data=null;
}
/**
* ID
*
* @return ID
* @throws Exception
*/
public int getImageFileId() {
return imageFileId;
}
/**
*
*
* @return
* @throws Exception
*/
public String getImageFileName() {
return imageFileName;
}
/**
* MIME
*
* @return MIME
* @throws Exception
*/
public String getImageFileType() {
return imageFileType;
}
public void setImageFileType(String para) {
this.imageFileType = para;
}
/**
* 使
*
* @return 使
* @throws Exception
*/
public int getImageFileUsed() {
return imageFileUsed;
}
/**
*
*
* @return
* @throws Exception
*/
public String getFileRealPath() {
return fileRealPath;
}
/**
*
*
* @return
* @throws Exception
*/
public String getIsZip() {
return isZip;
}
/**
* 使
*
* @return
* @throws Exception
*/
public String getIsencrypt() {
return isencrypt;
}
/**
*
*
* @return
* @throws Exception
*/
public String getFilesize() {
return filesize;
}
/**
*
*
* @return
* @throws Exception
*/
public int getDownloads() {
return downloads;
}
/**
*
*
* @return
* @throws Exception
*/
public String getMiniimgpath() {
return miniimgpath;
}
/**
*
*
* @return
* @throws Exception
*/
public String getImgsize() {
return imgsize;
}
/**
* FTP 1: 0
*
* @return FTP 1: 0
* @throws Exception
*/
public String getIsFTP() {
return isFTP;
}
/**
* FTPID
*
* @return FTPID
* @throws Exception
*/
public int getFTPConfigId() {
return FTPConfigId;
}
/**
* 使AES
*
* @return 使AES
* @throws Exception
*/
public int getIsaesencrypt() {
return isaesencrypt;
}
/**
* AES
*
* @return AES
* @throws Exception
*/
public String getAescode() {
return aescode;
}
/**
*
*
* @return
* @throws Exception
*/
public String getTokenKey() {
return tokenKey;
}
/**
*
*
* @return
* @throws Exception
*/
public String getStorageStatus() {
return storageStatus;
}
public void setImageFileId(int imageFileId) {
this.imageFileId = imageFileId;
}
public int getSecretlevel() {
return secretlevel;
}
public void setSecretlevel(int secretlevel) {
this.secretlevel = secretlevel;
}
public String getSecretvalidity() {
return secretvalidity;
}
public void setSecretvalidity(String secretvalidity) {
this.secretvalidity = secretvalidity;
}
/**
*
*
* @return
* @throws Exception
*/
public String getComefrom() {
return comefrom;
}
public Date getLastModified()
{
Date lastModified = null;
boolean isonlyAliOSS=false;
if(!tokenKey.equals("")&&storageStatus.equals("1")&&AliOSSObjectManager.isonlyAliOSS()){
isonlyAliOSS=true;
}
if(isonlyAliOSS){
lastModified = weaver.alioss.AliOSSObjectUtil.getLastModified(tokenKey);
}else{
if(fileRealPath!=null&&!fileRealPath.equals("")){
File file = new File(fileRealPath);
boolean bool = file.exists();
if(bool)
{
long millisec = file.lastModified();
lastModified = new Date(millisec);
}
}
}
return lastModified;
}
public Date getLastModified(String fileRealPath)
{
Date lastModified = null;
if(fileRealPath!=null&&!fileRealPath.equals("")) {
File file = new File(fileRealPath);
boolean bool = file.exists();
if (bool) {
long millisec = file.lastModified();
lastModified = new Date(millisec);
}
}
return lastModified;
}
/**
*
*
* @param docid_para
*
*/
public void setImagFileName(String para) {
this.imageFileName = para;
}
/**
*
*
* @param docid_para
*
*/
public void setComefrom(String para) {
this.comefrom = para;
}
/**
* ID
*
* @param docid_para
* ID
*/
public void setData(byte[] para) {
this.data = para;
}
/**
*
*
* @return
* @throws Exception
*/
public InputStream getInputStream() {
InputStream imagefile = null;
try{
boolean isonlyAliOSS=false;
if(!tokenKey.equals("")&&storageStatus.equals("1")&&AliOSSObjectManager.isonlyAliOSS()){
isonlyAliOSS=true;
}
if(isonlyAliOSS){
imagefile=weaver.alioss.AliOSSObjectUtil.downloadFile(tokenKey);
}else{
if(fileRealPath!=null&&!fileRealPath.equals("")){
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)) ;
}
}else{
ConnStatement statement = null;
try {
statement = new ConnStatement();
boolean isoracle = ("oracle").equals(statement.getDBType()) ;
String sql = "select imagefile from ImageFile where imagefileid = " + imageFileId;
statement.setStatementSql(sql);
statement.executeQuery();
if(statement.next()){
if( isoracle ) imagefile = new BufferedInputStream(statement.getBlobBinary("imagefile")) ;
else imagefile = new BufferedInputStream(statement.getBinaryStream("imagefile")) ;
}
} catch(Exception ex) {
}finally {
try{
if(statement!=null) statement.close() ;
}catch(Exception ex){
}
}
}
if(isaesencrypt==1){
imagefile = AESCoder.decrypt(imagefile, aescode);
}
}
}catch(Exception ex){
}
return imagefile;
}
/**
*
* @param imageFileId_para
* @return
* @throws Exception
*/
public static InputStream getInputStreamById(int imageFileId_para) {
InputStream imagefile = null;
try{
RecordSet rs = new RecordSet();
String sql = "select imagefilename,fileRealPath,isZip,isaesencrypt,aescode,tokenKey,storageStatus from ImageFile where imageFileId = " + imageFileId_para;
rs.executeSql(sql);
if(rs.next()){
String static_fileRealPath=Util.null2String(rs.getString("fileRealPath"));
String static_isZip=Util.null2String(rs.getString("isZip"));
int static_isaesencrypt=Util.getIntValue(rs.getString("isaesencrypt"),0);
String static_aescode=Util.null2String(rs.getString("aescode"));
String static_tokenKey=Util.null2String(rs.getString("tokenKey"));
String static_storageStatus=Util.null2String(rs.getString("storageStatus"));
String imagefilename=Util.null2String(rs.getString("imagefilename"));
boolean isonlyAliOSS=false;
if(!static_tokenKey.equals("")&&static_storageStatus.equals("1")&&AliOSSObjectManager.isonlyAliOSS()){
isonlyAliOSS=true;
}
if(isonlyAliOSS){
imagefile=weaver.alioss.AliOSSObjectUtil.downloadFile(static_tokenKey);
}else{
if(static_fileRealPath!=null&&!static_fileRealPath.equals("")){
File thefile = new File(static_fileRealPath) ;
if(static_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)) ;
}
}else{
ConnStatement statement = null;
try {
statement = new ConnStatement();
boolean isoracle = ("oracle").equals(statement.getDBType()) ;
statement.setStatementSql("select imagefile from ImageFile where imagefileid = " + imageFileId_para);
statement.executeQuery();
if(statement.next()){
if( isoracle ) imagefile = new BufferedInputStream(statement.getBlobBinary("imagefile")) ;
else imagefile = new BufferedInputStream(statement.getBinaryStream("imagefile")) ;
}
} catch(Exception ex) {
}finally {
try{
if(statement!=null) statement.close() ;
}catch(Exception ex){
}
}
}
if(static_isaesencrypt==1){
imagefile = AESCoder.decrypt(imagefile, static_aescode);
}
}
if(null!=imagefile){
String extName = OdocFileUtil.getFileExt(imagefilename);
int byteread;
byte data[] = new byte[1024];
if(isOfficeToDocument(extName)&&isMsgObjToDocument()){
//正文的处理
ByteArrayOutputStream bout = null;
try {
bout = new ByteArrayOutputStream() ;
while((byteread = imagefile.read(data)) != -1) {
bout.write(data, 0, byteread) ;
bout.flush() ;
}
byte[] fileBody = bout.toByteArray();
iMsgServer2000 MsgObj = new iMsgServer2000();
MsgObj.MsgFileBody(fileBody); //将文件信息打包
fileBody = MsgObj.ToDocument(MsgObj.MsgFileBody()); //通过iMsgServer200 将pgf文件流转化为普通Office文件流
imagefile = new ByteArrayInputStream(fileBody);
bout.close();
}
catch(Exception e) {
if(bout!=null) bout.close();
new BaseBean().writeLog("ImageFileManager.getInputStreamById(int imageFileId_para),exception1:",e);
}
}
}
}
}catch(Exception ex){
new BaseBean().writeLog("ImageFileManager.getInputStreamById(int imageFileId_para),exception:",ex);
}
return imagefile;
}
private static boolean isOfficeToDocument(String extName){
boolean isOfficeForToDocument=false;
if("xls".equalsIgnoreCase(extName) || "doc".equalsIgnoreCase(extName)||"wps".equalsIgnoreCase(extName)||"ppt".equalsIgnoreCase(extName)||"docx".equalsIgnoreCase(extName)||"xlsx".equalsIgnoreCase(extName)||"pptx".equalsIgnoreCase(extName)){
isOfficeForToDocument=true;
}
return isOfficeForToDocument;
}
private static boolean isMsgObjToDocument(){
boolean isMsgObjToDocument=true;
BaseBean basebean = new BaseBean();
String mClientName=Util.null2String(basebean.getPropValue("weaver_obj","iWebOfficeClientName"));
boolean isIWebOffice2003 = (mClientName.indexOf("iWebOffice2003")>-1)?true:false;
String isHandWriteForIWebOffice2009=Util.null2String(basebean.getPropValue("weaver_obj","isHandWriteForIWebOffice2009"));
if(isIWebOffice2003||isHandWriteForIWebOffice2009.equals("0")){
isMsgObjToDocument=false;
}
if(mClientName.indexOf("iWebOffice2009")>-1)
{
isMsgObjToDocument=true;
}
return isMsgObjToDocument;
}
/**
* ID
*
* @throws Exception
*/
public void getImageFileInfoById(int imageFileId_para) {
try{
RecordSet rs = new RecordSet();
String sql = "select t1.imageFileId,t1.imageFileName,t1.imageFileType,t1.imageFileUsed,t1.fileRealPath,t1.isZip,t1.isencrypt,t1.filesize,t1.downloads,t1.miniimgpath,t1.imgsize,t1.isFTP,t1.FTPConfigId,t1.isaesencrypt,t1.aescode,t1.tokenKey,t1.storageStatus,t1.comefrom,t2.imagefilename as realname from ImageFile t1 left join DocImageFile t2 on t1.imagefileid = t2.imagefileid where t1.imagefileid = " + imageFileId_para;
rs.executeSql(sql);
if(rs.next()){
imageFileId=Util.getIntValue(rs.getString("imageFileId"),0);
imageFileName=Util.null2String(rs.getString("realname"));
if(imageFileName.equals("")){
imageFileName = Util.null2String(rs.getString("imageFileName"));
}
imageFileType=Util.null2String(rs.getString("imageFileType"));
imageFileUsed=Util.getIntValue(rs.getString("imageFileUsed"),0);
fileRealPath=Util.null2String(rs.getString("fileRealPath"));
isZip=Util.null2String(rs.getString("isZip"));
isencrypt=Util.null2String(rs.getString("isencrypt"));
filesize=Util.null2String(rs.getString("filesize"));
downloads=Util.getIntValue(rs.getString("downloads"),0);
miniimgpath=Util.null2String(rs.getString("miniimgpath"));
imgsize=Util.null2String(rs.getString("imgsize"));
isFTP=Util.null2String(rs.getString("isFTP"));
FTPConfigId=Util.getIntValue(rs.getString("FTPConfigId"),0);
isaesencrypt=Util.getIntValue(rs.getString("isaesencrypt"),0);
aescode=Util.null2String(rs.getString("aescode"));
tokenKey=Util.null2String(rs.getString("tokenKey"));
storageStatus=Util.null2String(rs.getString("storageStatus"));
comefrom=Util.null2String(rs.getString("comefrom"));
secretlevel = Util.getIntValue(rs.getString("secretlevel"));
secretvalidity = Util.null2s(rs.getString(secretvalidity),"");
}
}catch(Exception ex){
}
}
/**
*
* @param imageFileId_para
* @return id
*/
public static int copyImageFile(int imageFileId_para) {
int new_imageFileId=0;
if(imageFileId_para<=0){
return new_imageFileId;
}
try{
String static_imageFileName="";
String static_imageFileType="";
int static_imageFileUsed=0;
String static_fileRealPath="";
String static_isZip="";
String static_isencrypt="";
String static_filesize="";
int static_downloads=0;
String static_miniimgpath="";
String static_imgsize="";
String static_isFTP="";
int static_FTPConfigId=0;
int static_isaesencrypt=0;
String static_aescode="";
String static_tokenKey="";
String static_storageStatus="";
String static_comefrom="";
int static_mainimagefile=0;
int static_secretlevel=DocManager.DEFAILT_SECRET_LEVEL;
String static_secretvalidity="";
RecordSet rs = new RecordSet();
String sql = "select t1.imageFileId,t1.imageFileName,t1.imageFileType,t1.imageFileUsed,t1.fileRealPath,t1.isZip,t1.isencrypt,t1.filesize,t1.downloads,t1.miniimgpath,t1.imgsize,t1.isFTP,t1.FTPConfigId,t1.isaesencrypt,t1.aescode,t1.tokenKey,t1.storageStatus,t1.comefrom,t1.mainimagefile," +
"t1.secretlevel,t1.secretvalidity,t2.imagefilename as realname from ImageFile t1 left join DocImageFile t2 on t1.imagefileid = t2.imagefileid where t1.imagefileid = " + imageFileId_para;
rs.executeSql(sql);
if(rs.next()){
static_imageFileName=Util.null2String(rs.getString("realname"));
if(static_imageFileName.equals("")){
static_imageFileName = Util.null2String(rs.getString("imageFileName"));
}
static_imageFileType=Util.null2String(rs.getString("imageFileType"));
static_imageFileUsed=Util.getIntValue(rs.getString("imageFileUsed"),0);
static_fileRealPath=Util.null2String(rs.getString("fileRealPath"));
static_isZip=Util.null2String(rs.getString("isZip"));
static_isencrypt=Util.null2String(rs.getString("isencrypt"));
static_filesize=Util.null2String(rs.getString("filesize"));
static_downloads=Util.getIntValue(rs.getString("downloads"),0);
static_miniimgpath=Util.null2String(rs.getString("miniimgpath"));
static_imgsize=Util.null2String(rs.getString("imgsize"));
static_isFTP=Util.null2String(rs.getString("isFTP"));
static_FTPConfigId=Util.getIntValue(rs.getString("FTPConfigId"),0);
static_isaesencrypt=Util.getIntValue(rs.getString("isaesencrypt"),0);
static_aescode=Util.null2String(rs.getString("aescode"));
static_tokenKey=Util.null2String(rs.getString("tokenKey"));
static_storageStatus=Util.null2String(rs.getString("storageStatus"));
static_comefrom=Util.null2String(rs.getString("comefrom"));
static_mainimagefile=Util.getIntValue(rs.getString("mainimagefile"),0);
static_secretlevel=Util.getIntValue(rs.getString("secretlevel"),0);
static_secretvalidity=Util.null2String(rs.getString("secretvalidity"));
}
if(static_imageFileName==null||static_imageFileName.trim().equals("")){
return new_imageFileId;
}
SystemComInfo syscominfo = new SystemComInfo();
String createdir = FileUpload.getCreateDir(syscominfo.getFilesystem()) ;
String fileName = UUID.randomUUID().toString();// System.currentTimeMillis() + "";解决高并发的情况下,文件重复 李迎新
if(static_fileRealPath.lastIndexOf(".")>-1){
fileName+=static_fileRealPath.substring(static_fileRealPath.lastIndexOf("."));
}
String new_fileRealPath = createdir + fileName ;
String new_tokenKey=AliOSSObjectManager.getTokenKeyByFileRealPath(new_fileRealPath);
String new_storageStatus="";
new_imageFileId=imageFileIdUpdate.getImageFileNewId();
boolean isonlyAliOSS=false;
if(!static_tokenKey.equals("")&&static_storageStatus.equals("1")&&AliOSSObjectManager.isonlyAliOSS()){
isonlyAliOSS=true;
}
if(isonlyAliOSS){
boolean copyStatus=weaver.alioss.AliOSSObjectUtil.copyObject(static_tokenKey, new_tokenKey);
if(copyStatus){
new_storageStatus="1";
}
}else if(!static_tokenKey.equals("")&&static_storageStatus.equals("1")&&AliOSSObjectManager.isEnable()){
boolean copyStatus=weaver.alioss.AliOSSObjectUtil.copyObject(static_tokenKey, new_tokenKey);
if(copyStatus){
new_storageStatus="1";
}
FileManage.createDir(createdir);
FileManage.copy(static_fileRealPath, new_fileRealPath);
}else{
FileManage.createDir(createdir);
FileManage.copy(static_fileRealPath, new_fileRealPath);
}
ConnStatement statement = null;
try{
statement = new ConnStatement();
sql = "insert into ImageFile(" +
"imageFileId,imageFileName,imageFileType,imageFileUsed,fileRealPath" +
",isZip,isencrypt,filesize,downloads,miniimgpath" +
",imgsize,isFTP,FTPConfigId,isaesencrypt,aescode" +
" ,tokenKey,storageStatus,comefrom,mainimagefile" +
",secretlevel,secretvalidity"+
") values(" +
"?,?,?,?,?" +
",?,?,?,?,?" +
",?,?,?,?,?" +
",?,?,?,?" +
",?,?"+
")";
statement.setStatementSql(sql);
statement.setInt(1, new_imageFileId);
statement.setString(2, static_imageFileName);
statement.setString(3, static_imageFileType);
statement.setInt(4,static_imageFileUsed);
statement.setString(5, new_fileRealPath);
statement.setString(6, static_isZip);
statement.setString(7, static_isencrypt);
statement.setString(8, static_filesize);
statement.setInt(9, static_downloads);
statement.setString(10, static_miniimgpath);
statement.setString(11, static_imgsize);
statement.setString(12, static_isFTP);
statement.setInt(13,static_FTPConfigId);
statement.setInt(14, static_isaesencrypt);
statement.setString(15, static_aescode);
statement.setString(16, new_tokenKey);
statement.setString(17, new_storageStatus);
statement.setString(18, static_comefrom);
statement.setInt(19, static_mainimagefile);
statement.setInt(20, static_secretlevel);
statement.setString(21, static_secretvalidity);
statement.executeUpdate();
}catch(Exception ex){
new BaseBean().writeLog("imagefilemanager error",ex);
new BaseBean().writeLog("imagefilemanager error haha:"+ex.getMessage());
} finally {
try {
statement.close();
} catch (Exception ex) {
}
}
}catch(Exception ee){
new BaseBean().writeLog("imagefilemanager error",ee);
new BaseBean().writeLog("imagefilemanager error haha:"+ee.getMessage());
}
return new_imageFileId;
}
/**
*
* @return id
*/
public int saveImageFile(){
int new_imageFileId=0;
if(this.data==null){
return new_imageFileId;
}
try{
String static_imageFileName=this.imageFileName;
String static_imageFileType="";
if(this.imageFileType==null||this.imageFileType.trim().equals("")){
static_imageFileType="application/octet-stream";
}
int static_imageFileUsed=1;
String static_fileRealPath="";
String static_isZip="";
String static_isencrypt="";
String static_filesize="";
if(this.data!=null){
static_filesize=""+this.data.length;
}
int static_downloads=0;
String static_miniimgpath="";
String static_imgsize="";
String static_isFTP="";
int static_FTPConfigId=0;
int static_isaesencrypt=0;
String static_aescode="";
String static_tokenKey="";
String static_storageStatus="";
int static_secretlevel=DocManager.DEFAILT_SECRET_LEVEL;
String static_secretvalidity="";
String static_comefrom=this.comefrom;
SystemComInfo syscominfo = new SystemComInfo() ;
String createdir = FileUpload.getCreateDir(syscominfo.getFilesystem()) ;
String random= UUID.randomUUID().toString(); // 解决高并发下,文件重复问题 李迎新
String fileName = random ;
static_isZip = syscominfo.getNeedzip();
static_isaesencrypt = Util.getIntValue(syscominfo.getIsaesencrypt(),0);
static_aescode = Util.getRandomString(13);
if("1".equals(static_isZip)){
fileName += ".zip";
}
String fullName = createdir + fileName ;
static_fileRealPath = fullName;
int fsize = 0;
FileManage.createDir(createdir);
//写压缩文件
OutputStream fileOut = null ;
try{
File file = new File(static_fileRealPath) ;
if(static_isZip.equals("1")) {
ZipOutputStream filezipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file))) ;
filezipOut.setMethod(ZipOutputStream.DEFLATED) ; //设置压缩方法
filezipOut.putNextEntry(new ZipEntry(random)) ;
fileOut = filezipOut ;
} else {
fileOut = new BufferedOutputStream(new FileOutputStream(file)) ;
}
if(static_isaesencrypt==1){
fileOut = AESCoder.encrypt(fileOut, static_aescode);
String sm4 = Util.null2String(getPropValue("weaver_security_type","reversible_enc_type"));
if("sm4".equalsIgnoreCase(sm4)&&static_aescode.startsWith("sm4start")&&static_aescode.endsWith("sm4end")){
SM4Utils sM4Utils=new SM4Utils();
byte[] _key=sM4Utils.getSMCode(static_aescode);
this.data = sM4Utils.encodeBytes(this.data,_key);
}
}
fileOut.write(this.data, 0, this.data.length) ;
}catch(Exception ex){
}finally{
fileOut.close() ;
}
new_imageFileId=imageFileIdUpdate.getImageFileNewId();
static_tokenKey=AliOSSObjectManager.getTokenKeyByFileRealPath(static_fileRealPath);
ConnStatement statement = null;
try{
statement = new ConnStatement();
String sql = "insert into ImageFile(" +
"imageFileId,imageFileName,imageFileType,imageFileUsed,fileRealPath" +
",isZip,isencrypt,filesize,downloads,miniimgpath" +
",imgsize,isFTP,FTPConfigId,isaesencrypt,aescode" +
" ,tokenKey,storageStatus,comefrom" +
",secretlevel,secretvalidity"+
") values(" +
"?,?,?,?,?" +
",?,?,?,?,?" +
",?,?,?,?,?" +
",?,?,?" +
",?,?" +
")";
statement.setStatementSql(sql);
statement.setInt(1, new_imageFileId);
statement.setString(2, static_imageFileName);
statement.setString(3, static_imageFileType);
statement.setInt(4,static_imageFileUsed);
statement.setString(5, static_fileRealPath);
statement.setString(6, static_isZip);
statement.setString(7, static_isencrypt);
statement.setString(8, static_filesize);
statement.setInt(9, static_downloads);
statement.setString(10, static_miniimgpath);
statement.setString(11, static_imgsize);
statement.setString(12, static_isFTP);
statement.setInt(13,static_FTPConfigId);
statement.setInt(14, static_isaesencrypt);
statement.setString(15, static_aescode);
statement.setString(16, static_tokenKey);
statement.setString(17, static_storageStatus);
statement.setString(18, static_comefrom);
statement.setInt(19, static_secretlevel);
statement.setString(20, static_secretvalidity);
statement.executeUpdate();
}catch(Exception ex){
} finally {
try {
statement.close();
} catch (Exception ex) {
}
}
AliOSSObjectManager aliOSSObjectManager=new AliOSSObjectManager();
aliOSSObjectManager.uploadFile(static_fileRealPath,static_imageFileName,static_isZip,""+static_isaesencrypt,static_aescode);
}catch(Exception ex){
}
return new_imageFileId;
}
public int saveImageFileByInputStream(InputStream is,String filename){
int new_imageFileId=0;
if(is==null){
return new_imageFileId;
}
try{
String static_imageFileName=filename;
String static_imageFileType="";
if(this.imageFileType==null||this.imageFileType.trim().equals("")){
static_imageFileType="application/octet-stream";
}
int static_imageFileUsed=1;
String static_fileRealPath="";
String static_isZip="";
String static_isencrypt="";
String static_filesize="";
int static_downloads=0;
String static_miniimgpath="";
String static_imgsize="";
String static_isFTP="";
int static_FTPConfigId=0;
int static_isaesencrypt=0;
String static_aescode="";
String static_tokenKey="";
String static_storageStatus="";
int static_secretlevel=DocManager.DEFAILT_SECRET_LEVEL;
String static_secretvalidity="";
String static_comefrom=this.comefrom;
SystemComInfo syscominfo = new SystemComInfo() ;
String createdir = FileUpload.getCreateDir(syscominfo.getFilesystem()) ;
String random= UUID.randomUUID().toString(); // 解决高并发下,文件重复问题 李迎新
String fileName = random ;
static_isZip = syscominfo.getNeedzip();
static_isaesencrypt = Util.getIntValue(syscominfo.getIsaesencrypt(),0);
static_aescode = Util.getRandomString(13);
if("1".equals(static_isZip)){
fileName += ".zip";
}
String fullName = createdir + fileName ;
static_fileRealPath = fullName;
FileManage.createDir(createdir);
//写压缩文件
OutputStream fileOut = null ;
try{
File file = new File(static_fileRealPath) ;
if(static_isZip.equals("1")) {
ZipOutputStream filezipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file))) ;
filezipOut.setMethod(ZipOutputStream.DEFLATED) ; //设置压缩方法
filezipOut.putNextEntry(new ZipEntry(random)) ;
fileOut = filezipOut ;
} else {
fileOut = new BufferedOutputStream(new FileOutputStream(file)) ;
}
boolean writeInput = true;
if(static_isaesencrypt==1){
fileOut = AESCoder.encrypt(fileOut, static_aescode);
String sm4 = Util.null2String(getPropValue("weaver_security_type","reversible_enc_type"));
if("sm4".equalsIgnoreCase(sm4)&&static_aescode.startsWith("sm4start")&&static_aescode.endsWith("sm4end")){
ByteArrayOutputStream bos = null;
try {
bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = is.read(b)) != -1) {
bos.write(b, 0, n);
}
this.data = bos.toByteArray();
static_filesize = this.data.length + "";
}catch(Exception e){
}finally{
if(bos != null){
try{
bos.close();
}catch(Exception e){}
}
}
SM4Utils sM4Utils=new SM4Utils();
byte[] _key=sM4Utils.getSMCode(static_aescode);
this.data = sM4Utils.encodeBytes(this.data,_key);
writeInput = false;
}
}
if(writeInput){
int n =0;
byte []b = new byte[2048];
long size = 0;
while((n=is.read(b)) != -1){
fileOut.write(b, 0, n) ;
size += n;
}
static_filesize = size+"";
}else{
fileOut.write(this.data,0,this.data.length);
}
}catch(Exception ex){
}finally{
fileOut.close() ;
}
new_imageFileId=imageFileIdUpdate.getImageFileNewId();
static_tokenKey=AliOSSObjectManager.getTokenKeyByFileRealPath(static_fileRealPath);
ConnStatement statement = null;
try{
statement = new ConnStatement();
String sql = "insert into ImageFile(" +
"imageFileId,imageFileName,imageFileType,imageFileUsed,fileRealPath" +
",isZip,isencrypt,filesize,downloads,miniimgpath" +
",imgsize,isFTP,FTPConfigId,isaesencrypt,aescode" +
" ,tokenKey,storageStatus,comefrom" +
",secretlevel,secretvalidity"+
") values(" +
"?,?,?,?,?" +
",?,?,?,?,?" +
",?,?,?,?,?" +
",?,?,?" +
",?,?" +
")";
statement.setStatementSql(sql);
statement.setInt(1, new_imageFileId);
statement.setString(2, static_imageFileName);
statement.setString(3, static_imageFileType);
statement.setInt(4,static_imageFileUsed);
statement.setString(5, static_fileRealPath);
statement.setString(6, static_isZip);
statement.setString(7, static_isencrypt);
statement.setString(8, static_filesize);
statement.setInt(9, static_downloads);
statement.setString(10, static_miniimgpath);
statement.setString(11, static_imgsize);
statement.setString(12, static_isFTP);
statement.setInt(13,static_FTPConfigId);
statement.setInt(14, static_isaesencrypt);
statement.setString(15, static_aescode);
statement.setString(16, static_tokenKey);
statement.setString(17, static_storageStatus);
statement.setString(18, static_comefrom);
statement.setInt(19, static_secretlevel);
statement.setString(20, static_secretvalidity);
statement.executeUpdate();
}catch(Exception ex){
} finally {
try {
statement.close();
} catch (Exception ex) {
}
}
AliOSSObjectManager aliOSSObjectManager=new AliOSSObjectManager();
aliOSSObjectManager.uploadFile(static_fileRealPath,static_imageFileName,static_isZip,""+static_isaesencrypt,static_aescode);
}catch(Exception ex){
}finally{
if(is != null){
try{
is.close();
}catch(Exception e){}
}
}
return new_imageFileId;
}
public void updatesecretlevel(){
RecordSet rs = new RecordSet();
String sql = "update imagefile set secretlevel=?,secretvalidity=? where imagefileid = ?";
rs.executeUpdate(sql,secretlevel,secretvalidity,imageFileId);
}
/**
*
* @param imageFileId_para
*/
public static void ImageFileTrigger(int imageFileId_para) {
if(imageFileId_para<=0){
return ;
}
try{
RecordSet rs = new RecordSet();
rs.executeSql("insert into ImageFileBackUp(imageFileId) values("+imageFileId_para+")");
rs.executeSql("update DocPreview set mustReconverted='1' where imageFileId="+imageFileId_para);
rs.executeSql("update DocPreviewHtml set mustReconverted='1' where imageFileId="+imageFileId_para);
deletePdfImageFile(imageFileId_para);
}catch(Exception ex){
}
}
public static void deletePdfImageFile(int imageFileId) {
if(imageFileId<=0){
return ;
}
try{
RecordSet rs = new RecordSet();
String sql = "select p.pdfimagefileid,i.filerealpath from pdf_imagefile p,imagefile i where p.pdfimagefileid = i.imagefileid and p.imagefileid = " + imageFileId;
rs.executeSql(sql);
if(rs.next())
{
String pdfimagefileid = rs.getString("pdfimagefileid");
String filerealpath = rs.getString("filerealpath");
File file = new File(filerealpath);
new FileDeleteUtil().deleteFile(file);
rs.executeSql("delete from pdf_imagefile where imagefileid="+imageFileId);
rs.executeSql("delete from imagefile where imagefileid="+pdfimagefileid);
}
rs.executeUpdate("delete from convert_imagefile where imagefileid=?",imageFileId);
}catch(Exception ex){
}
}
/**
* ID
*/
public static int getNewImageFileId(){
if(null == imageFileIdUpdate) {
return -1;
}else {
return imageFileIdUpdate.getImageFileNewId();
}
}
}