2014-02-27 33 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); –