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

📄 generaladmintaskswebhandler.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }

    public void prepareViewLogSystem(GenericRequest request)
        throws FileNotFoundException, DatabaseException,
        BadInputException, AuthenticationException, AssertionException, IOException {

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

        // just for checking the valid value of "linecount"
        GenericParamUtil.getParameterUnsignedInt(request, "linecount", 25);
        String shortName = GenericParamUtil.getParameterSafe(request, "filename", false);
        String logDir = MVNForumConfig.getLogDir();
        String logFileName = "";

        if (shortName.length() == 0) {
            logFileName = MVNForumConfig.getLogFile();
        } else {
            File logDirFile = new File(logDir);
            if (!logDirFile.exists()) {
                throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath());
            }

            if (!logDirFile.isDirectory()) {
                throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath());
            }
            FileUtil.checkGoodFileName(shortName);
            logFileName = logDir + File.separatorChar + shortName;
        }

        File logFile = new File(logFileName);
        if (!logFile.exists()) {
            throw new FileNotFoundException("Cannot find the log file " + logFile.getAbsolutePath());
        }

        long size = logFile.length();
        String humanSize = FileUtil.getHumanSize(size);

        request.setAttribute("FileName", shortName);
        request.setAttribute("LogDir", logDir);
        request.setAttribute("LogFileName", logFileName);
        request.setAttribute("LogFileSize", String.valueOf(size));
        request.setAttribute("LogFileHumanSize", humanSize);

        if (MVNForumConfig.getEnablePortlet()) {

            if (!logFile.canRead()) {
                throw new IOException("Cannot read the log file: " + logFile.getAbsolutePath());
            }
            int lineCount = GenericParamUtil.getParameterUnsignedInt(request, "linecount", 25); // 25, 50 100 200 400 800
            if (lineCount > 5000) {
                lineCount = 5000;
            }
            String[] contentLog = FileUtil.getLastLines(logFile, lineCount);

            request.setAttribute("ContentLog", contentLog);
        }
    }

    public void prepareLogFrame(GenericRequest request)
        throws DatabaseException, AuthenticationException, IOException,
        FileNotFoundException, AssertionException, BadInputException {

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

        String logFileName = GenericParamUtil.getParameterSafe(request, "filename", false);

        if (logFileName.length() == 0) {
            logFileName = MVNForumConfig.getLogFile();
        } else {
            String logDir = MVNForumConfig.getLogDir();
            File logDirFile = new File(logDir);
            if (!logDirFile.exists()) {
                throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath());
            }

            if (!logDirFile.isDirectory()) {
                throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath());
            }
            FileUtil.checkGoodFileName(logFileName);
            logFileName = logDir + File.separatorChar + logFileName;
        }

        File logFile = new File(logFileName);
        if (!logFile.exists()) {
            throw new FileNotFoundException("Cannot find the log file: " + logFile.getAbsolutePath());
        }
        if (!logFile.canRead()) {
            throw new IOException("Cannot read the log file: " + logFile.getAbsolutePath());
        }

        int lineCount = GenericParamUtil.getParameterUnsignedInt(request, "linecount", 25); // 25, 50 100 200 400 800
        if (lineCount > 5000) {
            lineCount = 5000;
        }
        int offset = 0;
        if (offset < 0) offset = 0;//@todo : redandant code, check it later

        String[] contentLog = FileUtil.getLastLines(logFile, lineCount);

        request.setAttribute("Offset", new Integer(offset));
        request.setAttribute("ContentLog", contentLog);
    }

    public void backupSystemLog(GenericRequest request)
        throws DatabaseException, AuthenticationException,
        IOException, FileNotFoundException, AssertionException {

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

        boolean empty = GenericParamUtil.getParameterBoolean(request, "empty");

        String logFileName = MVNForumConfig.getLogFile();

        String logDir = MVNForumConfig.getLogDir();
        File logDirFile = new File(logDir);
        if (!logDirFile.exists()) {
            throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath());
        }
        if (!logDirFile.isDirectory()) {
            throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath());
        }

        String pattern = "yyyy-MM-dd_HH-mm-ss";
        String newFileName = logDir + File.separator + "mvnforumlog_" + DateUtil.format(new Date(), pattern) + ".log";
        File newFile = new File(newFileName);
        // We don't have to check file here, we check it in FileUtil.copyFile
        FileUtil.copyFile(logFileName, newFileName, false);

        if (empty) {
            FileUtil.emptyFile(logFileName);
        }

        long size = newFile.length();
        String humanSize = FileUtil.getHumanSize(size);

        request.setAttribute("LogFileName", newFile.getAbsolutePath());
        request.setAttribute("LogFileSize", String.valueOf(size));
        request.setAttribute("LogFileHumanSize", humanSize);
    }

    public void prepareListLogFiles(GenericRequest request)
        throws DatabaseException, AuthenticationException,
        IOException, FileNotFoundException, AssertionException {

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

        String logDir = MVNForumConfig.getLogDir();
        File logDirFile = new File(logDir);
        if (!logDirFile.exists()) {
            throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath());
        }
        if (!logDirFile.isDirectory()) {
            throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath());
        }

        File[] files = logDirFile.listFiles();
        File currentLogFile = new File(MVNForumConfig.getLogFile());

        request.setAttribute("LogFiles", files);
        request.setAttribute("CurrentLogFile", currentLogFile);
    }

    public void downloadLogFile(HttpServletRequest request, HttpServletResponse response)
        throws DatabaseException, AuthenticationException, AssertionException, BadInputException, IOException {

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

        Locale locale = I18nUtil.getLocaleInRequest(request);

        // This method require fileName is not empty
        String fileName = ParamUtil.getParameterSafe(request, "filename", true);// must check empty here
        FileUtil.checkGoodFileName(fileName);

        String logDir = MVNForumConfig.getLogDir();
        File logDirFile = new File(logDir);
        if (!logDirFile.exists()) {
            throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath());
        }
        if (!logDirFile.isDirectory()) {
            throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath());
        }

        File file = new File(MVNForumConfig.getLogDir() + File.separatorChar + fileName);
        if ((!file.exists()) || (!file.isFile())) {
            log.error("Can't find a file " + file + " to be downloaded (or maybe it's directory).");
            String localizedMessage = MVNForumResourceBundle.getString(locale, "java.io.IOException.not_exist_or_not_file_to_be_downloaded");
            throw new IOException(localizedMessage + " " + file);
            //throw new IOException("Can't find a file to be downloaded (or maybe it's directory).");
        }

        BufferedOutputStream output = null;
        try {
            response.setContentType("application/octet-stream");
            response.setHeader("Location", fileName);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            int length = (int) file.length();
            if (length > 0) {
                response.setContentLength(length);
            }

            output = new BufferedOutputStream(response.getOutputStream(), 1024 /* buffer size */);
            response.setBufferSize(1024);

            //when we start download, we cannot redirect or raise exceptions
            FileUtil.popFile(file, output);
            output.flush();
        } catch (FileNotFoundException e) {
            log.error("Can't find the such log file on server " + fileName);
        } catch (IOException e) {
            log.error("Error while trying to send backup file from server (" + fileName + ").", e);
        } finally {
            if (output != null) {
                try {
                    output.close();
                } catch (IOException e) { }
            }
        }
    }

    public void deleteLogFile(GenericRequest request)
        throws DatabaseException, AuthenticationException, AssertionException, BadInputException, IOException {

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

        String fileName = GenericParamUtil.getParameterSafe(request, "filename", true);
        FileUtil.checkGoodFileName(fileName);

        String logDir = MVNForumConfig.getLogDir();
        File logDirFile = new File(logDir);
        if (!logDirFile.exists()) {
            throw new FileNotFoundException("Cannot find the log dir: " + logDirFile.getAbsolutePath());
        }
        if (!logDirFile.isDirectory()) {
            throw new IOException("The log dir is not a directory: " + logDirFile.getAbsolutePath());
        }

        fileName = logDir + File.separator + fileName;
        String logFileName = MVNForumConfig.getLogFile();
        File currentLogFile = new File(logFileName);

        if (currentLogFile.equals(new File(fileName))) {
            throw new AssertionException("Cannot delete the current log file: " + fileName);
        }

        FileUtil.deleteFile(fileName);
    }

}

⌨️ 快捷键说明

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