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

📄 binarystorageimpldisk.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/impl/BinaryStorageImplDisk.java,v 1.14 2006/04/14 16:29:57 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.14 $
 * $Date: 2006/04/14 16:29:57 $
 *
 * ====================================================================
 *
 * 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: Trong Vo
 */
package com.mvnforum.impl;

import java.io.*;

import net.myvietnam.mvncore.exception.AssertionException;
import net.myvietnam.mvncore.exception.DatabaseException;
import net.myvietnam.mvncore.exception.ObjectNotFoundException;
import net.myvietnam.mvncore.service.BinaryStorage;
import net.myvietnam.mvncore.service.BinaryStorageHandle;
import net.myvietnam.mvncore.util.FileUtil;
import net.myvietnam.mvncore.util.ImageUtil;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mvnforum.MVNForumConfig;
import com.mvnforum.MVNForumGlobal;
import com.mvnforum.common.AttachmentUtil;
import com.mvnforum.db.DAOFactory;
import com.mvnforum.db.MemberBean;

public class BinaryStorageImplDisk extends BinaryStorageBase implements BinaryStorage {

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

    public BinaryStorageHandle storeData(String category, String nameId, String fileName, InputStream inputStream,
                                         int attachDataFileSize, int attachOption, int attachStatus, String contentType, String storageIP)
        throws IOException {

        if (inputStream == null) {
            throw new IllegalArgumentException("Cannot store an empty inputStream.");
        }

        if (category.equals(CATEGORY_POST_ATTACHMENT)) {
            int attachID = 0;
            try {
                attachID = Integer.parseInt(nameId);
            } catch (NumberFormatException e) {
                log.error("Cannot parse to an int value " + nameId, e);
                throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
            }

            String filename = AttachmentUtil.getAttachFilenameOnDisk(attachID);
            FileOutputStream fos = null;
            try {
                log.debug("Attach filename to save to file system = " + filename);

                fos = new FileOutputStream(filename);
                IOUtils.copy(inputStream, fos);// already do the buffering with block = 4096

                BinaryStorageHandle handle = new BinaryStorageHandle(BinaryStorage.BINARY_STORAGE_TYPE_DISK, 0, filename);
                return handle;
            } catch (IOException e) {
                log.error("Disk Error", e);
                throw e;
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.debug("Cannot close input file", e);
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        log.debug("Cannot close output file", e);
                    }
                }
            }
        } else if (category.equals(CATEGORY_PM_ATTACHMENT)) {
            int attachID = 0;
            try {
                attachID = Integer.parseInt(nameId);
            } catch (NumberFormatException e) {
                log.error("Cannot parse to an int value " + nameId, e);
                throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
            }

            String filename = AttachmentUtil.getPmAttachFilenameOnDisk(attachID);
            FileOutputStream fos = null;
            try {
                log.debug("PmAttach filename to save to file system = " + filename);

                fos = new FileOutputStream(filename);
                IOUtils.copy(inputStream, fos);// already do the buffering with block = 4096

                BinaryStorageHandle handle = new BinaryStorageHandle(BinaryStorage.BINARY_STORAGE_TYPE_DISK, 0, filename);
                return handle;
            } catch (IOException e) {
                log.error("Disk Error", e);
                throw e;
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.debug("Cannot close input file", e);
                }
                if (fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        log.debug("Cannot close output file", e);
                    }
                }
            }
        } else if (category.equals(CATEGORY_AVATAR)) {
            int memberID = 0;
            try {
                memberID = Integer.parseInt(nameId);
            } catch (NumberFormatException e) {
                log.error("Cannot parse to an int value " + nameId, e);
                throw new IllegalArgumentException("Cannot parse to an int value " + nameId);
            }

            FileOutputStream fos = null;
            try {
                MemberBean memberBean = (MemberBean) DAOFactory.getMemberDAO().getMember_forPublic(memberID);
                String memberName = memberBean.getMemberName();

                StringBuffer bufferPicFile = new StringBuffer(128);
                bufferPicFile.append(MVNForumConfig.getAvatarDir());
                log.debug("Upload avatar to the folder " + MVNForumConfig.getAvatarDir());
                bufferPicFile.append(File.separatorChar).append(memberName).append(".jpg");
                String thumbnailFile =  bufferPicFile.toString();
                log.debug("uploaded file = " + thumbnailFile);

//              The below method createThumbnail closes the inputStream after it have done its work.
                ImageUtil.createThumbnail(inputStream, thumbnailFile, 150/*maxWidth*/, 150/*maxHeight*/);// can throw BadInputException

                // NOTE: actually we should call FileUtil.deleteFile(thumbnailFile);
                // if the below method failed, however, left it here is also not serious
                DAOFactory.getMemberDAO().updateAvatar(memberID, MemberBean.MEMBER_AVATAR_USING_UPLOAD);

                BinaryStorageHandle handle = new BinaryStorageHandle(BinaryStorage.BINARY_STORAGE_TYPE_DISK, 0, fileName);
                return handle;
            } catch (ObjectNotFoundException e) {
                log.error("ObjectNotFoundException Error", e);
                throw new IOException(e.getMessage());
            } catch (DatabaseException e) {

⌨️ 快捷键说明

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