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

📄 importwebhandler.java

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

import java.io.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mvnforum.MVNForumConfig;
import com.mvnforum.MVNForumResourceBundle;
import com.mvnforum.admin.importexport.jive.ImportJive;
import com.mvnforum.admin.importexport.mvnforum.ImportMvnForum;
import com.mvnforum.auth.*;
import com.mvnforum.user.UserModuleConfig;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
import net.myvietnam.mvncore.filter.EnableHtmlTagFilter;
import net.myvietnam.mvncore.util.*;
import net.myvietnam.mvncore.web.*;
import net.myvietnam.mvncore.web.fileupload.FileItem;
import net.myvietnam.mvncore.web.fileupload.FileUploadException;
import net.myvietnam.mvncore.web.impl.GenericRequestServletImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Igor Manic
 * @version $Revision: 1.13 $, $Date: 2006/04/14 17:36:29 $
 * <br/>
 * <code>ImportWebHandler</code> class implements methods that process HTTP
 * requests for import. Data could be imported from MVN Forum XML file conforming
 * <a href="http://www.mvnforum.com/mvn.dtd">http://www.mvnforum.com/mvn.dtd</a>,
 * or from MVN Forum backup ZIP file. Data can also be migrated from other sources
 * (for now, it's only available for <code>Jive Forums</code>).
 *
 */
public class ImportWebHandler {

    /** Message log. */
    private static Log log = LogFactory.getLog(ImportWebHandler.class);

    private static PrintWriter outputHtml=null;
    /**
     * All output messages (status, informational and error) will be written
     * to <code>outHtml</code>.
     *
     * @param outHtml <code>PrintWriter</code> to write all output to. If it is
     *                <code>null</code>, there will be no output messages.
     */
    public static void setOutputHtmlWriter(PrintWriter outHtml) {
        ImportWebHandler.outputHtml = outHtml;
    }

    /** Cannot instantiate. */
    private ImportWebHandler() {
    }

    public static Vector getBackupFilesOnServer() {
        Vector result = new Vector();
        File dir = new File(MVNForumConfig.getBackupDir());
        File[] files = dir.listFiles();
        if (files != null) {
            for (int i = 0; i < files.length; i++) {
                File file = files[i];
                if (file.isFile()) {
                    result.add(DisableHtmlTagFilter.filter(file.getName()));
                } //else ignore subdirs
            }
        }
        return result;
    }

    /**
     * This is the main import request dispatcher. It parses request
     * parameters, and decides what is the type of file used and what procedure
     * to call (restore from mvnForum XML, or mvnForum ZIP, or migrate from Jive XML, ...).
     * It also extracts additional request parameters, if they exist. Then it calls
     * the desired procedure and gives it all parameters it extracted from the request.<br/>
     * //todo Igor: add @see tags for all importXml/Zip methods
     *
     * @param request <code>HttpServletRequest</code> object of the request.
     * @param response <code>HttpServletResponse</code> object of the request.
     *
     * @throws ImportException
     * @throws AuthenticationException
     * @throws DatabaseException
     * @throws AssertionException
     *
     */
    public static void importXmlZip(HttpServletRequest request, HttpServletResponse response)
        throws ImportException, AuthenticationException, DatabaseException, AssertionException {

        OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();
        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();
        permission.ensureCanAdminSystem();

        Locale locale = I18nUtil.getLocaleInRequest(request);

        int sizeMax = MVNForumConfig.getMaxImportSize();
        int sizeThreshold = 100000;// max memory used = 100K
        String tempDir = MVNForumConfig.getTempDir();
        List fileItems;
        try {
            GenericRequest genericRequest = new GenericRequestServletImpl(request);
            FileUploadParser uploadParser = FileUploadParserFactory.getFileUploadParser();
            fileItems = uploadParser.parseRequest(genericRequest, sizeMax, sizeThreshold, tempDir, "UTF-8");
        } catch (FileUploadException ex) {
            log.error("Cannot upload", ex);
            String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.cannot_upload", new Object[] {ex.getMessage()});
            throw new ImportException(localizedMessage);
            //throw new ImportException("Cannot upload file. Detailed reason: " + ex.getMessage());
        }

        // values that must get from the form
        String serverImportFilename   = null; //if importing from the server backup directory
        String clientImportFilename   = null; //if uploading import file
        int clientImportFileSize      = 0;    //if uploading import file
        FileItem clientImportFileItem = null; //if uploading import file

        int importType = MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default is MVN Forum XML
        boolean clearIfError = true; //default is to reset database in case of error
        int messageLevel = MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES;
        Vector otherFieldValues=new Vector();

        int      logonMemberID     = onlineUser.getMemberID();
        String   logonMemberName   = onlineUser.getMemberName();
        Calendar importTime        = Calendar.getInstance();
        String   importIP          = request.getRemoteAddr();
        File     importFile        = null;
        boolean  deleteAfterImport = false;

        try {
            for (int i = 0; i < fileItems.size(); i++ ) {
                FileItem currentFileItem = (FileItem)fileItems.get(i);
                //content-type: currentFileItem.getContentType();
                String fieldName = currentFileItem.getFieldName();
                boolean isFormField = currentFileItem.isFormField();

                if ( (fieldName.equals("ServerImportFile")) && (isFormField) ) {
                    serverImportFilename=EnableHtmlTagFilter.filter(currentFileItem.getString());
                    log.debug("serverImportFilename = " + serverImportFilename);
                    currentFileItem.delete(); currentFileItem=null;

                } else if (fieldName.equals("ClientImportFile")) {
                    if (isFormField) {
                        String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.cannot_process_uploaded_import_file_with_a_form_field");
                        throw new ImportException(localizedMessage);
                        //throw new AssertionException("Cannot process uploaded import file with a form field.");
                    }
                    clientImportFileSize = (int)currentFileItem.getSize();
                    String fullFilePath = currentFileItem.getName();
                    clientImportFilename = FileUtil.getFileName(fullFilePath);
                    log.debug("clientImportFilename = " + clientImportFilename);
                    // now save to clientImportFileItem
                    clientImportFileItem = currentFileItem;

                } else if ( (fieldName.equals("ImportType")) && (isFormField) ) {
                    try {
                        importType=Integer.parseInt(currentFileItem.getString());
                    } catch (NumberFormatException e) {
                        importType=MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML; //default
                    }
                    currentFileItem.delete(); currentFileItem=null;
                } else if ( (fieldName.equals("ClearIfError")) && (isFormField) ) {
                    int clearInt=1;
                    try {
                        clearInt=Integer.parseInt(currentFileItem.getString());
                    } catch (NumberFormatException e) {
                        clearInt=1; //default
                    }
                    if (clearInt==0) clearIfError=false;
                    else clearIfError=true;
                    currentFileItem.delete(); currentFileItem=null;
                } else if ( (fieldName.equals("MessageLevel")) && (isFormField) ) {
                    try {
                        messageLevel=Integer.parseInt(currentFileItem.getString());
                    } catch (NumberFormatException e) {
                        messageLevel=MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES; //default
                    }
                    currentFileItem.delete(); currentFileItem=null;

                } else { //other field values
                    if (isFormField) {
                        otherFieldValues.add(new String(fieldName));
                        otherFieldValues.add(currentFileItem.getString());
                    }
                    currentFileItem.delete(); currentFileItem=null;
                }
            } //for (int i = 0; i < fileItems.size(); i++ )

            if ((serverImportFilename!=null) && (!serverImportFilename.equals(""))) {
                //import from server backup repository
                String filepath = MVNForumConfig.getBackupDir() + File.separatorChar + serverImportFilename;
                importFile=new File(filepath);
                deleteAfterImport=false;

            } else {
                //upload client import file
                deleteAfterImport=true; //delete uploaded file
                if ((clientImportFilename==null) || (clientImportFilename.equals(""))) {
                    log.error("Cannot import. Please choose either a file on server, or upload a file.");
                    String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.cannot_import.choose_a_file_on_server_or_upload_a_file");
                    throw new ImportException(localizedMessage);
                    //throw new ImportException("Cannot import. Please choose either a file on server, or upload a file.");
                } else {
                    log.debug("ImportWebHandler : process upload with temp dir = " + MVNForumConfig.getTempDir());
                    if (clientImportFileSize == 0) {
                        String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.cannot_process_import.file_size_is_zero");
                        throw new ImportException(localizedMessage);
                        //throw new ImportException("Cannot process an import file with size = 0. Please check the file size or check if your file is missing.");
                    }
                }

                try {

⌨️ 快捷键说明

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