使用JSF应用程序下载日志文件? [英] Download log file using JSF application?
问题描述
我将日志文件保存到此位置: E:\Tomcat-6.0.29\Tomcat6.0\logs\project.log
我的tomcat(webapps)位置: E:\Tomcat-6.0.29\Tomcat 6.0\webapps
当我点击a4j:命令按钮我想下载该日志文件,而不更改内容和文件名。
以下代码在(JSF1.2)中工作。但是
转换JSF2.0后,以下代码无效。
download.jsp
< h:form id =downloadFormbinding =#{Download.initForm}>
< a4j:outputPanel id =downloadOutputPanel>
< a4j:commandButton value =下载日志
action =#{Download.downloadButtonAction}
reRender =downloadOutputPanel/> < / A4J:outputpanel>
< / h:form>
Download.java
package com.test;
import 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下载{
private HtmlForm initForm;
public String downloadButtonAction()
{
String fileName =logs+ File.separator +project.log;
System.setProperty(download.logfile,download-logfile);
downloadLogFile(fileName);
返回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(下载模板时出现异常:+ ex);
}
}
public HtmlForm getInitForm(){
return initForm;
}
public void setInitForm(HtmlForm initForm){
this.initForm = initForm;
}
}
我的 FileDownloadServlet.java / p>
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);
字符串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(异常异常)
{
System.out.println(解析请求URI时发生异常:+异常);
}
finally
{
System.out.println(下载servlet解析后的文件路径+ filePath);
}
filePath = filePath + File.separator + fileName;
fileName = URLDecoder.decode(fileName,UTF-8);
文件文件= new File(filePath);
try
{
contentType = request.getSession()。getServletContext()。getMimeType(fileName);
}
catch(异常异常)
{
System.out.println(异常时获取内容类型:,异常);
}
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(异常e)
{
System.out.println(文件下载中的异常+ e);
}
finally
{
close(output);
关闭(输入);
}
}
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>
...
...
错误是: / strong>
HTTP状态404 - /fileDownloadServlet/logs/project.log未找到
类型状态报告
message /fileDownloadServlet/logs/project.log未找到
描述请求的资源(/fileDownloadServlet/logs/project.log未找到)`不可用`
Apache Tomcat / 6.0.29
同时我的地址栏显示此网址
http:// localhost :8080 / JSF-Richfaces-3.3.3-Demo-2 / faces / fileDownloadServlet / logs / project.log
帮助我..
提前感谢。
我宁愿推荐使用另一个servlet做压缩,并用 h:outputLink
代替。即使您设法以某种方式将文件推送到FacesServlet中,也可能不是便携式的,也可能会导致一些意想不到的问题。
In my application developed by JSF2.0 + Richfaces3.3.3 + Tomcat6.0.29.
I maintain my log file into this location : E:\Tomcat-6.0.29\Tomcat6.0\logs\project.log
My tomcat(webapps) Location : E:\Tomcat-6.0.29\Tomcat 6.0\webapps
When i click that a4j:commandbutton i want to download that log file, without change the content and filename.
The following code worked in (JSF1.2). But
After convert JSF2.0, the following code doesn't work.
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;
import 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;
}
}
And my FileDownloadServlet.java class
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>
...
...
Error is :
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
At the same time my address bar show this url http://localhost:8080/JSF-Richfaces-3.3.3-Demo-2/faces/fileDownloadServlet/logs/project.log
Help me.. Thanks in advance.
I would rather recommend using another servlet to do the zipping and point to it with h:outputLink
instead. Even if you do manage to somehow push the file through the FacesServlet it may not be portable or may cause some unexpected problems.
- You would need to implement a simple servlet which generates a zipped log file
- Add mapping for this servlet to your web.xml
- Add
h:outputLink
with a link pointing to your new servlet
这篇关于使用JSF应用程序下载日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!