내 응용 프로그램에서 JSF2.0 + Richfaces3.3.3 + Tomcat6.0.29로 개발했습니다.JSF 애플리케이션을 사용하여 로그 파일을 다운로드 하시겠습니까?
나는이 위치에 내 로그 파일을 유지 : E : \ 톰캣-6.0.29 \ Tomcat6.0 \ 로그 \ project.log
내 바람둥이 (웹 어플리케이션) 위치 : 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>
...
...
오류는 다음과 같습니다
// 로컬 호스트 : 8080/JSF - Richfaces-3.3.3-데모-2/얼굴/fileDownloadServlet가/로그/project.log동시에 내 주소 표시 줄의 표시 URL을 HTTP에서3210
도와주세요 .. 미리 감사드립니다.
안녕하세요 mrembisz, 답장을 보내 주셔서 감사합니다. 나는 너의 생각을 잡을 수 없다. 나에게 세부 설명을 해줘. 그렇지 않으면 샘플 URL? – jackrobert
@jackrobert가 몇 가지 링크를 추가했습니다. 문제를 해결하는 데 도움이됩니다. – mrembisz
안녕하세요 mrembisz. 내 질문을 업데이 트 ..또한 나는 오류가있어. 다운로드 프로세스가 작동하지 않습니다 ... – jackrobert