如何使用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页面使响应失真.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...