您的位置:首页 > 数据库

OAF数据库动态图片的实现

2010-08-15 16:02 423 查看
将图片写入数据库:

表字段类型为BLOB,在OAF中创建对应的Item,关联到相应VO属性,Item syle 设为MessageFileUpload

提交后就会自动保存到数据库中,使用PL/SQL Developer查看图片是否存储。

读取图片并在网页中显示:

流程:获取临时文件夹物理路径---从VO读取数据---使用IO流输出图片-修改Image Item的Source属性。

获取临时文件夹物理路径(CO代码):

//物理路径
String phyPath = pageContext.getTemporaryImageLocation();
//相对路径
String relPath = pageContext.getTemporaryImageSource();

/*以下两行注释用于生成唯一图片名,可视需要使用*/

//String imageName = pageContext.generateUniqueImageName("UserIcon.gif",
//OAWebBeanConstants.OA_TEMPORARY_IMAGE);
String imageName = "UserIcon"+user_id+".gif";
Serializable[] parameters = { user_id, phyPath, imageName };
am.invokeMethod("queryUser", parameters);
//物理路径
String phyPath = pageContext.getTemporaryImageLocation();
//相对路径
String relPath = pageContext.getTemporaryImageSource();

/*以下两行注释用于生成唯一图片名,可视需要使用*/

//String imageName = pageContext.generateUniqueImageName("UserIcon.gif",
//OAWebBeanConstants.OA_TEMPORARY_IMAGE);
String imageName = "UserIcon"+user_id+".gif";
Serializable[] parameters = { user_id, phyPath, imageName };
am.invokeMethod("queryUser", parameters);


从VO读取数据(AM代码):

public void queryUser(String user_id, String phyPath,
String imageName) {
//此处调用了视图的查询方法
getUserVO1().initQuery(new Number(Integer.parseInt(user_id)));
OAViewObject vo = (OAViewObject)getUserVO1();
BlobDomain image = null;
if (vo.hasNext()) {
Row row = vo.next();
image = (BlobDomain)row.getAttribute("Icon");
if (image != null)
initImage(image, phyPath, imageName);
}
}
public void queryUser(String user_id, String phyPath,
String imageName) {
//此处调用了视图的查询方法
getUserVO1().initQuery(new Number(Integer.parseInt(user_id)));
OAViewObject vo = (OAViewObject)getUserVO1();
BlobDomain image = null;
if (vo.hasNext()) {
Row row = vo.next();
image = (BlobDomain)row.getAttribute("Icon");
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();
}
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();
}


修改Image Item的Source属性(CO代码)

OAImageBean image =
(OAImageBean)webBean.findIndexedChildRecursive("Icon");
if (image != null) {
image.setSource(relPath + imageName);}
OAImageBean image =
(OAImageBean)webBean.findIndexedChildRecursive("Icon");
if (image != null) {
image.setSource(relPath + imageName);}


注解:

1.如果不需要显示图片,而只是提供下载,那么使用MessageDownload会容易得多(无需手动输出)

2.Image Item也可以用代码创建。

3.由于OAF默认的图片文件夹是在OA_MEDIA下,如果使用视图的临时变量作为Image Item的 Image URL引用,图片地址会错误

4.将图片存储于数据库中管理不易,性能也不如直接保存图片URL的好

5.生成的图片不论原格式是JPG.PNG或是GIF,统一为GIF浏览器也可以显示出来

6.此代码没有做安全处理,包括上传文件大小和文件类型

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/vslkyjnew/archive/2009/09/09/4535358.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: