加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

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

发布时间:2020-12-12 08:53:56 所属栏目:MsSql教程 来源:网络整理
导读:我需要下载存储在数据库中的文件.我想我做了正确的查询并调用它我只是不知道如何将它连接到JSF页面中的按钮. 另外我想知道,在将它传递到JSF页面之前,我是否必须将该图像保存在服务器的文件夹中.如果是这样,我该怎么做? 这是我用来从db返回byte []的查询: @N
我需要下载存储在数据库中的文件.我想我做了正确的查询并调用它我只是不知道如何将它连接到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页面使响应失真.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读