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

📄 mertaddmngservlet.java

📁 培训考试系统代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.huawei.icd30.agt.mertmng;

import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;
import javax.servlet.*;
import javax.servlet.http.*;
import com.huawei.icd30.agt.util.*;
import com.huawei.icd30.agt.util.zip.*;
import com.huawei.icd30.common.db.*;
import com.huawei.icd30.agt.configmng.*;
import java.util.StringTokenizer;
import com.huawei.icd30.common.systemconfig.*;
import net.sf.jftp.net.*;

import java.net.InetAddress;

/**
 * <p> 增加资料操作。</p>
 * <p> </p>
 * <p> </p>
 * @author 龙燕茜
 * @version 1.0
 */

public class MertAddMngServlet extends HttpServlet
{
    private static final String CONTENT_TYPE = "text/html; charset=gb2312";

    private FtpConnection  connection = null;
    //private  FtpDown ftp = null;
    private  String staffno = "";
    //Initialize global variables
    public void init() throws ServletException
    {
    }
    //Process the HTTP Post request
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        request.setCharacterEncoding("gb2312");
        response.setContentType(CONTENT_TYPE);

        response.getOutputStream().println("<html>");
        response.getOutputStream().println("<head>");
        response.getOutputStream().println(new String("<title>培训考试系统 </title>".getBytes("ISO-8859-1"),"gb2312"));
        response.getOutputStream().println("<link href=\"/agt/public/css/gb.css\" rel=\"stylesheet\" type=\"text/css\">");
        response.getOutputStream().println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">");
        response.getOutputStream().println("<script type=\"text/javascript\" language=\"JavaScript1.2\" src=\"/agt/public/js/stm31.js\"></script>");
        response.getOutputStream().println("</head>");
        response.getOutputStream().println("<STYLE type=text/css>BODY {");
        response.getOutputStream().println("	SCROLLBAR-FACE-COLOR: #d6efff; SCROLLBAR-HIGHLIGHT-COLOR: #9E9E9E; SCROLLBAR-SHADOW-COLOR: #9E9E9E; SCROLLBAR-3DLIGHT-COLOR: #ffffff; SCROLLBAR-ARROW-COLOR: #666666; SCROLLBAR-TRACK-COLOR: #d6efff; SCROLLBAR-DARKSHADOW-COLOR: #ffffff}");
        response.getOutputStream().println("</STYLE>");
        response.getOutputStream().println("<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\" bgcolor=\"B7D5F9\">");
        response.getOutputStream().println("<p>&nbsp;</p>");
        response.getOutputStream().println("<p>&nbsp;</p>");

        response.getOutputStream().println(new String("<p><h5 align=\"center\">资料添加操作正在进行中,请稍候。</h5></p>".getBytes("ISO-8859-1"),"gb2312"));

        response.getOutputStream().println("</body>");
        response.getOutputStream().println("</html>");

        MultiRequest mr = new MultiRequest(request) ;

        //获取参数值
        String title = mr.getParameter("title");
        String keyWord = mr.getParameter("keyWord");
        String abstracts = mr.getParameter("abstracts");
        String classType = mr.getParameter("classType");
        String isKbs = mr.getParameter("isKbs");

        String classId = mr.getParameter("classId");
        String authInfo = mr.getParameter("authInfo");
        String staffNoInfo = mr.getParameter("staffNoInfo");
        String isZip = mr.getParameter("isZip");
        String indexFile = mr.getParameter("indexFile");

        String className = ClassInfo.get(classId);

        staffno = (String)request.getSession().getAttribute("staffno");

        //接收文件

        File toFile = null ;
        BufferedOutputStream os = null;
        SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmSSS");
        //dingjiangtao
        SimpleDateFormat dm=new SimpleDateFormat("yyMMdd");

        String fileTemp_1=dm.format(new Date());
        //
        String fileTemp=df.format(new Date());

  // ** end dingjiangtao 2004--6-14
        File filePath = new File(this.getServletContext().getRealPath("agt"));

        String subFilePath = "mertres" + File.separatorChar + className +File.separatorChar +fileTemp_1 + File.separatorChar
                         + fileTemp;

        String fileName = mr.getFilename();
        String fullFile = "";
        String sFileName = "";

        while(fileName!=null && !fileName.trim().equals(""))
        {
            //String suffix = fileName.substring(fileName.lastIndexOf(".") ,fileName.length());
            //String newName = String.valueOf(increase++) + suffix;
            fullFile = subFilePath +  File.separatorChar + fileName;
            sFileName = fileName;
            toFile = new File(filePath , fullFile);

            toFile.getParentFile().mkdirs();
            os = new BufferedOutputStream(
                    new FileOutputStream(toFile));

            mr.upload(os);
            os.close();
            fileName = mr.getFilename() ;
         }

        if("1".equalsIgnoreCase(isZip))
        {//解开压缩包
           ExecuteFile ef = new ExecuteFile();
           String uzFileName = filePath.getPath() + File.separatorChar + fullFile;
           String uzPath = filePath.getPath() + File.separatorChar + subFilePath;


           if(!ef.fileUnZip(uzFileName,uzPath))
           {

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

           fullFile = subFilePath +  File.separatorChar + indexFile;
           File file = new File(uzFileName);
           file.delete();
        }

        SysConnPool cspool = null;
        SysDbConn con = null;

        //顶\uFFFD\uFFFD目录应该可以配置,此处有问题,要求保证目录的层次结构,必须
        //做一个目录,目的是文件按照目录检索的时候

        try
        {
          ///:huangyuyuan add 2004-06-09
          //得出服务器的ip地址和端口
          String remoteHost = "";
          int remotePort = request.getServerPort();
          String remoteAdd = request.getRequestURL().toString();

          remoteAdd = remoteAdd.substring(7);
          int location = remoteAdd.indexOf("/");
          remoteAdd = remoteAdd.substring(0,location);

          if(("127.0.0.1".equals(remoteAdd.trim()))||("localhost".equals(remoteAdd.trim())))
          {
            InetAddress inetAdd = InetAddress.getLocalHost();
            remoteAdd = inetAdd.getHostAddress();
          }
          if(80 == remotePort)
          {
            remoteHost = "http://" + remoteAdd;
          }
          else
          {
            remoteHost = "http://" + remoteAdd + ":" + remotePort;
          }
          ///:~

          //操作数据库
          cspool = SysConnPool.getInstance();
          con = cspool.getAplComs();
          con.preparedSP();
          con.setString(1, title);
          con.setString(2, keyWord);
          con.setString(3, abstracts);
          con.setString(4, classType);
          con.setString(5, File.separatorChar + "agt" + File.separatorChar +fullFile);
          con.setString(6, classId);
          con.setString(7, authInfo);
          con.setString(8, staffNoInfo);

          SysRecord res = con.csCommonSP("P_Agt_MaterUp").getParamSet();
          if (res.getInt(0) != 0)
          {
              response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_UP);
              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(IOException ioe)
       {
           ioe.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.FILE_EORROR);
           return;
       }
       catch(SecurityException se)
       {
           se.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.FILE_SECURITY_EORROR);
           return;
       }
       catch(Exception e)
       {//捕获未知异常,定向到出错页面
           e.printStackTrace();
           response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.UNKNOW_ERROR);
           return;
       }
       finally
       {
          con.close();
       }


      if ("1".equalsIgnoreCase(isKbs))
      {
          try
          {

             String cityId = Initor.systemConfig.get("agt/kbsCityId","00");

             String serverId = "0";
             String systemName = "培训考试资料";
             String remoteBaseDir =  cityId +  "/" + serverId + "/" +
             systemName + "/" + className+"/"+fileTemp_1+"/"+fileTemp;

             String localDir = filePath.getPath() + "\\" + subFilePath;



             //?ftp = new FtpDown(cityId);
             String kbsIp=""; // = Initor.systemConfig.get("agt/kbsIp","100.100.100.100");
             String kbsUser ="";// Initor.systemConfig.get("agt/kbsUser","guest");
             String kbsPasswd = "";//Initor.systemConfig.get("agt/kbsPasswd","0");
             //?if(!(ftp.isLogon(kbsIp, kbsUser,kbsPasswd)))
             String kbsPort="";
              SysDbConn aplcoms = null;
              try
             {
           //得到一个连接的实例
           aplcoms = SysConnPool.getInstance().getAplComs();

           //得到FTP主机的ip地址
           aplcoms.preparedQuery("");
           aplcoms.setString(1,"44");
           SysResultSet rs1 = aplcoms.csCommonQuery("SQL_Agt_GetParam" ,"1" ,"-1").getResultSet();
            rs1.next();
           kbsIp=rs1.getString("paramvalues");




           //得到登陆FTP主机的用户名
           aplcoms.preparedQuery("");
           aplcoms.setString(1,"45");
           SysResultSet rs2 = aplcoms.csCommonQuery("SQL_Agt_GetParam" ,"1" ,"-1").getResultSet();
             rs2.next();


           kbsUser=rs2.getString("paramvalues");

           //得到F登陆TP主机的密码
           aplcoms.preparedQuery("");
           aplcoms.setString(1,"46");
           SysResultSet rs3 = aplcoms.csCommonQuery("SQL_Agt_GetParam" ,"1" ,"-1").getResultSet();

           rs3.next();
           kbsPasswd=rs3.getString("paramvalues");

           //得到F登陆TP主机的端口号
          aplcoms.preparedQuery("");
          aplcoms.setString(1,"47");
          SysResultSet rs4 = aplcoms.csCommonQuery("SQL_Agt_GetParam" ,"1" ,"-1").getResultSet();
         kbsPort=rs2.getString("paramvalues");





       }
       catch (SysDbException aple)
       {//捕获CommonService系统异常,
           //输出异常信息
           aple.printStackTrace();

       }
       catch(java.sql.SQLException sqle)
       {//捕获调用aplcoms异常
           // 输出异常信息
           sqle.printStackTrace();

       }
       catch(Exception e)
       {//捕获未知异常
           //输出异常信息
           e.printStackTrace();

       }
       finally
       {//关闭连接实例
           if(aplcoms != null)
           {
              aplcoms.close();
           }
       }
             connection = new FtpConnection(kbsIp);
             if(!("ok".equalsIgnoreCase(connection.login(kbsUser,kbsPasswd))))
             {
                 response.sendRedirect("/agt/public/jsp/ShowError.jsp?errorId=" + ErrorCode.MERT_LOGIN_KBS);
                 return;
             }

             //路径为 cityId/serverId/培训考试/className/资料文件
             //不管目录是否存在,先建立主路径,需要进行优化
             //? ftp.sendCmd("mkd " + cityId);
             //? ftp.sendCmd("mkd " + cityId + File.separatorChar + systemName);
             //? ftp.sendCmd("mkd " + remoteBaseDir);
             try{
               connection.mkdir(cityId);
               connection.mkdir(cityId + "/" + serverId);
              connection.mkdir(cityId + "/" + serverId + "/" + systemName);
              connection.mkdir(cityId + "/" + serverId + "/" + systemName + "/" +className);
              connection.mkdir(cityId + "/" + serverId + "/" + systemName + "/" +className + "/" + fileTemp_1);
              connection.mkdir(remoteBaseDir);


             }
             catch(Exception e)
             {
               System.out.println(e);
             }


              //上传文件
              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]);
                  String localFile = localDir + File.separatorChar +  files[i];
                  String remoteFile = remoteBaseDir + "/"  + files[i];

                  if(f2.isFile())
                  {//为文件则直接上传

                       connection.upload(localFile,remoteFile);
                       //ftp.upload(localFile, remoteFile);
                  }
                  else if(f2.isDirectory())
                  { //为目录
                       uploadDirToKbs(cityId,serverId,localFile,remoteFile);
                  }

               }

⌨️ 快捷键说明

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