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

📄 downloadfilemanager.java

📁 OA典型例子
💻 JAVA
字号:
package com.sure.oa.down;

/**
 * <p>Title: OA</p>
 * <p>Description: 国办项目</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: surec</p>
 * @author mengzy
 * @version 1.0
 */

import com.jspsmart.upload.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import javax.servlet.http.*;
//javax.servlet.jsp.PageContext;
import java.sql.*;
import java.util.Properties;
import java.util.Vector;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Calendar;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.FileInputStream;
import com.sure.businessmodel.UpdateException;
import com.sure.businesslogic.AlreadyExistsException;
import com.sure.businesslogic.NotFoundException;
import com.sure.dataabstraction.DBManager;
import com.sure.dataabstraction.DBPoolException;
import com.sure.dataabstraction.*;
import com.sure.businessmodel.Page;
import com.sure.util.*;
import com.sure.oa.down.*;
import com.sure.businessmodel.Page;
/**
 * status 状态位设定  1:正文 2:附件
 */
public class downLoadFileManager {
    public downLoadFileManager() {}
    public static  void  upload(PageContext pageContext,String docId) throws ServletException,SmartUploadException, IOException, DBPoolException, SQLException,Exception  {
          SmartUpload upload = new SmartUpload();//
          upload.initialize(pageContext);
          try {//非Mulitpart形式的提交会导致一个异常
            upload.upload();
          } catch(NegativeArraySizeException e) {}
          Request req = upload.getRequest();
          String action = req.getParameter("action");//upload或者delete
          String fileId = req.getParameter("fileId");//文件ID
          String unitId = req.getParameter("unitId");//用来构建上传路径目录“单位部门表Unit中的UnitId字段”
          String status = req.getParameter("status");//文件标识
          String tableName=req.getParameter("tableName");//文件所对应的表名(发文表downDocForm、SendDocLog;收文表RecvDocForm、RecvDocLog)
          String subdir = req.getParameter("subdir");
          String editbz = req.getParameter("editbz");
          String docstatus = req.getParameter("docstatus");
          if (fileId == null) { return ;}
          if (action != null && action.equals("delete")) {
             deleteDownLoadFile(fileId,docId,docstatus,status);
            return ;
          }
          downLoadFile bean = new downLoadFile();
          bean.setFileId(fileId);
          bean.setDocId(docId);
          bean.setStatus(status);
          bean.setTableName(tableName);
          com.jspsmart.upload.File file = upload.getFiles().getFile(0);
          if (!file.isMissing()) {
            Calendar cal = new GregorianCalendar();
            cal.setTime(new Date());
            String year = Integer.toString(cal.get(cal.YEAR));
            String month = Integer.toString(cal.get(cal.MONTH));
            String path="/upload/"+unitId+"/"+year+"/"+month+"/";
            String graphpath="\\upload\\"+unitId+"\\"+year+"\\"+month+"\\"+subdir;
            //System.out.println("editbz:"+editbz);
            if (editbz.equals("1")){
                downDocForm sform=downDocFormManager.getDoc(docId);
                subdir=sform.getDocTitle();
                graphpath="\\upload\\"+unitId+"\\"+year+"\\"+month+"\\"+subdir;
                Vector v=getDownLoadFileList(docId);
                if (v.size()>0){
                  downLoadFile att=(downLoadFile)v.firstElement();
                  path=att.getPath();
                }else {
                   initPath(graphpath);
                }
            }else{
              initPath(graphpath);
            }
            bean.setPath(path);
            bean.setTitle(file.getFileName());
            bean.setFileName(file.getFileName());
            String file_etc=(file.getFileName()).substring((file.getFileName()).indexOf(".")+1);
            bean.setMimeType(getMimeType(file.getTypeMIME() + "/" + file.getSubTypeMIME(),file_etc));
            bean.setFileSize(file.getSize());
            saveDownLoadFile(bean);
            file.saveAs(path+subdir+"/"+file.getFileName());
          }
    }

    /**
     * 获得某个文件的详情
     */
    public static  downLoadFile getDownLoadFile(String fileId) throws DBPoolException, SQLException ,NotFoundException{
          Connection conn = DBManager.getConnection();
          String where="Where fileId='"+fileId+"'";
          try {
             return  (downLoadFile)downLoadFilePersistent.load(conn, where).firstElement();
          }
          finally {
             conn.close();
          }
    }

    /**
     *修改:2004-3-4
     */
    public static  Page getList(String docId) throws DBPoolException, SQLException ,NotFoundException{
          Connection conn = DBManager.getConnection();
          String where="Where docId='"+docId+"' order by fileId";
          try {
             return  downLoadFilePersistent.load(conn, 1, 20, where);
          }
          finally {
             conn.close();
          }
    }

    public static  Vector getDownLoadFileList(String docId) throws DBPoolException, SQLException ,NotFoundException{
          Connection conn = DBManager.getConnection();
          String where="Where docId='"+docId+"' order by fileId";
          try {
             return  downLoadFilePersistent.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  downLoadFilePersistent.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  downLoadFilePersistent.load(conn,where);
       } finally {
          conn.close();
       }
   }

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

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

   public static void  deleteDownLoadFile(String fileId,String docId,String docstatus,String status) throws DBPoolException, SQLException ,NotFoundException{
        String where = "Where FileId=" + fileId;
        Connection conn = DBManager.getConnection();
        String initpath=getUploadPath();
        downLoadFile bean;
        File file;
        try{
               bean = downLoadFileManager.getDownLoadFile(fileId);
               file = new File(initpath+StringUtils.replaceAll(bean.getPath(), "\\","/"));
               file.delete();
               downLoadFilePersistent.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=downLoadFileManager.getDownLoadFileList(docId);
            if (beans.size()>0){
              downLoadFile bean=(downLoadFile)beans.firstElement();
              File file = new File(initpath + StringUtils.replaceAll(bean.getPath() ,"\\","/"));
              file.delete();
              downLoadFilePersistent.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 = downLoadFilePersistent.load(cn, where);
          downLoadFile bean = null;
         if(beans.size() > 0) {
             bean = (downLoadFile) beans.firstElement();
             response.setContentType(bean.getMimeType());
             response.setHeader("Content-Disposition","; filename=\"" + bean.getTitle() + "\"");
             FileInputStream inp = new FileInputStream(getUploadPath() + bean.getPath());
            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 + -