OAF_文件系列4_实现OAF上传显示数据库动态图片Image(案例)

简介: 20150805 Created By BaoXinjian 一、摘要 实现的过程 Step1. 通过数据库中BLOB栏位保存Image类型图片 Step2. 将BLOB类型Item通过messageupload上传至Database VO BLOB中 Step3.

 20150805 Created By BaoXinjian

一、摘要


实现的过程

Step1. 通过数据库中BLOB栏位保存Image类型图片

Step2. 将BLOB类型Item通过messageupload上传至Database VO BLOB中

Step3. 将VO BLOB通过OutStream写到Server文件系统中,并保存文件路径

Step4. 将文件路径设置到Image URL中,动态显示图片,功能可实现

 

二、实现方式


1. 创建Employee Image PG

 

2. 创建CO中初始化事件和文件上传后显示图片的作用

package bxj.oracle.apps.ap.employees.webui;

import bxj.oracle.apps.ap.employees.server.EmpManageAMImpl;
import bxj.oracle.apps.ap.employees.server.EmployeeImageVOImpl;

import java.io.Serializable;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAImageBean;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;

public class EmployeeImageCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

  /**初始化insert
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
    
    EmpManageAMImpl am = (EmpManageAMImpl) pageContext.getApplicationModule(webBean);
    am.invokeMethod("initemployeeimage");
  }

  /**
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processFormRequest(pageContext, webBean);
    
    OAApplicationModule am = pageContext.getApplicationModule(webBean);
    
    if ("upload".equals(pageContext.getParameter(EVENT_PARAM))) {
        am.getTransaction().commit();
        
        System.out.println("processRequest2");
        String employee_id = pageContext.getParameter("EmployeeId");
        //物理路径
        String phyPath = pageContext.getTemporaryImageLocation();
        //相对路径
        String relPath = pageContext.getTemporaryImageSource();
        System.out.println("co phyPath"+phyPath);
        System.out.println("co relPath"+relPath);
          
        /*以下两行注释用于生成唯一图片名,可视需要使用*/
        String imageName = "UserIcon"+employee_id+".gif";
        Serializable[] parameters = { employee_id, phyPath, imageName };
        am.invokeMethod("queryUser", parameters);
        
        OAImageBean image =  (OAImageBean)webBean.findIndexedChildRecursive("EmployeeImage");
        if (image != null) {
          image.setSource(relPath + imageName);
        }
    }
  }

}

 

3. AM中主方法

 //初始化
    public void initemployeeimage() {  
         EmployeeImageVOImpl vo =  this.getEmployeeImageVO();
         //vo.setMaxFetchSize(0);
         vo.setWhereClause(" EMPLOYEE_ID = 2 ");
         vo.executeQuery();
         
         System.out.println("initemployeeimage "+vo.getRowCount());
         //EmployeeImageVORowImpl employeeimagerow = (EmployeeImageVORowImpl) vo.createRow();
         //vo.insertRow(employeeimagerow);
         //employeeimagerow.setNewRowState(Row.STATUS_INITIALIZED);
    }  

    public void queryUser(String user_id, String phyPath, String imageName) {  
        //此处调用了视图的查询方法   
        EmployeeImageVOImpl empvo =  this.getEmployeeImageVO();
        EmployeeImageVORowImpl emprow = (EmployeeImageVORowImpl) empvo.getRowAtRangeIndex(0);
        BlobDomain image = emprow.getBlob();
        if (image != null) {
          initImage(image, phyPath, imageName);  
        }
    }   
        
    //使用IO流输出图片(AM代码):
    private void initImage(BlobDomain image, String phyPath, String imageName) {     
        File directory = new File(phyPath);     
        if (!directory.exists())     
          directory.mkdirs();     
          File imageFile = new File(directory, imageName);     
        try {     
           fromInputToOutput(image.getBinaryStream(), new FileOutputStream(imageFile));     
        } catch (Exception exception) {     
           exception.printStackTrace();     
        }     
        //DEBUG专用,看看路径对不对      
        System.out.println(imageFile.getAbsolutePath());     
    }     
        
    /*复制方法,图片大可以增大byte块*/    
    public void fromInputToOutput(InputStream inputstream, OutputStream outputstream) throws IOException {    
        byte abyte0[] = new byte[255];     
        for (int i = 255; i == 255; ) {     
          i = inputstream.read(abyte0);     
          if (i < 0)     
            break;     
            outputstream.write(abyte0, 0, i);     
        }     
        outputstream.close();     
        inputstream.close();     
    } 

 

三、运行测试


1. 将图片文件上传后,点击upload, 系统会将Image输出到server中,并将路径设置到Image URL

 

2. 文件上传成功, 查看数据库中BLOB类型的问题

 

3. 程式通过outstream将image文件写到server中

 

4. 通过引用server中写入的image,通过image item显示在页面中