将图片写入数据库:
表字段类型为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 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);
//物理路径 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代码):
- 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);
- }
- }
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代码):
- 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();
- }
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代码)
- 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);}
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.此代码没有做安全处理,包括上传文件大小和文件类型
转载于:https://blog.51cto.com/shaofeng/664142