OAF_文件系列1_实现OAF文件上传和下载MessageFileUpload/MessageDownload(案例)

20150707 Created By BaoXinjian

一、摘要


1. 文件上传

首先建立用于存放附件的临时视图对象(View Object),包含Blob类型的视图属性(View Attribute),建立页面包含messageFileUpload类型的Item,并创建页面的控制器。

 

2. 文件下载

这里要实现的功能是当单击附件链接时,能自动打开附件保存对话框.

 

二、文件上传


1. 创建messageFileUpload空间,其类型为BLOB

 

2. 创建CO实现方法

作用1. 数据保存至VO Blob中

作用2. 将VO中Blob数据保存至server上

复制代码
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    
      EmpManageAMImpl empAM = (EmpManageAMImpl) pageContext.getApplicationModule(webBean);

      if ("upload".equals(pageContext.getParameter(EVENT_PARAM))) {
          try {
            EmployeeFilesVOImpl empfilevo = empAM.getEmployeeFilesVO();
            EmployeeFilesVORowImpl empfilerow = (EmployeeFilesVORowImpl) empfilevo.getCurrentRow();
            BlobDomain blob = (BlobDomain) empfilerow.getEmployeeFile();
            InputStream instream = blob.getBinaryStream();
            
            File directory = new File("/u2/VIS/visappl/gavin/files"); 
            if(!directory.exists()) {
              directory.mkdirs();  
            }
            String filename = empfilerow.getFileId().toString()+".txt";
            File file = new File (directory + filename);
            FileOutputStream outstream = new FileOutputStream(file);
            
            int size = blob.getBufferSize();
            byte[] buffer = new byte[size];
            int length = 0;
            while ((length = instream.read(buffer)) != -1) {
              outstream.write(buffer, 0, length);
            }
            
            instream.close();
            outstream.flush();
            outstream.close();
            
            empAM.getOADBTransaction().commit();
          }catch(IOException ex) {
            empAM.getOADBTransaction().rollback();
            ex.printStackTrace();
          } 
      }
    
  }
复制代码

 

三、文件下载


1. 创建一个MessageDown类型的空间,其会将附件显示在页面之上

 

2. 创建CO方法,将数据中的Blob数据通过或response保存至本地

复制代码
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    
      EmpManageAMImpl empAM = (EmpManageAMImpl) pageContext.getApplicationModule(webBean);
  
      if ("download".equals(pageContext.getParameter(EVENT_PARAM))) {
          try {
            EmployeeFilesVOImpl empfilevo = empAM.getEmployeeFilesVO();
            EmployeeFilesVORowImpl empfilerow = (EmployeeFilesVORowImpl) empfilevo.getCurrentRow();
            BlobDomain blob = (BlobDomain) empfilerow.getEmployeeFile();
            InputStream instream = blob.getBinaryStream();
            
            HttpServletResponse response = (HttpServletResponse) pageContext.getRenderingContext().getServletResponse();
            response.addHeader("Content-Disposition", "attachment;filename="+ "gavin.txt");
            response.setContentType("application/x-msdownload");

            int size = blob.getBufferSize();
            byte[] buffer = new byte[size];
            int length = 0;

            while ((length = instream.read(buffer)) != -1) {
              response.getOutputStream().write(buffer,0,length);
            }
            instream.close();
            response.getOutputStream().flush();
            response.getOutputStream().close();

            empAM.getOADBTransaction().commit();
          }catch(IOException ex) {
            empAM.getOADBTransaction().rollback();
            ex.printStackTrace();
          } 
      }
  }
复制代码

 

四、测试上传下载文件


Test1. 文件上传测试

Step1. 将employee.txt上传至服务器上

Step2. 查看上传后,文本文件会分别保存至数据库和服务器上

Step3.1  上传至服务器上

Step3.2  上传至数据库中

Step4. 上传后显示在页面中

 

Test2. 文件下载测试

Step1. 文件下载

Step2. 文件下载后保存至数据库中

 

Thanks and Regards

posted on 2015-05-01 20:09  东方瀚海  阅读(1064)  评论(0编辑  收藏  举报