如何在 xpage 中显示 base 64 数组?
How to show a array of base 64 in a xpage?
我需要在屏幕上显示一个文件,该文件由 Web 服务返回,格式为 base 64 数组。理想的是在新浏览器中显示它 window。我构建了一个 java 代理来使用 Web 服务。
我认为输出将是一个 xagent,但我从未实施过,我不知道是否是这样,以及我将如何做到这一点。 xpage 上的按钮会调用将执行 xagent 的 xpage 吗?您将如何使用 Web 服务?调用xagent xpage代理? Impior xagent 的 java 代理代码?我整天都在研究如何做这个有趣的事情,但到目前为止我没有太大的成功。
非常感谢,
马库斯
以下 XSnippet(XAgent 的)让您对要做什么有一些了解。
https://openntf.org/XSnippets.nsf/snippet.xsp?id=download-all-attachments
在上面的 XSnippet 中,Document 中的所有附件都被压缩,然后作为 zip 文件发送到浏览器。
url 有一些参数,例如XAgent 使用它来确定要压缩哪个文档的 documentID。
XAgent 获取 HttpServletResponse 的句柄,并对其进行配置,以便指定发送 'application/zip' 文件而不是发回 XPage。
然后它使用 documentId 找到文档并将其压缩,将内容写入响应,然后告诉 facesContext 响应已完成(不再进行渲染)。
在您的情况下,您将放置一个参数来标识您要下载的文件。您可以使用 url 例如 link 到此 XAgent
Download.xsp?fileId=somefileid
然后您的 XAgent 将设置类似于上面的响应,但内容类型可能不是 'application/zip'。如果您不知道文件类型,您可以使用 'application/octet-sream',但如果您知道它是某个文件的 pdf,您可以使用适当的 Mime Type
使用您为访问网络服务而编写的任何代码检索文件,对其进行解码,然后将其写入响应的输出流
在 Java 中作为托管 bean 实施的示例
以下示例输出一些原本位于 base64 字节数组中的纯文本。它被解码然后写入响应。
您要做的就是将内容类型更改为 'application/octet-stream'
在 Java 设计元素中创建托管 bean。
package com.example;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import com.sun.faces.util.Base64;
public class DownloadBean implements Serializable {
private static final long serialVersionUID = 1L;
public DownloadBean() {
};
public void downloadFile() throws IOException {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext context = fc.getExternalContext();
String myFileName = "SomeFile.txt";
HttpServletResponse resp = (HttpServletResponse) context.getResponse();
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", -1);
// This example is just plain text but you would
// change this to 'application/octet-stream'
resp.setContentType("text/plain");
// Tell the browser it is an attachment with filename of <myFilename>
resp.setHeader("Content-Disposition", "attachment; filename="
+ myFileName);
OutputStream os = resp.getOutputStream();
// Somehow you need to get your byte[],
// that is up to you how you do that
// This example has just used base64 encoding of 'Hello Marcus'
byte[] base64bytes = "SGVsbG8gTWFyY3Vz".getBytes();
// Option 1 : use sun.misc.BASE64Decoder to decode with Streams
ByteArrayInputStream bais = new ByteArrayInputStream(base64bytes);
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
dec.decodeBuffer(bais, os);
// Option 2 : use com.sun.faces.util.Base64 to decode to normal byte[]
//byte[] normalBytes = Base64.decode(base64bytes);
//os.write(normalBytes);
os.flush();
os.close();
fc.responseComplete();
}
}
注册到faces-config.xml。请求范围应该足够了。因此,例如,如果您将其命名为 'downloadBean',并且它属于 class com.example.DownloadBean,请将此条目放入 faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>downloadBean</managed-bean-name>
<managed-bean-class>com.example.DownloadBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
使用方法
然后您可以通过按钮调用它:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:button value="Download" id="buttonDownload">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete" action="#{downloadBean.downloadFile}">
</xp:eventHandler>
</xp:button>
</xp:view>
或者您可以创建一个像 XAgent 这样的 XPage,它只下载文件,然后 link 到新 window 中的这个 Xpage。
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
rendered="false" beforePageLoad="#{downloadBean.downloadFile}">
</xp:view>
如果需要,您可以使用类似的方式访问 url 参数
https://openntf.org/XSnippets.nsf/snippet.xsp?id=get-url-parameter-using-java
我需要在屏幕上显示一个文件,该文件由 Web 服务返回,格式为 base 64 数组。理想的是在新浏览器中显示它 window。我构建了一个 java 代理来使用 Web 服务。
我认为输出将是一个 xagent,但我从未实施过,我不知道是否是这样,以及我将如何做到这一点。 xpage 上的按钮会调用将执行 xagent 的 xpage 吗?您将如何使用 Web 服务?调用xagent xpage代理? Impior xagent 的 java 代理代码?我整天都在研究如何做这个有趣的事情,但到目前为止我没有太大的成功。
非常感谢, 马库斯
以下 XSnippet(XAgent 的)让您对要做什么有一些了解。 https://openntf.org/XSnippets.nsf/snippet.xsp?id=download-all-attachments
在上面的 XSnippet 中,Document 中的所有附件都被压缩,然后作为 zip 文件发送到浏览器。 url 有一些参数,例如XAgent 使用它来确定要压缩哪个文档的 documentID。 XAgent 获取 HttpServletResponse 的句柄,并对其进行配置,以便指定发送 'application/zip' 文件而不是发回 XPage。
然后它使用 documentId 找到文档并将其压缩,将内容写入响应,然后告诉 facesContext 响应已完成(不再进行渲染)。
在您的情况下,您将放置一个参数来标识您要下载的文件。您可以使用 url 例如 link 到此 XAgent
Download.xsp?fileId=somefileid
然后您的 XAgent 将设置类似于上面的响应,但内容类型可能不是 'application/zip'。如果您不知道文件类型,您可以使用 'application/octet-sream',但如果您知道它是某个文件的 pdf,您可以使用适当的 Mime Type
使用您为访问网络服务而编写的任何代码检索文件,对其进行解码,然后将其写入响应的输出流
在 Java 中作为托管 bean 实施的示例
以下示例输出一些原本位于 base64 字节数组中的纯文本。它被解码然后写入响应。 您要做的就是将内容类型更改为 'application/octet-stream'
在 Java 设计元素中创建托管 bean。
package com.example;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import com.sun.faces.util.Base64;
public class DownloadBean implements Serializable {
private static final long serialVersionUID = 1L;
public DownloadBean() {
};
public void downloadFile() throws IOException {
FacesContext fc = FacesContext.getCurrentInstance();
ExternalContext context = fc.getExternalContext();
String myFileName = "SomeFile.txt";
HttpServletResponse resp = (HttpServletResponse) context.getResponse();
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", -1);
// This example is just plain text but you would
// change this to 'application/octet-stream'
resp.setContentType("text/plain");
// Tell the browser it is an attachment with filename of <myFilename>
resp.setHeader("Content-Disposition", "attachment; filename="
+ myFileName);
OutputStream os = resp.getOutputStream();
// Somehow you need to get your byte[],
// that is up to you how you do that
// This example has just used base64 encoding of 'Hello Marcus'
byte[] base64bytes = "SGVsbG8gTWFyY3Vz".getBytes();
// Option 1 : use sun.misc.BASE64Decoder to decode with Streams
ByteArrayInputStream bais = new ByteArrayInputStream(base64bytes);
sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
dec.decodeBuffer(bais, os);
// Option 2 : use com.sun.faces.util.Base64 to decode to normal byte[]
//byte[] normalBytes = Base64.decode(base64bytes);
//os.write(normalBytes);
os.flush();
os.close();
fc.responseComplete();
}
}
注册到faces-config.xml。请求范围应该足够了。因此,例如,如果您将其命名为 'downloadBean',并且它属于 class com.example.DownloadBean,请将此条目放入 faces-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config>
<managed-bean>
<managed-bean-name>downloadBean</managed-bean-name>
<managed-bean-class>com.example.DownloadBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
使用方法
然后您可以通过按钮调用它:
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
<xp:button value="Download" id="buttonDownload">
<xp:eventHandler event="onclick" submit="true"
refreshMode="complete" action="#{downloadBean.downloadFile}">
</xp:eventHandler>
</xp:button>
</xp:view>
或者您可以创建一个像 XAgent 这样的 XPage,它只下载文件,然后 link 到新 window 中的这个 Xpage。
<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core"
rendered="false" beforePageLoad="#{downloadBean.downloadFile}">
</xp:view>
如果需要,您可以使用类似的方式访问 url 参数 https://openntf.org/XSnippets.nsf/snippet.xsp?id=get-url-parameter-using-java