2014-02-27 34 views
0

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

的代码如下:

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),并且当用户点击链接时,文件被下载。有没有其他方法可以做到这一点?谢谢。

回答

0

创建commandLink和提供File Download Action Listener到和代码到听者

<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(); 
} 
+0

嗨,实际上我有编程方式创建命令按钮并试图alose以下代码 –

+0

RichCommandImageLink BT =新RichCommandImageLink(); bt.setText(“mybt”+ part.getFileName());MethodExpression returnMethodExpression = fileDownladActionListenerMethodExpression(“#{pageFlowScope.jagranmailclient.downloadAttachment}”); FileDownloadActionListener fileDownLoadListener = new FileDownloadActionListener(); fileDownLoadListener.setMethod(returnMethodExpression); fileDownLoadListener.setContentType(“plain/text”); fileDownLoadListener.setFilename(fileName); testbt.addActionListener(fileDownLoadListener); –