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

📄 mertaddmngservlet.java

📁 培训考试系统代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

               //退出FTP
               //?ftp.sendCmd("QUIT");




               //插入记录到kbs db
               //将上传任务写入数据库
               String jobId = df.format(new Date());

               insertJobToKbsDB(response,cityId,jobId,localDir,systemName);

               insertToKbsDb(jobId,cityId,serverId,className,"0","/",systemName,"/" + systemName);

              //将上传条目写入kbs db
              f1 = new File(localDir);
              files = f1.list();

              for(int i=0; i<files.length;i++)
              {
                  File f2 = new File(localDir + File.separatorChar +  files[i]);
                  String localFile = localDir + File.separatorChar +  files[i];
                  String remoteFile = remoteBaseDir + File.separatorChar  + files[i];

                  if(f2.isFile())
                  {//为文件则直接
                      insertToKbsDb(jobId,cityId,serverId,files[i],"1","/" + systemName,
                        className,"/" + systemName + className);
                  }
                  else if(f2.isDirectory())
                  { //为目录
                       insertItemToKbsDB(cityId,jobId,serverId,localFile,remoteFile);
                  }

               }

              //更新任务状态

              updateKbsJobState(response,jobId,1);

           }
          catch (SysDbException e)
          {
                e.printStackTrace();
                response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.COMMONSERVICE_ERROR);
                return;
          }
          catch (java.sql.SQLException e)
          {
                e.printStackTrace();
                response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.DATABASE_ERROR);
                return;
          }
          catch (Exception e)
          {
             if(ErrorCode.MERT_INSERT_KBS.equalsIgnoreCase(e.getMessage()))
             {
                  response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_INSERT_KBS);
                  return;
              }
              e.printStackTrace();

              response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_UPTO_KBS);

              return;
          }


        }


         response.sendRedirect("/agt/mertmng/MertMngUp.jsp?success=1");


           if (connection != null)
                  {

                    connection.disconnect();

                  }



        return;
    }

     /**
      * 上传目录到KBS
      * @param cityId
      * @param serverId
      * @param localDir
      * @param remoteDir
      */
    private void uploadDirToKbs(String cityId,String serverId,String localDir,String remoteDir) throws Exception
    {

        try
        {
          connection.mkdir(remoteDir);

          //处理目录下的文件或目录
          File f1 = new File(localDir);
          String  files[] = f1.list();
          for(int i=0; i<files.length;i++)
          {
            File f2 = new File(localDir + File.separatorChar +  files[i]);
            if(f2.isDirectory())
            {
               uploadDirToKbs(cityId,serverId,localDir + File.separatorChar  + files[i],remoteDir + "/"  + files[i]);
            }
            else
            {
               connection.upload(localDir + File.separatorChar  +files[i],remoteDir + "/" + files[i]);
               //ftp.upload(localDir + File.separatorChar  +files[i], remoteDir + File.separatorChar  + files[i]);
            }
         }
        }
        catch(Exception e)
        {
          throw e;
        }
    }

    /**
     * 将上传任务写入数据库
     * @param response
     * @param cityId
     * @param jobId
     * @param localDir
     * @param remoteDir
     */
    private void insertJobToKbsDB(HttpServletResponse response,
                String cityId,String jobId,String localDir,String remoteDir)
    {
      try
      {
        SysConnPool cspool = null;
        SysDbConn con = null;
        try
        {
            String tmpLocalDir = localDir.replace(File.separatorChar,"/".charAt(0));
            //String localCityId  = (String)request.getSession().getAttribute("CITYID");
            String localCityId = "00";

            cspool = SysConnPool.getInstance();
            con = cspool.getAplComs();
            con.preparedSP();
            con.setString(1, localCityId);
            con.setString(2, cityId);
            con.setString(3, jobId);
            con.setString(4, tmpLocalDir);
            con.setString(5, "/" + remoteDir);
            con.setString(6, "0");
            con.setString(7, "2002-01-01 10:00:00");
            con.setString(8, "2082-01-01 10:00:00");
            con.setInt(9, 0);


            SysRecord res = con.csCommonSP("P_Kbs_UpLoad_Job").getParamSet();
            if (res.getInt(0) != 0)
            {
                 response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_INSERT_KBS);
                 return;
            }
        }
        catch (SysDbException e)
        {
            e.printStackTrace();
            response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.COMMONSERVICE_ERROR);
            return;
        }
        catch (java.sql.SQLException e)
        {
            e.printStackTrace();
            response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.DATABASE_ERROR);
            return;
         }
         catch (Exception e)
         {
            response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_UPTO_KBS);
            return;
         }
         finally
         {
            con.close();
         }

       }
       catch(IOException e)
       {
          e.printStackTrace();
       }

    }

     /**
      * 上传目录信息到KBS
      * @param cityId
      * @param serverId
      * @param localDir
      * @param remoteDir
      */
    private void insertItemToKbsDB(String cityId,String jobId,String serverId,String localDir,String remoteDir) throws Exception
    {

        try
        {

          String itemName = remoteDir.substring(remoteDir.lastIndexOf(File.separatorChar)+1,remoteDir.length());

          //获取父目录的前一级目录,父目录与父名称
          String tmpDir = remoteDir.substring(remoteDir.indexOf(File.separatorChar)+1,remoteDir.length());
          tmpDir = tmpDir.substring(0,tmpDir.lastIndexOf(File.separatorChar));

          String parentDir = File.separatorChar + tmpDir;

          String parentName = tmpDir.substring(tmpDir.lastIndexOf(File.separatorChar)+1,tmpDir.length());

          tmpDir = tmpDir.substring(0,tmpDir.lastIndexOf(File.separatorChar));


          String preParentDir = File.separatorChar + tmpDir;

          //插入kbs 数据库
          insertToKbsDb(jobId,cityId,serverId,itemName,"0",preParentDir,parentName,parentDir);

          //处理目录下的文件或目录
          File f1 = new File(localDir);
          String  files[] = f1.list();
          for(int i=0; i<files.length;i++)
          {
            File f2 = new File(localDir + File.separatorChar +  files[i]);
            if(f2.isDirectory())
            {
               insertItemToKbsDB(cityId,jobId,serverId,localDir + File.separatorChar  + files[i],remoteDir + File.separatorChar  + files[i]);
            }
            else
            {
               insertToKbsDb(jobId,cityId,serverId,files[i],"1",parentDir,itemName,parentDir+ File.separatorChar+itemName);
            }
         }
        }
        catch(Exception e)
        {
          throw e;
        }
    }


    /**
     * 插数据到KBS DB
     * @param cityId
     * @param serverId
     * @param temName
     * @param itemType
     * @param preParentDir
     */
    private void insertToKbsDb(String jobId,String cityId,String serverId,String itemName,String itemType,String preParentDir,
        String parentName, String parentDir) throws Exception
    {
         SysConnPool cspool = null;
         SysDbConn con = null;
         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

         String npreParentDir = preParentDir.replace(File.separatorChar,"/".charAt(0));
         String nparentDir = parentDir.replace(File.separatorChar,"/".charAt(0));

         try
         {
             cspool = SysConnPool.getInstance();
             con = cspool.getAplComs();
             con.preparedSP();
             con.setString(1, cityId);
             con.setString(2, itemType);
             con.setString(3, itemName);
             con.setString(4, parentName);
             con.setString(5, npreParentDir);
             con.setString(6, staffno);
             con.setString(7, df.format(new Date()));
             con.setString(8, "0");
             con.setString(9, "2002-01-01 10:00:00");
             con.setString(10, "2082-01-01 10:00:00");
             con.setInt(11, 0);
             con.setInt(12, 3);
             con.setString(13, jobId);


             SysRecord re = con.csCommonSP("P_KBS_UPLOAD_ITEM").getParamSet();
             if (re.getInt(0) != 0)
             {
                Exception e = new Exception(ErrorCode.MERT_INSERT_KBS);
                throw e;
            }
          }
          catch (Exception e)
          {
              throw e;
          }
          finally
          {
              con.close();
          }
    }

    /**
     * 更新状态
     * @param jobId
     */
    private void updateKbsJobState(HttpServletResponse response,
                String jobId,int status)
    {
      try
      {
        SysConnPool cspool = null;
        SysDbConn con = null;
        try
        {
            cspool = SysConnPool.getInstance();
            con = cspool.getAplComs();
            con.preparedSP();
            con.setString(1,jobId);
            con.setInt(2, status);

            SysRecord res = con.csCommonSP("P_AGT_Update_KBSJobStatue").getParamSet();
            if (res.getInt(0) != 0)
            {
                 response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_INSERT_KBS);
                 return;
            }
        }
        catch (SysDbException e)
        {
            e.printStackTrace();
            response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.COMMONSERVICE_ERROR);
            return;
        }
        catch (java.sql.SQLException e)
        {
            e.printStackTrace();
            response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.DATABASE_ERROR);
            return;
         }
         catch (Exception e)
         {
            response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_UPTO_KBS);
            return;
         }
         finally
         {
            con.close();
         }

       }
       catch(IOException e)
       {
          e.printStackTrace();
       }

    }

 //Clean up resources
    public void destroy()
    {
    }
}

⌨️ 快捷键说明

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