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

📄 companywebhandler.java

📁 解觖java技术中后台无法上传数给的情况
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        } catch (ObjectNotFoundException e) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object[] {new Integer(companyID)});
            throw new ObjectNotFoundException(localizedMessage);
        }

        request.setAttribute("CompanyBean", companyBean);
    }

    public void processUpdateCompanyInfo(GenericRequest request)
        throws BadInputException, DatabaseException, ObjectNotFoundException,
        DuplicateKeyException, AuthenticationException, AssertionException {

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

        Timestamp now = DateUtil.getCurrentGMTTimestamp();

        int    companyID                = GenericParamUtil.getParameterInt(request,"companyid");
        String companyName              = GenericParamUtil.getParameterSafe(request, "CompanyName", true);
        String companyAddress           = GenericParamUtil.getParameterSafe(request, "CompanyAddress", false);
        String companyCity              = GenericParamUtil.getParameterSafe(request, "CompanyCity", false);
        String companyCAP               = GenericParamUtil.getParameterSafe(request, "CompanyCAP", false);
        String companyProvince          = GenericParamUtil.getParameterSafe(request, "CompanyProvince", false);
        String companyRegion            = GenericParamUtil.getParameterSafe(request, "CompanyRegion", false);
        String companyPhone             = GenericParamUtil.getParameterSafe(request, "CompanyPhone", false);
        String companyFax               = GenericParamUtil.getParameterSafe(request, "CompanyFax", false);
        String companyWebsite           = GenericParamUtil.getParameterUrl(request, "CompanyWebsite");
        String companyEmail             = GenericParamUtil.getParameterEmail(request, "CompanyEmail");
        String companySpaceName         = GenericParamUtil.getParameterSafe(request, "CompanySpaceName", true);
        String companySpaceHeader       = GenericParamUtil.getParameterSafe(request,"CompanySpaceHeader", false);
        String companySpaceFooter       = GenericParamUtil.getParameterSafe(request, "CompanySpaceFooter", false);
        String companyVATNumber         = GenericParamUtil.getParameterSafe(request, "CompanyVATNumber", true);

        DAOFactory.getCompanyDAO().updateCompanyInfo(companyID,
                                                     companyName,
                                                     companyAddress,
                                                     companyCity,
                                                     companyCAP,
                                                     companyProvince,
                                                     companyRegion,
                                                     companyPhone,
                                                     companyFax,
                                                     companyWebsite,
                                                     companyEmail,
                                                     companySpaceName,
                                                     companySpaceHeader,
                                                     companySpaceFooter,
                                                     companyVATNumber,
                                                     now/*companyModifiedDate*/);
        CompanyBean companyBean = null;
        try {
            companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
        } catch (ObjectNotFoundException e) {
            Locale locale = I18nUtil.getLocaleInRequest(request);
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object[] {new Integer(companyID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        request.setAttribute("CompanyBean", companyBean);

        // Next, create group for this company
        int groupID      = companyBean.getGroupID();
        String groupName = MVNForumGlobal.COMPANY_GROUP_FREFIX + companyName;
        String groupDesc = "This is the default generated group for company " + companyName;

        DAOFactory.getGroupsDAO().update(groupID, groupName, groupDesc, now/*groupModifiedDate*/);

        // now update the search index
        CompanyIndexer.scheduleUpdateCompanyTask(companyBean);
    }

    public void processChangeLogo(ServletContext context, GenericRequest request)
        throws BadInputException, AuthenticationException, IOException,
        AssertionException, ObjectNotFoundException, DatabaseException {

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

        Locale locale = I18nUtil.getLocaleInRequest(request);

        // primary key column(s)
        int companyID = GenericParamUtil.getParameterInt(request, "companyid");

        int sizeMax = 70000; // 70KB
        int sizeThreshold = 100000;// max memory used = 100K (more than needed)
        List fileItems;
        try {
            FileUploadParser uploadParser = FileUploadParserFactory.getFileUploadParser();
            fileItems = uploadParser.parseRequest(request, sizeMax, sizeThreshold, null, "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 IOException(localizedMessage);
            //throw new IOException("Cannot upload. Detailed reason: " + ex.getMessage());
        }

        // make sure only one file upload
        if (fileItems.size() != 1) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_upload_more_than_one", new Object[] {"logo"});
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Assertion: Cannot upload more than 1 file while processing upload logo file for company.");
        }

        //get the first and only file
        FileItem myFile = (FileItem)fileItems.get(0);
        if (myFile.isFormField() == true) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_uploaded_file_with_a_form_field", new Object[] {"logo"});
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot process uploaded company logo file with a form field.");
        }

        // now everything all right, go ahead and create logo
        InputStream inputStream = myFile.getInputStream();

        StringBuffer bufferPicFile = new StringBuffer(128);
        bufferPicFile.append(request.getRealPath(MVNForumGlobal.UPLOADED_COMPANY_DIR));
        bufferPicFile.append(File.separatorChar).append(companyID);

        String companyDir =  bufferPicFile.toString();
        FileUtil.createDirs(companyDir, true/*ignore if exist*/);

        bufferPicFile.append("/").append("logo.jpg");
        String logoFile =  bufferPicFile.toString();

        log.trace("uploaded file = " + logoFile);

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

        // now the image has been save, go ahead and update database
        StringBuffer bufferVirtualFile = new StringBuffer(128);
        bufferVirtualFile.append(MVNForumGlobal.UPLOADED_COMPANY_DIR);
        bufferVirtualFile.append("/").append(companyID).append("/").append("logo.jpg");
        String virtualFile =  bufferVirtualFile.toString();

        try {
            DAOFactory.getCompanyDAO().updateCompanyLogo(companyID, virtualFile);
        } catch (DatabaseException ex) {// we dont need to catch ObjectNotFoundException since the companyID is already exits
            log.fatal("Assertion in CompanyWebHandler.processChangeLogo");// we dont want it to be here
            // need to delete the file if the above database task failed
            FileUtil.deleteFile(logoFile);
            throw ex;
        }
        CompanyBean companyBean = null;
        try {
            companyBean = DAOFactory.getCompanyDAO().getCompany(companyID);
        } catch (ObjectNotFoundException e) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.ObjectNotFoundException.companyid_not_exists", new Object[] {new Integer(companyID)});
            throw new ObjectNotFoundException(localizedMessage);
        }
        request.setAttribute("CompanyBean", companyBean);
    }

    public void processChangeCss(ServletContext context, GenericRequest request)
        throws BadInputException, AuthenticationException, IOException,
        AssertionException, ObjectNotFoundException, DatabaseException {

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

        Locale locale = I18nUtil.getLocaleInRequest(request);

        // primary key column(s)
        int companyID = GenericParamUtil.getParameterInt(request, "companyid");

        int sizeMax = 20000; // 20KB
        int sizeThreshold = 100000;// max memory used = 100K (more than needed)
        List fileItems;
        try {
            FileUploadParser uploadParser = FileUploadParserFactory.getFileUploadParser();
            fileItems = uploadParser.parseRequest(request, sizeMax, sizeThreshold, null, "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 IOException(localizedMessage);
            //throw new IOException("Cannot upload. Detailed reason: " + ex.getMessage());
        }

        // make sure only one file upload
        if (fileItems.size() != 1) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_upload_more_than_one", new Object[] {"css"});
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Assertion: Cannot upload more than 1 file while processing upload css file for company.");
        }

        //get the first and only file
        FileItem myFile = (FileItem)fileItems.get(0);
        if (myFile.isFormField() == true) {
            String localizedMessage = MVNForumResourceBundle.getString(locale, "mvncore.exception.AssertionException.cannot_process_uploaded_file_with_a_form_field", new Object[] {"css"});
            throw new AssertionException(localizedMessage);
            //throw new AssertionException("Cannot process uploaded company css file with a form field.");
        }

        // now everything all right, go ahead and create logo
        InputStream inputStream = myFile.getInputStream();
        StringBuffer bufferCssFile = new StringBuffer(128);
        bufferCssFile.append(context.getRealPath(MVNForumGlobal.UPLOADED_COMPANY_DIR));
        bufferCssFile.append(File.separatorChar).append(companyID);

        String companyDir =  bufferCssFile.toString();
        FileUtil.createDirs(companyDir, true/*ignore if exist*/);

        bufferCssFile.append("/").append("style.css");
        String cssFile =  bufferCssFile.toString();

        log.trace("uploaded file = " + cssFile);

        //The below method closes the inputStream after it have done its work.
        FileUtil.createTextFile(inputStream, cssFile);// can throw BadInputException

⌨️ 快捷键说明

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