OAF数据库动态图片的实现

将图片写入数据库:

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

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

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

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

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

  1. //物理路径
  2. String phyPath = pageContext.getTemporaryImageLocation();
  3. //相对路径
  4. String relPath = pageContext.getTemporaryImageSource();
  5. /*以下两行注释用于生成唯一图片名,可视需要使用*/
  6. //String imageName = pageContext.generateUniqueImageName("UserIcon.gif",
  7. //OAWebBeanConstants.OA_TEMPORARY_IMAGE);
  8. String imageName = "UserIcon"+user_id+".gif";
  9. Serializable[] parameters = { user_id, phyPath, imageName };
  10. am.invokeMethod("queryUser", parameters);

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

  1. public void queryUser(String user_id, String phyPath,
  2. String imageName) {
  3. //此处调用了视图的查询方法
  4. getUserVO1().initQuery(new Number(Integer.parseInt(user_id)));
  5. OAViewObject vo = (OAViewObject)getUserVO1();
  6. BlobDomain image = null;
  7. if (vo.hasNext()) {
  8. Row row = vo.next();
  9. image = (BlobDomain)row.getAttribute("Icon");
  10. if (image != null)
  11. initImage(image, phyPath, imageName);
  12. }
  13. }

使用IO流输出图片(AM代码):

  1. private void initImage(BlobDomain image, String phyPath,
  2. String imageName) {
  3. File directory = new File(phyPath);
  4. if (!directory.exists())
  5. directory.mkdirs();
  6. File imageFile = new File(directory, imageName);
  7. try {
  8. fromInputToOutput(image.getBinaryStream(),
  9. new FileOutputStream(imageFile));
  10. } catch (Exception exception) {
  11. exception.printStackTrace();
  12. }
  13. //DEBUG专用,看看路径对不对
  14. System.out.println(imageFile.getAbsolutePath());
  15. }
  16. /*复制方法,图片大可以增大byte块*/
  17. public void fromInputToOutput(InputStream inputstream,
  18. OutputStream outputstream) throws IOException {
  19. byte abyte0[] = new byte[255];
  20. for (int i = 255; i == 255; ) {
  21. i = inputstream.read(abyte0);
  22. if (i < 0)
  23. break;
  24. outputstream.write(abyte0, 0, i);
  25. }
  26. outputstream.close();
  27. inputstream.close();
  28. }

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

  1. OAImageBean image =
  2. (OAImageBean)webBean.findIndexedChildRecursive("Icon");
  3. if (image != null) {
  4. 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.此代码没有做安全处理,包括上传文件大小和文件类型

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/122642/viewspace-1055464/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/122642/viewspace-1055464/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值