jsf - 使用 JSF 应用程序下载日志文件?

标签 jsf tomcat download richfaces

我的应用是JSF2.0 + Richfaces3.3.3 + Tomcat6.0.29开发的。

我将我的日志文件保存在这个位置:E:\Tomcat-6.0.29\Tomcat6.0\logs\project.log
我的 tomcat(webapps) 位置:E:\Tomcat-6.0.29\Tomcat 6.0\webapps

当我单击 a4j:commandbutton 时,我想下载该日志文件,而不更改内容和文件名。

以下代码适用于 (JSF1.2)。但是
转换JSF2.0后,以下代码不起作用。

下载.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>

下载.java

package com.test;

导入java.io.文件; 导入 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://localhost:8080/JSF-Richfaces-3.3.3-Demo-2/faces/fileDownloadServlet/logs/project.log

帮帮我.. 提前致谢。

最佳答案

我宁愿推荐使用另一个 servlet 进行压缩并使用 h:outputLink 指向它。即使您设法以某种方式通过 FacesServlet 推送文件,它也可能无法移植或可能导致一些意外问题。

  1. 您需要实现一个简单的 servlet这会产生一个 压缩日志文件
  2. 添加mapping对于这个 servlet 到你的 web.xml
  3. 添加带有 linkh:outputLink指向你的新 servlet

关于jsf - 使用 JSF 应用程序下载日志文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8877794/

相关文章:

java - 如何在用户按 Tab 时检查文本框值

jsf - 如何将注释与 faces-config.xml 混合

tomcat - 用于 tomcat 7 的 apache maven tomcat 插件的 404 问题

java - 如何通过部署他们的 war 从 Tomcat 在本地运行多个依赖应用程序?

java - 将 XML 属性转换为有效的 HTML 属性值?

android - 通过 Retrofit 2 Asynchronous 下载时是否可以显示进度条?

javascript - 使用reactJS下载xlsx文件: Excel can not open file

java - JSF 表单不执行操作,因为组合框

jsf - <p :commandButton> inside <ui:fragment rendered> is not invoked 的 Action

svn - 从 SVN 存储库下载文件