繁体   English   中英

如何在ADF中创建下载链接?

[英]How to create download link in ADF?

我正在使用jdev 11.1.1.5.0. 在我的用例中,我想创建一个下载链接。 当用户单击链接时,文件应自动下载(例如下载servlet)。

代码如下:

HttpServletResponse response= (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();                                                         response.setContentType("text/plain");                                                          response.setHeader("Content-Disposition","attachment;filename="+part.getFileName());     response.setContentType("text/plain");                                                  response.setHeader("ContentDisposition","attachment;filename="+part.getFileName());        InputStreaminput=part.getInputStream();                                                     int read=0;        
  byte[] bytes = new byte[1024];                                                    OutputStream os =response.getOutputStream();    


while((read=input.read(bytes))!=-1)
{os.write(bytes, 0, read);
}
os.flush();                                                   
os.close(); 

但这是行不通的。 我的要求是要创建动态链接(URL),并且当用户单击链接时,将下载文件。 还有其他方法吗? 谢谢。

创建一个commandLink并为其提供文件下载操作侦听器 ,并对该侦听进行编码

    <af:commandButton text="Say Hello">
      <af:fileDownloadActionListener filename="hello_txt"
                                contentType="text/plain; charset=utf-8"
                                method="#{bean.sayHello}"/>
    </af:commandButton>


public void sayHello(FacesContext context, OutputStream out) throws IOException
{
  OutputStreamWriter w = new OutputStreamWriter(out, "UTF-8");
  w.write("Hi there!");
  // The stream is automatically closed, but since we wrapped it,
  // we'd better flush our writer
  w.flush();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM