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

📄 importwebhandler.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    String filepath = MVNForumConfig.getTempDir() + File.separatorChar +
                               "import-" +
                               ((importType==MVNForumConfig.IMPORTEXPORT_TYPE_JIVE_XML)?"jive-":"") +
                               importTime.get(Calendar.YEAR) + "-" +
                               importTime.get(Calendar.MONTH) + "-" +
                               importTime.get(Calendar.DAY_OF_MONTH) + "-" +
                               importTime.get(Calendar.HOUR_OF_DAY) + "-" +
                               importTime.get(Calendar.MINUTE) + "-" +
                               importTime.get(Calendar.SECOND) +
                               ((importType==MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP)?".zip":".xml");
                    log.debug("Client import file to save to file system = " + filepath);
                    clientImportFileItem.write(new File(filepath));
                    importFile=new File(filepath);
                    clientImportFileItem.delete();
                    clientImportFileItem=null;
                } catch (Exception ex) {
                    log.error("Cannot save the import file.", ex);
                    String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.cannot_import.cannot_save_import_file");
                    throw new ImportException(localizedMessage);
                    //throw new ImportException("Cannot save the import file to the file system.", ex);
                }
            }

        } finally {
            for (int i = 0; i < fileItems.size(); i++ ) {
                try { ((FileItem)fileItems.get(i)).delete();
                } catch (Exception e) {}
            }
            fileItems.clear(); fileItems=null;
        }

        if (!importFile.exists()) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.cannot_find_import_file");
            throw new ImportException(localizedMessage);
            //throw new ImportException("Can't find import file.");
        }
        else if (!importFile.isFile()) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.import_file_is_not_a_file");
            throw new ImportException(localizedMessage);
            //throw new ImportException("Import \"file\" is actually not a file (probably a directory).");
        }
        else if (!importFile.canRead()) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.dont_have_read_permission");
            throw new ImportException(localizedMessage);
            //throw new ImportException("Don't have a permission to read an import file from server.");
        }

        try {
            switch (importType) {
                case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_XML:
                    ImportMvnForum.importXml(importFile,
                                            request, response,
                                            logonMemberID, logonMemberName,
                                            importTime, importIP,
                                            clearIfError, messageLevel);
                    break;
                case MVNForumConfig.IMPORTEXPORT_TYPE_MVN_ZIP:
                    ImportMvnForum.importZip(importFile,
                                            request, response,
                                            logonMemberID, logonMemberName,
                                            importTime, importIP,
                                            clearIfError, messageLevel);
                    break;
                case MVNForumConfig.IMPORTEXPORT_TYPE_JIVE_XML:
                    ImportJive.importXml(importFile,
                                     request, response,
                                     logonMemberID, logonMemberName,
                                     importTime, importIP,
                                     clearIfError, otherFieldValues, messageLevel);
                    break;

                default:
                    log.error("importXmlZip: invalid importType = " + importType);
                    String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ImportException.invalid_import_type_specified");
                    throw new ImportException(localizedMessage);
                    //throw new AssertionException("Invalid import type specified.");
            }
        } catch (ImportException e) {
           /* In case of this exception, I already printed error message.
            * Now I should NOT allow this exception to propagate higher, since
            * it will then initiate errorpage, which I don't want, and can't
            * allow because already commited output.
            * So, I don't do anything here, just catch it.
            */
        } finally {
            /* Logout current user, even if the import raised an exception,
               because database data are probably changed anyway. */
            onlineUserManager.logout(request, response);
            //this didn't work: ManagerFactory.getOnlineUserFactory().logout(request, response);

            //delete uploaded file, but only if it's not already deleted !
            if (deleteAfterImport && (importFile.exists())) {
                importFile.delete();
            }
            importFile=null;
        }
    }


// =================================================================
// ===== PRINTING STATUS AND ERROR MESSAGES TO THE OUTPUT HTML =====
// =================================================================
    private static boolean oddLine=true;
    private static boolean outputHtmlNotEnded=false;
    private static boolean outputNonImportantMessages=true;
    private static boolean outputImportantMessages=true;
    //outputErrors is always true

    public static void setMessageOutputLevel(int messageLevel) {
        switch (messageLevel) {
            case MVNForumConfig.MESSAGE_LEVEL_ONLY_ERRORS:
                outputImportantMessages=false;
                outputNonImportantMessages=false;
                break;
            case MVNForumConfig.MESSAGE_LEVEL_IMPORTANT_MESSAGES:
                outputImportantMessages=true;
                outputNonImportantMessages=false;
                break;
            default: //MVNForumConfig.MESSAGE_LEVEL_ALL_MESSAGES
                outputNonImportantMessages=true;
                outputImportantMessages=true;
        }
    }


    public static void startHtml(HttpServletRequest request) {
        if (outputHtml!=null) {
            outputHtmlNotEnded=true;
            outputHtml.println("<html>");
            outputHtml.println("<head>");
            outputHtml.println("   <title>mvnForum - Import/Restore Process</title>");
            outputHtml.println("   <meta name=\"copyright\" content=\"This software is copyrighted by MyVietnam.net and released under the terms of the GNU General Public License GPL license. All rights reserved.\">");
            outputHtml.println("   <meta name=\"Keywords\" content=\"mvnForum, server side java, online meetings, collaboration, bulletin board, board, forum, discussion board, jsp, servlet, java, j2se, j2ee, database, jdbc, connection pool, mysql, MyVietnam, mvnForum, framework, web app, web application, servlet container, tomcat, jboss, weblogic, websphere\">");
            outputHtml.println("   <meta name=\"Description\" content=\"mvnForum is a powerful Jsp/Servlet forum (discussion board) - based on Java technology.\">");
            outputHtml.println("   <meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">");
            outputHtml.println("   <meta http-equiv=\"Expires\" content=\"-1\">");
            outputHtml.println("   <link href=\""+request.getContextPath()+"/mvnplugin/mvnforum/css/style.css\" rel=\"stylesheet\" type=\"text/css\">");
            outputHtml.println("</head>");
            outputHtml.println("<body leftmargin=\"0\" topmargin=\"0\">");
            outputHtml.println("<br/>");
            outputHtml.println("");
            outputHtml.println("<table class=\"tborder\" width=\"95%\" cellpadding=\"3\" align=\"center\">");
            outputHtml.println("<tr class=\"theader\">");
            outputHtml.println("   <td align=\"center\">Messages during the process</td>");
            outputHtml.println("</tr>");
            outputHtml.flush();
            oddLine=true;
        }
    }

    public static void endHtml() {
        if (outputHtml!=null) {
            outputHtml.println("</table>");
            outputHtml.println("");
            outputHtml.println("<br/>");
            outputHtml.println("</body>");
            outputHtml.println("</html>");
            outputHtml.flush();
            outputHtmlNotEnded=false;
        }
    }

    public static void addMessage(String message) {
        if (outputNonImportantMessages) {
            outputHtmlTableRow(message, "messageText");
        }
    }

    public static void addErrorMessage(String message) {
        outputHtmlTableRow(message, "messageTextBoldRed");
    }

    /**
     * Adds warning that the process contained some errors, and the database
     * might be inconsistent now. It also tries to clear the database and
     * create default entries, including "admin" member.
     */
    public static void addFinalErrorHandling(HttpServletRequest request, boolean resetAllData) {
        outputHtmlTableRow("&nbsp;", "messageTextBoldRed"); //add one empty row in output message table
        String message = "There was an error during the import. DATA ARE NOW PROBABLY DAMAGED.<br/>";
        if (resetAllData) message+="Trying to clear the database and create \"admin\" member (password will be \"admin\")...<br/>";
        outputHtmlTableRow(message, "messageTextBoldRed");

        String indexUrl = ParamUtil.getContextPath()+
                          UserModuleConfig.getUrlPattern()+
                          "/index";
        String finalMessage = "If you still want to import these data that produced an error, " +
               "please correct above errors first.<br/>" +
               "Then proceed to: &nbsp;&nbsp;&nbsp;"+
               "<a href=\"login\" class=\"menuLink\">Login</a>&nbsp;|&nbsp;" +
               "<a href=\"index\" class=\"menuLink\">Admin Index</a>&nbsp;|&nbsp;" +
               "<a href=\""+indexUrl+"\" class=\"menuLink\">Forum Index</a><br/>" +
               "<span class=\"messageTextBoldRed\">" +
               "Notice you should enter new username and password!" +
               "</span>";
        try {
            if (resetAllData) {
                ImportWebHelper.clearDatabase();
                ImportWebHelper.clearFiles(request.getSession().getServletContext());
                ImportWebHelper.createDefaultContents();
            }
        } catch (IOException e) {
            addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
        } catch (DatabaseException e) {
            addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
        } catch (CreateException e) {
            addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
        } catch (DuplicateKeyException e) {
            addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
        } catch (ObjectNotFoundException e) {
            addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
        } catch (ForeignKeyNotFoundException e) {
            addErrorMessage("Still got an error. We strongly suggest you to create new empty database.<br/>");
        } finally {
            outputHtmlTableRow(finalMessage, "messageTextBoldRed");
        }
    }

    public static void addSuccessMessage() {
        String indexUrl = ParamUtil.getContextPath()+
                          UserModuleConfig.getUrlPattern()+
                          "/index";
        String message = "Successful. Proceed to: &nbsp;&nbsp;&nbsp;"+
                         "<a href=\"login\" class=\"menuLink\">Login</a>&nbsp;|&nbsp;" +
                         "<a href=\"index\" class=\"menuLink\">Admin Index</a>&nbsp;|&nbsp;" +
                         "<a href=\""+indexUrl+"\" class=\"menuLink\">Forum Index</a><br/>" +
                         "<span class=\"messageTextBoldRed\">" +
                         "Notice you should enter new username and password!" +
                         "</span>";
        outputHtmlTableRow(message, "messageTextBold");
    }

    public static void addImportantMessage(String message) {
        if (outputImportantMessages) {
            outputHtmlTableRow(message, "messageTextBoldRed");
        }
    }

    private static void outputHtmlTableRow(String message, String cssClass) {
        if (outputHtml!=null) {
            if (oddLine) {
                outputHtml.println("<tr class=\"trow1\">");
            } else {
                outputHtml.println("<tr class=\"trow2\">");
            }
            outputHtml.println("   <td class=\""+cssClass+"\">"+message+"</td>");
            outputHtml.println("</tr>");
            outputHtml.flush();
            oddLine=!oddLine;
        }
    }

}

⌨️ 快捷键说明

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