将图片写入数据库:

表字段类型为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. /*以下两行注释用于生成唯一图片名,可视需要使用*/    
  7.     
  8.             //String p_w_picpathName = pageContext.generateUniqueImageName("UserIcon.gif",       
  9.             //OAWebBeanConstants.OA_TEMPORARY_IMAGE);      
  10.             String p_w_picpathName = "UserIcon"+user_id+".gif";     
  11.             Serializable[] parameters = { user_id, phyPath, p_w_picpathName };     
  12.             am.invokeMethod("queryUser", parameters);    
  13. //物理路径               
  14. String phyPath = pageContext.getTemporaryImageLocation();  
  15. //相对路径   
  16.             String relPath = pageContext.getTemporaryImageSource();  
  17.   
  18. /*以下两行注释用于生成唯一图片名,可视需要使用*/  
  19.   
  20.             //String p_w_picpathName = pageContext.generateUniqueImageName("UserIcon.gif",    
  21.             //OAWebBeanConstants.OA_TEMPORARY_IMAGE);   
  22.             String p_w_picpathName = "UserIcon"+user_id+".gif";  
  23.             Serializable[] parameters = { user_id, phyPath, p_w_picpathName };  
  24.             am.invokeMethod("queryUser", parameters);   

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

 

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

 

  1. public void queryUser(String user_id, String phyPath,      
  2.                          String p_w_picpathName) {     
  3. //此处调用了视图的查询方法      
  4.         getUserVO1().initQuery(new Number(Integer.parseInt(user_id)));     
  5.         OAViewObject vo = (OAViewObject)getUserVO1();     
  6.         BlobDomain p_w_picpath = null;     
  7.         if (vo.hasNext()) {     
  8.             Row row = vo.next();     
  9.             p_w_picpath = (BlobDomain)row.getAttribute("Icon");     
  10.             if (p_w_picpath != null)      
  11.                 initImage(p_w_picpath, phyPath, p_w_picpathName);     
  12.           }      
  13.     }    
  14.     public void queryUser(String user_id, String phyPath,   
  15.                          String p_w_picpathName) {  
  16. //此处调用了视图的查询方法   
  17.         getUserVO1().initQuery(new Number(Integer.parseInt(user_id)));  
  18.         OAViewObject vo = (OAViewObject)getUserVO1();  
  19.         BlobDomain p_w_picpath = null;  
  20.         if (vo.hasNext()) {  
  21.             Row row = vo.next();  
  22.             p_w_picpath = (BlobDomain)row.getAttribute("Icon");  
  23.             if (p_w_picpath != null)   
  24.                 initImage(p_w_picpath, phyPath, p_w_picpathName);  
  25.           }   
  26.     }   

public void queryUser(String user_id, String phyPath, String p_w_picpathName) { //此处调用了视图的查询方法 getUserVO1().initQuery(new Number(Integer.parseInt(user_id))); OAViewObject vo = (OAViewObject)getUserVO1(); BlobDomain p_w_picpath = null; if (vo.hasNext()) { Row row = vo.next(); p_w_picpath = (BlobDomain)row.getAttribute("Icon"); if (p_w_picpath != null) initImage(p_w_picpath, phyPath, p_w_picpathName); } } public void queryUser(String user_id, String phyPath, String p_w_picpathName) { //此处调用了视图的查询方法 getUserVO1().initQuery(new Number(Integer.parseInt(user_id))); OAViewObject vo = (OAViewObject)getUserVO1(); BlobDomain p_w_picpath = null; if (vo.hasNext()) { Row row = vo.next(); p_w_picpath = (BlobDomain)row.getAttribute("Icon"); if (p_w_picpath != null) initImage(p_w_picpath, phyPath, p_w_picpathName); } }

 

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

 

  1. private void initImage(BlobDomain p_w_picpath, String phyPath,      
  2.                            String p_w_picpathName) {     
  3.         File directory = new File(phyPath);     
  4.         if (!directory.exists())     
  5.             directory.mkdirs();     
  6.         File p_w_picpathFile = new File(directory, p_w_picpathName);     
  7.         try {     
  8.             fromInputToOutput(p_w_picpath.getBinaryStream(),      
  9.                               new FileOutputStream(p_w_picpathFile));     
  10.         } catch (Exception exception) {     
  11.             exception.printStackTrace();     
  12.         }     
  13. //DEBUG专用,看看路径对不对      
  14.         System.out.println(p_w_picpathFile.getAbsolutePath());     
  15.     }     
  16.     
  17. /*复制方法,图片大可以增大byte块*/    
  18.     public void fromInputToOutput(InputStream inputstream,      
  19.                                   OutputStream outputstream) throws IOException {     
  20.         byte abyte0[] = new byte[255];     
  21.         for (int i = 255; i == 255; ) {     
  22.             i = inputstream.read(abyte0);     
  23.             if (i < 0)     
  24.                 break;     
  25.             outputstream.write(abyte0, 0, i);     
  26.         }     
  27.         outputstream.close();     
  28.         inputstream.close();     
  29.     }    
  30.     private void initImage(BlobDomain p_w_picpath, String phyPath,   
  31.                            String p_w_picpathName) {  
  32.         File directory = new File(phyPath);  
  33.         if (!directory.exists())  
  34.             directory.mkdirs();  
  35.         File p_w_picpathFile = new File(directory, p_w_picpathName);  
  36.         try {  
  37.             fromInputToOutput(p_w_picpath.getBinaryStream(),   
  38.                               new FileOutputStream(p_w_picpathFile));  
  39.         } catch (Exception exception) {  
  40.             exception.printStackTrace();  
  41.         }  
  42. //DEBUG专用,看看路径对不对   
  43.         System.out.println(p_w_picpathFile.getAbsolutePath());  
  44.     }  
  45.   
  46. /*复制方法,图片大可以增大byte块*/  
  47.     public void fromInputToOutput(InputStream inputstream,   
  48.                                   OutputStream outputstream) throws IOException {  
  49.         byte abyte0[] = new byte[255];  
  50.         for (int i = 255; i == 255; ) {  
  51.             i = inputstream.read(abyte0);  
  52.             if (i < 0)  
  53.                 break;  
  54.             outputstream.write(abyte0, 0, i);  
  55.         }  
  56.         outputstream.close();  
  57.         inputstream.close();  
  58.     }   

private void initImage(BlobDomain p_w_picpath, String phyPath, String p_w_picpathName) { File directory = new File(phyPath); if (!directory.exists()) directory.mkdirs(); File p_w_picpathFile = new File(directory, p_w_picpathName); try { fromInputToOutput(p_w_picpath.getBinaryStream(), new FileOutputStream(p_w_picpathFile)); } catch (Exception exception) { exception.printStackTrace(); } //DEBUG专用,看看路径对不对 System.out.println(p_w_picpathFile.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 p_w_picpath, String phyPath, String p_w_picpathName) { File directory = new File(phyPath); if (!directory.exists()) directory.mkdirs(); File p_w_picpathFile = new File(directory, p_w_picpathName); try { fromInputToOutput(p_w_picpath.getBinaryStream(), new FileOutputStream(p_w_picpathFile)); } catch (Exception exception) { exception.printStackTrace(); } //DEBUG专用,看看路径对不对 System.out.println(p_w_picpathFile.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代码)

 

  1. OAImageBean p_w_picpath =      
  2.     (OAImageBean)webBean.findIndexedChildRecursive("Icon");     
  3. if (p_w_picpath != null) {      
  4.     p_w_picpath.setSource(relPath + p_w_picpathName);}    
  5.                 OAImageBean p_w_picpath =   
  6.                     (OAImageBean)webBean.findIndexedChildRecursive("Icon");  
  7.                 if (p_w_picpath != null) {   
  8.                     p_w_picpath.setSource(relPath + p_w_picpathName);}   

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

 

注解:

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

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

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

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

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

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