微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用JSF 2.0下载存储在数据库中的文件

我需要下载存储在数据库中的文件.我想我做了正确的查询调用它我只是不知道如何将它连接到JSF页面中的按钮.
另外我想知道,在将它传递到JSF页面之前,我是否必须将该图像保存在服务器的文件夹中.如果是这样,我该怎么做?

这是我用来从db返回byte []的查询

@NamedQuery(name = "downloadGarbage",query = "SELECT g.file FROM Garbage g WHERE g.id :idParam")
@Entity
public class Garbage implements Serializable {
@Lob
@Column(nullable = false)
private byte[] file;
....

这是一个简单的EJB,它调用查询然后获取id:

@Stateless(name = "ejbs/FileDownloaderEJB")
public class FileDownloaderEJB implements IFileDownloaderEJB {

@PersistenceContext
private EntityManager em;

public byte[] downloadGarbage(Long id) {
    Query query = em.createNamedQuery("downloadGarbage");
    query.setParameter("idParam",id);

    Object o = query.getSingleResult();
    byte[] tmpArray = (byte[]) o; 
    return tmpArray;
}

在这是困扰我的部分,我在JSF页面和托管bean上面怎么样?

@ManagedBean
@RequestScoped
public class DownloadController {

@EJB
private FileDownloaderEJB fileDownloaderEJB;

    ...

    private Garbage garbage;

public void startDownload(Long id) {
    fileDownloaderEJB.downloadGarbage(id);
//HOW TO START THE DOWNLOAD?
//Other Get Set Methods...

}

}

另外,我如何从commandButton中的JSF传递那个长id到managedBean?这是允许的吗?

<!-- How to pass the value of id from -->
<h:commandButton action="downloadController.startDownload(#{garbage.id})">

解决方法

仅当将web.xml声明为Servlet 3.0并且servletcontainer也支持它(Glassfish 3,JBoss AS 6,Tomcat 7等)时,才能在EL中传递参数.您的尝试中只有语法错误,这是正确的方法
<h:commandButton action="#{downloadController.startDownload(garbage.id)}" />

你甚至可以传递整个物体,在这种特殊情况下更好.

<h:commandButton action="#{downloadController.startDownload(garbage)}" />

然后,startDownload()方法应该设置响应头,以便webbrowser了解响应主体代表什么内容类型以及如何处理它,最后将内容写入响应主体.你可以在ExternalContext的帮助下完成所有这一切.这是一个启动示例:

public void startDownload(Garbage garbage) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    externalContext.setResponseHeader("Content-Type",garbage.getContentType());
    externalContext.setResponseHeader("Content-Length",garbage.getContent().length);
    externalContext.setResponseHeader("Content-disposition","attachment;filename=\"" + garbage.getFileName() + "\"");
    externalContext.getResponSEOutputStream().write(garbage.getContent());
    facesContext.responseComplete();
}

FacesContext#responseComplete()的最后一行是强制性的,以便JSF理解它不应该导航到某个视图,因此可能会在之后使用另一个JSF页面使响应失真.

原文地址:https://www.jb51.cc/mssql/75027.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐