2012-01-16 66 views
1

在我的應用程序由JSF2.0 + Richfaces3.3.3 + Tomcat6.0.29開發。使用JSF應用程序下載日誌文件?

我保持我的日誌文件到以下位置:E:\ Tomcat的6.0.29 \ Tomcat6.0 \日誌\ project.log
我的tomcat(web應用)地點:E:\ Tomcat6.0。 29 \ Tomcat 6.0 \ webapps

當我點擊a4j:commandbutton我想要下載該日誌文件,而不更改內容和文件名。

以下代碼在(JSF1.2)中工作。但是
轉換成JSF2.0後,下面的代碼不起作用。

download.jsp

<h:form id="downloadForm" binding="#{Download.initForm}"> 
     <a4j:outputPanel id="downloadOutputPanel"> 
       <a4j:commandButton value="Download Log" 
            action="#{Download.downloadButtonAction}" 
            reRender="downloadOutputPanel"/>        </a4j:outputpanel> 
</h:form> 

Download.java

package com.test; 

進口的java.io.File; import javax.faces.component.html.HtmlForm;

import javax.faces.context.ExternalContext; 
import javax.faces.context.FacesContext; 
import javax.servlet.http.HttpServletResponse; 
import org.apache.log4j.Logger; 

public class Download{ 

private HtmlForm initForm;  

public String downloadButtonAction() 
{   
    String fileName = "logs" + File.separator + "project.log"; 
    System.setProperty("download.logfile", "download-logfile") ; 
    downloadLogFile(fileName); 
    return null; 
} 

private void downloadLogFile(String fileName) 
{ 
    try 
    { 
    FacesContext facesContext = FacesContext.getCurrentInstance(); 
    ExternalContext context = facesContext.getExternalContext(); 
    HttpServletResponse response = (HttpServletResponse) context.getResponse(); 
    fileName = fileName.replace(File.separator, "/"); 

response.sendRedirect("/" + "JSF-Richfaces-3.3.3-Demo-2" + 
          /faces/fileDownloadServlet/" + fileName); 
} 
catch (Exception ex) 
{ 
    System.out.println("Exception occours while downloading templates: "+ ex); 
} 
} 

public HtmlForm getInitForm(){   
    return initForm; 
} 

public void setInitForm(HtmlForm initForm){ 
    this.initForm = initForm; 
} 
} 

而且我FileDownloadServlet.java

package com.test; 

import javax.servlet.ServletException; 
import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.Closeable; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.net.URLDecoder; 
import javax.servlet.ServletConfig; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class FileDownloadServlet extends HttpServlet 
{ 
ServletConfig servletConfig;  

@Override 
public void init(ServletConfig servletConfig) 
{ 
    this.servletConfig = servletConfig; 
} 

@Override 
protected void doGet(HttpServletRequest request, HttpServletResponse response) 
     throws IOException 
{ 
    String contentType = null; 
    String filePath = ""; 
    String fileName = ""; 

    String requestURI = request.getRequestURI(); 
    try 
    { 
     fileName = requestURI.substring(requestURI.lastIndexOf('/') + 1);      
     String catalinaHome = "." + File.separator + ".." + File.separator; 

     if (System.getProperty("os.name").contains("Windows")) 
     { 
      catalinaHome = ""; 
     } 
     filePath = catalinaHome + requestURI.substring(requestURI.indexOf(System.getProperty("download.logfile")), requestURI.lastIndexOf("/")); 
     filePath = filePath.replace("/", File.separator);   
    } 
    catch (Exception exception) 
    { 
     System.out.println("Exception occurred while parsing the request URI : " + exception); 
    } 
    finally 
    { 
     System.out.println("File path after parsing in download servlet : " + filePath); 
    } 

    filePath = filePath + File.separator + fileName;     
    fileName = URLDecoder.decode(fileName, "UTF-8");   
    File file = new File(filePath);    

    try 
    {    
     contentType = request.getSession().getServletContext().getMimeType(fileName); 
    } 
    catch (Exception exception) 
    { 
     System.out.println("Exception while getting content type : ", exception); 
    } 

    if (contentType == null) 
    { 
     contentType = "application/octet-stream"; 
    }   

    BufferedInputStream input = null; 
    BufferedOutputStream output = null; 
    try 
    {    
     input = new BufferedInputStream(new FileInputStream(file)); 
     int contentLength = input.available();   

     response.reset(); 
     response.setContentType(contentType); 
     response.setContentLength(contentLength); 
     response.setHeader("Content-disposition", "attachment; filename=\"" + 
       fileName + "\""); 
     output = new BufferedOutputStream(response.getOutputStream()); 

     for (int data; 
       (data = input.read()) != -1;) 
     { 
      output.write(data); 
     } 

     output.flush(); 
    } 
    catch (Exception e) 
    {   
     System.out.println("Exception in File Download : " + e); 
    } 
    finally 
    {   
     close(output); 
     close(input); 
    } 
} 


private static void close(Closeable resource) 
{ 
    if (resource != null) 
    { 
     try 
     { 
      resource.close(); 
     } 
     catch (IOException e) 
     {    
      System.out.println("Error ", e); 
     } 
    } 
} 
} 

的web.xml

... 
... 
<servlet> 
    <servlet-name>fileDownloadServlet</servlet-name> 
    <servlet-class>com.test.FileDownloadServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>fileDownloadServlet</servlet-name> 
    <url-pattern>/fileDownloadServlet/*</url-pattern> 
</servlet-mapping> 
... 
... 

錯誤是:

HTTP Status 404 - /fileDownloadServlet/logs/project.log not found 
type Status report 
message /fileDownloadServlet/logs/project.log not found 
description The requested resource (/fileDownloadServlet/logs/project.log not found) ` is not available.` 
Apache Tomcat/6.0.29 

同時我的地址欄中顯示該URL 的http://本地主機:8080/JSF,RichFaces的-3.3.3-DEMO-2 /面/ fileDownloadServlet /日誌/ project.log

幫助我.. 在此先感謝。

回答

2

我寧願推薦使用另一個servlet來進行壓縮,然後用h:outputLink來代替。即使您設法通過FacesServlet推送文件,它也可能不便攜,或者可能導致一些意外問題。

  1. 您需要實現一個簡單的servlet產生一個 壓縮日誌文件
  2. 爲這個servlet添加mapping到你的web.xml
  3. 添加h:outputLinklink指向您的新的servlet
+0

嗨mrembisz,謝謝你的迴應。我無法理解你的想法。請給我詳細的解釋。否則,任何示例網址? – jackrobert 2012-01-16 09:58:18

+0

@jackrobert添加了一些鏈接,他們應該幫助你解決問題 – mrembisz 2012-01-16 11:01:21

+0

嗨mrembisz。我更新我的問題..我也有錯誤。下載過程不起作用... – jackrobert 2012-01-17 06:32:41