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显示在页面中
四、总结注意
1. 如果不需要显示图片,而只是提供下载,那么使用MessageDownload会容易得多(无需手动输出)。
2. Image Item也可以用代码创建。
3. 由于OAF默认的图片文件夹是在OA_MEDIA下,如果使用视图的临时变量作为Image Item的 Image URL引用,图片地址会错误
4. 将图片存储于数据库中管理不易,性能也不如直接保存图片URL的好
5. 生成的图片不论原格式是JPG.PNG或是GIF,统一为GIF浏览器也可以显示出来
6. 此代码没有做安全处理,包括上传文件大小和文件类型
Thanks and Regards
参考: Tavor - http://shaofeng.blog.51cto.com/3392077/664142
技术交流,技术讨论,欢迎加入
Technology Blog Created By Oracle ERP - 鲍新建