2012-01-16 20 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 

同時に私のアドレスバーが HTTPこのURLを表示:// localhostを:8080/JSF-RichFacesの-3.3.3-デモ-2 /顔/ fileDownloadServlet /ログ/ project.log

に助けてください.. 事前に感謝します。

答えて

2

代わりに、別のサーブレットを使用してジップを行い、その代わりにh:outputLinkを指すことをお勧めします。 FacesServletを使って何らかの方法でファイルをプッシュすることができたとしても、移植できないか、または予期しない問題が発生する可能性があります。

  1. があなたの新しいサーブレットを指してlinkh:outputLinkを追加あなたは
  2. あなたのweb.xmlにこのサーブレットのmappingを追加 zip形式のログファイルを生成する単純なservletを実装する必要があります
+0

こんにちはmrembisz、ご回答いただきありがとうございます。私はあなたのアイデアをキャッチできません。詳細な説明を教えてください。そうでなければサンプルURL? – jackrobert

+0

@jackrobertがいくつかのリンクを追加しました。彼らは問題を手助けするべきです – mrembisz

+0

こんにちはmrembisz。私は私の質問を更新する..また、私はエラーがあった。ダウンロードプロセスが機能していません... – jackrobert

関連する問題