⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attachmentutil.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
字号:
/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/AttachmentUtil.java,v 1.10 2006/04/14 17:05:26 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.10 $
 * $Date: 2006/04/14 17:05:26 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2006 by MyVietnam.net
 *
 * All copyright notices regarding mvnForum MUST remain 
 * intact in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in 
 * the footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info at MyVietnam net
 *
 * @author: Minh Nguyen  
 * @author: Mai  Nguyen  
 */
package com.mvnforum.common;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import com.mvnforum.MVNForumConfig;
import com.mvnforum.ManagerFactory;
import com.mvnforum.db.AttachmentBean;
import com.mvnforum.db.DAOFactory;
import net.myvietnam.mvncore.exception.DatabaseException;
import net.myvietnam.mvncore.service.BinaryStorage;
import net.myvietnam.mvncore.util.FileUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class AttachmentUtil {

    private static Log log = LogFactory.getLog(AttachmentUtil.class);

    private AttachmentUtil() {
    }

    public static String getAttachFilenameOnDisk(int attachID) {
        String filename = MVNForumConfig.getAttachmentDir() + File.separatorChar + attachID + ".mvn";
        return filename;
    }

    /*
    // remove, now use BinaryStorage
    public static void deleteAttachFilenameOnDisk(int attachID) {
        String filename = getAttachFilenameOnDisk(attachID);
        try {
            log.info("About to delete post attachment = " + filename);
            FileUtil.deleteFile(filename);
        } catch (Exception ex) {
            log.warn("Cannot delete post attachment file " + filename, ex);
            //@todo schedule to delete it later
        }
    }*/

    public static String getPmAttachFilenameOnDisk(int attachID) {
        String filename = MVNForumConfig.getPmAttachmentDir() + File.separatorChar + attachID + ".mvn";
        return filename;
    }

    /*public static void deletePmAttachFilenameOnDisk(int pmAttachID) {
        String filename = getPmAttachFilenameOnDisk(pmAttachID);
        try {
            log.info("About to delete message attachment = " + filename);
            FileUtil.deleteFile(filename);
        } catch (Exception ex) {
            log.warn("Cannot delete message attachment file " + filename, ex);
            //@todo schedule to delete it later
        }
    }*/

    /**
     * NOTE: This method should be called before any attemp to delete a forum
     * because it require the forum is exited
     * After calling this method, go ahead and delete the forum
     *
     * NOTE: For Admin only
     */
    public static void deleteAttachments_inForum(int forumID) throws DatabaseException {

        BinaryStorage binaryStorage = ManagerFactory.getBinaryStorage();

        // First, try to delete attachment in database
        Collection attachmentBeans = DAOFactory.getAttachmentDAO().getAttachments_inForum(forumID);

        log.info("Delete forum = " + forumID + " attachment count = " + attachmentBeans.size());

        //now delete files on disk
        for (Iterator iter = attachmentBeans.iterator(); iter.hasNext(); ) {
            AttachmentBean attachmentBean = (AttachmentBean)iter.next();
            int attachID = attachmentBean.getAttachID();

            //this method already catch the exception
            //AttachmentUtil.deleteAttachFilenameOnDisk(attachID);
            try {
                binaryStorage.deleteData(BinaryStorage.CATEGORY_POST_ATTACHMENT, String.valueOf(attachID), null);
            } catch (IOException ex) {
                log.error("Cannot delete file", ex);
                // actually this exception is never existed
            }

            try {
                DAOFactory.getAttachmentDAO().delete(attachID);
            } catch (Exception ex) {
                log.warn("Cannot delete attachment in database", ex);
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -