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

📄 attachmentmanager.java

📁 OA典型例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
             conn.close();
          }
    }

    public static  Vector getAttachmentList(String docId) throws DBPoolException, SQLException ,NotFoundException{
          Connection conn = DBManager.getConnection();
          String where="Where docId='"+docId+"' order by fileId";
          try {
             return  attachmentPersistent.load(conn,where);
          }
          finally {
             conn.close();
          }
    }

   /**
    *获得某个发文所对应的正文或附件
    */
   public static  Page getListPage(String docId,String status) throws DBPoolException, SQLException ,NotFoundException{
       Connection conn = DBManager.getConnection();
       String where="Where docId='"+docId+"' and status='"+status+"' order by FileId";
       try{
         return  attachmentPersistent.load(conn, 1, 20, where);
       } finally {
          conn.close();
       }
   }

   /**
    *获得某个发文所对应的正文或附件
    */
   public static  Vector getListVector(String docId,String status) throws DBPoolException, SQLException ,NotFoundException{
       Connection conn = DBManager.getConnection();
       String where="Where docId='"+docId+"' and status='"+status+"' order by FileId";
       try{
         return  attachmentPersistent.load(conn,where);
       } finally {
          conn.close();
       }
   }

   public static int   saveAttachment(attachment bean) throws SQLException, DBPoolException {
       Connection conn = DBManager.getConnection();
       int retValue=0;
       try{
          attachmentPersistent p = new attachmentPersistent(bean, (bean.getFileId().intValue()> 0) );
          p.persist(conn);
          retValue=p.getBean().getFileId().intValue();
       }
       finally{
           conn.close();
       }
       return  retValue;
   }

   /**
    * @param 用于发文保存时更新文件连接
    */
   public static void  updateAttachment(String sessionId,int docId,String newpath) throws DBPoolException, SQLException ,NotFoundException{
     Connection conn = DBManager.getConnection();
     String where="where docId='"+sessionId+"'";
     String initpath=getUploadPath();
     try{
        Vector v=attachmentManager.getAttachmentList(sessionId);
        if (v.size()>0){
          attachment bean = (attachment) v.firstElement();
          File file = new File(initpath + bean.getPath() + "/" + bean.getSite());
          File file1 = new File(initpath + bean.getPath() + "/" + newpath);
          //System.out.println("原路径:"+initpath + bean.getPath() + "/" + bean.getSite());
          //System.out.println("现在路径:"+initpath + bean.getPath() + "/" + newpath);
          file.renameTo(file1);
          attachmentPersistent.updateAttachment(conn, docId, where, newpath);
        }
     }
     catch(Exception e){
       e.printStackTrace();
     }
     finally {
        conn.close();
     }
   }

   public static void  deleteAttachment(String fileId,String docId,String docKind,String docstatus,String status) throws DBPoolException, SQLException ,NotFoundException{
        String where = "Where FileId=" + fileId;
        Connection conn = DBManager.getConnection();
        String initpath=getUploadPath();
        attachment bean;
        File file;
        try{
            conn.setAutoCommit(false);
            if ((docKind.equals("0"))&&(docstatus.equals("2"))&&(status.equals("0"))){
               bean=(attachment)attachmentManager.getListVector(docId,"10").firstElement();
               file = new File(initpath+StringUtils.replaceAll(bean.getFilePath(), "\\","/"));
               file.delete();
               bean=(attachment)attachmentManager.getListVector(docId,"0").firstElement();
               file = new File(initpath+StringUtils.replaceAll(bean.getFilePath(), "\\","/"));
               file.delete();
               where="Where docId='"+docId+"' and(status=10 or status=0)";
               attachmentPersistent.delete(conn, where);
               where="Where docId='"+docId+"' and status=11";
               attachmentPersistent.updateAttachmentStatus(conn,where);
               where="Where docId='"+docId+"'";
               SendDocLogPersistent.delete(conn,where);
            }else{
               bean = attachmentManager.getAttachment(fileId);
               file = new File(initpath+StringUtils.replaceAll(bean.getFilePath(), "\\","/"));
               file.delete();
               attachmentPersistent.delete(conn, where);
            }
            conn.commit();
        }catch(Exception e){
            conn.rollback();
            e.printStackTrace();
        } finally {
            conn.setAutoCommit(true);
            conn.close();
        }
   }

   public static void  deleteAll(String docId) throws DBPoolException, SQLException,NotFoundException {
        String where = "Where docId='" + docId + "'";
        Connection conn = DBManager.getConnection();
        String initpath=getUploadPath();
        try{
            Vector  beans=attachmentManager.getAttachmentList(docId);
            if (beans.size()>0){
              attachment bean=(attachment)beans.firstElement();
              File file = new File(initpath + StringUtils.replaceAll(bean.getPath() + "/" + bean.getSite(),"\\","/"));
              file.delete();
              attachmentPersistent.delete(conn, where);
            }
        } finally {
            conn.close();
        }
    }

    /**
     * 在客户端打开附件
     */
    public static void  download(HttpServletResponse response,String fileId,String userId) throws ServletException,IOException,DBPoolException, SQLException{
      ServletOutputStream outw = response.getOutputStream();
      Connection cn = DBManager.getConnection();
      try{
          String where = "where fileId=" + fileId;
          Vector beans = attachmentPersistent.load(cn, where);
          attachment bean = null;
         if(beans.size() > 0) {
             bean = (attachment) beans.firstElement();
             response.setContentType(bean.getMimeType());
             response.setHeader("Content-Disposition","; filename=\"" + bean.getTitle() + "\"");
             FileInputStream inp = new FileInputStream(getUploadPath() + bean.getFilePath());
            int i;

             inp.close();
             outw.flush();
        }
        else {
           outw.println("您所请求的文件不存在。");
        }
      } finally {
           cn.close();
           outw.close();
      }
  }

   /**
    *  Constructor for uploadpath
   */
  public  static  void  initPath(String path){
    String initpath=getUploadPath();
    File file_dir = new File(initpath+path);
    if(!file_dir.exists())
    {
      file_dir.mkdirs();
    }
  }

  /**
   * 获得上传路径UploadPath
   */
  public static  String getUploadPath() {
       String retValue="";
       Properties property = null;
       String configFile = "./conf/oc.conf";
       File file = new File(configFile);
       if (!file.exists()) {
           throw new  RuntimeException(configFile + " file not found");
       }
       try {
           property = new Properties();
           property.load(new BufferedInputStream(new FileInputStream(file)));
           retValue = property.getProperty("UploadPath");
       } catch (IOException ex) {
           System.err.println(ex.getMessage());
       }
       finally {
           return  retValue;
       }
  }

  /**
   * @返回文件的MimeType值
   */
  public static  String getMimeType(String oldType,String subType){
     String retval=oldType;
     if(oldType.equals("application/octet-stream")){
       retval="application/"+subType.toLowerCase();
     }
     return retval;
  }
}

⌨️ 快捷键说明

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