📄 fileuploadmanager.java
字号:
/* * Copyright 2006-2007 Queplix Corp. * * Licensed under the Queplix Public License, Version 1.1.1 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.queplix.com/solutions/commercial-open-source/queplix-public-license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */package com.queplix.core.modules.eqlext.utils.images;import com.queplix.core.error.GenericSystemException;import com.queplix.core.modules.config.ejb.EntityViewConfigManagerLocal;import com.queplix.core.modules.config.ejb.EntityViewConfigManagerLocalHome;import com.queplix.core.modules.eql.error.EQLException;import com.queplix.core.modules.eqlext.error.FileManagerException;import com.queplix.core.modules.eqlext.utils.FileManager;import com.queplix.core.modules.jeo.JEObjectHandler;import com.queplix.core.modules.jeo.ejb.JEOManagerLocal;import com.queplix.core.modules.jeo.ejb.JEOManagerLocalHome;import com.queplix.core.modules.jeo.gen.ImageObject;import com.queplix.core.modules.jeo.gen.ImageObjectHandler;import com.queplix.core.modules.jeo.gen.ImageUsageObject;import com.queplix.core.modules.jeo.gen.ImageUsageObjectHandler;import com.queplix.core.modules.jeo.gen.AttachmentObject;import com.queplix.core.modules.jeo.gen.AttachmentObjectHandler;import com.queplix.core.modules.jeo.gen.UploadFolderObject;import com.queplix.core.modules.jeo.gen.UploadFolderObjectHandler;import com.queplix.core.integrator.security.LogonSession;import com.queplix.core.utils.DateHelper;import com.queplix.core.utils.FileHelper;import com.queplix.core.utils.JNDINames;import com.queplix.core.utils.StringHelper;import com.queplix.core.utils.cache.CacheObjectManager;import com.queplix.core.utils.log.AbstractLogger;import org.apache.commons.fileupload.FileItem;import java.io.File;import java.util.ArrayList;import java.util.Date;import java.util.List;/** * Attachment's file and directory helper class. * * @author [ALB] Andrey Baranov * @version $Revision: 1.1.1.1 $ $Date: 2005/09/12 15:30:43 $ * @see com.queplix.core.modules.eqlext.utils.FileManager * @see com.queplix.core.modules.eqlext.www.FileUploadServlet */public final class FileUploadManager extends AbstractLogger { // ========================================================== Variables // User logon session. private LogonSession ls; // Cache local intterfaces of EJB. private CacheObjectManager com = new CacheObjectManager(); // ========================================================== Constrcutor public FileUploadManager( LogonSession ls ) { this.ls = ls; } // ========================================================== Public methods /** * Loads the file by ID (as the <code>FileManager. FileInfo</code> object). * * @param fileID file ID * @return FileManager.FileInfo object * @throws FileManagerException */ public FileManager.FileInfo loadFile( long fileID ) throws FileManagerException { INFO( "Loading file (ID = " + fileID + ")..." ); try { JEObjectHandler hnd = ImageObjectHandler.selectByPkey( getJEOManager(), ls, fileID ); if( hnd == null ) { throw new IllegalStateException( "Can't load file info record with ID = " + fileID ); } ImageObject obj = ( ImageObject ) hnd.getJEObject(); return FileManager.loadFile( FileManager.getLocalName( obj.getFileName(), obj.getFolder() ) ); } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } } /** * Loads the file by ID. * * @param fileID file ID * @return byte array * @throws FileManagerException */ public byte[] loadFileAsByteArray( long fileID ) throws FileManagerException { INFO( "Loading file (ID = " + fileID + ")..." ); try { JEObjectHandler hnd = AttachmentObjectHandler.selectByPkey( getJEOManager(), ls, fileID ); if( hnd == null ) { throw new IllegalStateException( "Can't load file info record with ID = " + fileID ); } AttachmentObject obj = ( AttachmentObject ) hnd.getJEObject(); return obj.getData(); //FileManager.loadFile( FileManager.getLocalName( obj.getFileName(), obj.getFolder() ) ); } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } } /** * Saves the file and creates the related record. * * @param fileInfo FileInfo object * @param subFolder subfolder to store (optional) * @param entityName entity name (optional) * @param recordKey record key for wich we store file (optional) * @return File object * @throws FileManagerException */ public File saveFile( FileManager.FileInfo fileInfo, String subFolder, String entityName, Long recordKey ) throws FileManagerException { File file = null; try { // Do saving. file = FileManager.saveFile( fileInfo, subFolder ); // Add the related database record. addFileRecord( file, entityName, recordKey ); } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } return file; } /* * No javadoc. * @see saveFile( FileManager.FileInfo, .. ) */ public File saveFile( FileItem fileItem, String subFolder, String entityName, Long recordKey ) throws FileManagerException { FileManager.FileInfo fileInfo = new FileManager.FileInfo( fileItem.getName(), fileItem.get() ); return saveFile( fileInfo, subFolder, entityName, recordKey ); } /** * Saves the file * * @param fileInfo FileInfo object * @param subFolder subfolder to store (optional) * @return File object * @throws FileManagerException */ public File saveFile( FileManager.FileInfo fileInfo, String subFolder ) throws FileManagerException { File file = null; // Do saving. file = FileManager.saveFile( fileInfo, subFolder ); return file; } /* * No javadoc. * @see saveFile( FileManager.FileInfo, .. ) */ public File saveFile( FileItem fileItem, String subFolder ) throws FileManagerException { FileManager.FileInfo fileInfo = new FileManager.FileInfo( fileItem.getName(), fileItem.get() ); return saveFile( fileInfo, subFolder ); } /** * Replaces the existing file and updates the related record. * * @param fileID file ID * @param fileItem FileItem object * @return File object of new file * @throws FileManagerException */ public File replaceFile( long fileID, FileItem fileItem ) throws FileManagerException { INFO( "Replacing the file (ID = " + fileID + ")..." ); File file = null; try { // Get the related database record. JEObjectHandler hnd = ImageObjectHandler.selectByPkey( getJEOManager(), ls, fileID ); if( hnd == null ) { throw new IllegalStateException( "Can't load file info record with ID = " + fileID ); } ImageObject obj = ( ImageObject ) hnd.getJEObject(); // Construct FileInfo. FileManager.FileInfo fileInfo = new FileManager.FileInfo( fileItem.getName(), fileItem.get() ); // Do replacing. file = FileManager.replaceFile( FileManager.getLocalName( obj.getFileName(), obj.getFolder() ), fileInfo ); // Update the related database record. updateFileRecord( ( ImageObjectHandler ) hnd, file ); } catch( EQLException ex ) { ERROR( ex ); throw new GenericSystemException( ex ); } return file; } /** * Deletes the existing file and updates the related record. * @param fileID file ID * @throws FileManagerException */ public void deleteFile( long fileID ) throws FileManagerException { INFO( "Deleting the file (ID = " + fileID + ")..." ); try { // Get the related database record. JEObjectHandler hnd = ImageObjectHandler.selectByPkey( getJEOManager(), ls, fileID ); if( hnd == null ) { throw new IllegalStateException( "Can't load file info record with ID = " + fileID ); } ImageObject obj = ( ImageObject ) hnd.getJEObject(); // Do deleting.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -