You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Bibs L <bi...@yahoo.com> on 2007/07/17 15:47:08 UTC
Help! Download .CSV file using SurvletResponse, it added unwanted characters in the end of the file!!
Hi, I need some help woth downloading .CSV file with the regular ServletResponseObject, it is adding unwanted characters (2 boxes) in the end of the file when I download it. Your help would be greatly appreciated!
I have a download.jsp file, that calls a manage bean downloadfile.java to render a file, it is all working well with the excaption of downloading .CSV file. The problem is that when I try to download a CSV file, by clickin on the link, thats calls the Download.java bean which set the Response object, and opens up the "Download File Diaglog - Open/Save As", when I open/save as the CSV file, it added unwanted characters at the very bottom of the file. Even when I tested to upload a totally empty file, when I download it, it added the unwanted characters (2 Boxes) , where it should have been empty.
Orignally, I have the <html><body>..etc tags on the download.jsp, and all of those HTML code were added to the end of the CSV file in addition to the unwanted file. So, I removed all of the unnesscary <html> tags, and the HTML codes are gone from the CSV file, but the unwanted characters are still being added at the end of the file. I have no idea how it got there. PLEASE HELP!
---------------------------------------------------
Download.jsp:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ page import="java.util.ResourceBundle" %>
<%@ page import="com......bean.DownloadFileBean" %>
<f:loadBundle basename=".....nl.Resource_en" var="msgBundle" />
<%
javax.faces.context.FacesContext facesContext = null;
ResourceBundle messages = ResourceBundle.getBundle("com......nl.ErrorMessages");
LogServices LOG = LogServicesFactory.getInstance().createLogServices(DownloadFileBean.class);
try{
//get an instance from faceContext
facesContext = javax.faces.context.FacesContext.getCurrentInstance();
DownloadFileBean downloadFileBean = (DownloadFileBean)facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "downloadFileBean");
String result = downloadFileBean.DownloadFile();
} catch (Exception e) {
LOG.error("Exception : RenderFile.jsp - " + e.getMessage(), e);
} finally {
if (facesContext != null) facesContext = null;
}
%>
---------------------------------------------------
DownloadFile.Java:
public boolean DownloadFile (){
boolean result = false;
FacesContext faces = null;
HttpServletResponse response = null;
File file = null;
FileInputStream fileIn = null;
ServletOutputStream output = null;
try{
MyFileBean myFileBean = (MyFileBean)getBean("myFileBean");
String fileName = myFileBean.getRenderingFileName();
String filePath = myFileBean.getRenderingFilePath();
String fileFullPath = myFileBean.getRenderingFilePath() + fileName;
LOG.debug("file full path = " + fileFullPath);
if (fileFullPath == null) {
LOG.debug("file path is null...");
return result;
}
file = new File(fileFullPath);
if(file.exists() == false){
LOG.debug(fileName + " - file not found...");
return result;
}
faces = FacesContext.getCurrentInstance();
response = (HttpServletResponse) faces.getExternalContext().getResponse();
long startTime = System.currentTimeMillis();
fileIn = new FileInputStream(file);
output = response.getOutputStream();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment; filename=\"" + file.getName() + "\"");
IOUtils.copy(fileIn, output);
output.flush();
faces.responseComplete();
long endTime = System.currentTimeMillis();
result = true;
LOG.debug("Total Time Taken to download: "+(endTime - startTime) + " ms");
} catch (FileNotFoundException e) {
LOG.error("FileNotFoundException : DownloadFileBean.java - " + e.getMessage(), e);
} catch (IOException e) {
LOG.error("IOException : DownloadFileBean.java - " + e.getMessage(), e);
} catch (Exception e) {
LOG.error("Exception : DownloadFileBean.java - " + e.getMessage(), e);
} finally {
//Close output and input resources.
try {
if (faces != null) faces = null;
if (response != null) response = null;
if (file != null) file = null;
if (output != null) output.close();
} catch (Exception e) {
LOG.error("Exception : DownloadFileBean.java - Closing objects " + e.getMessage(), e);
}
}
LOG.debug(">>>END - DownloadFileBean : downloadFile ");
return result;
}
----------------------------------------------------------
Unwanted Code added to CSV File: (Used to be like this before I removed all the HTML tags from Download.jsp)
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="IBM Software Development Platform"> <TITLE>Render File</TITLE> </HEAD> <body class="bodyWithinFrame"> <form id="RenderFileForm" method="post" action="/Download.jsp" enctype="multipart/form-data"> 4 unwanted boxes...
<input type="hidden" name="RenderFileForm" value="RenderFileForm" /></form> </body> </HTML>
----------------------------------------------------------
Unwanted Code added to CSV File: (after I removed all the HTML tags from Download.jsp)
2 boxes (I can't copy and pasted the unwanted boxes into the email.)
---------------------------------
Be a better Heartthrob. Get better relationship answers from someone who knows.
Yahoo! Answers - Check it out.
Re: Help! Download .CSV file using SurvletResponse, it added unwanted characters in the end of the file!!
Posted by Martin Cooper <ma...@apache.org>.
On 7/17/07, Bibs L <bi...@yahoo.com> wrote:
>
> Hi, I need some help woth downloading .CSV file with the regular
> ServletResponseObject, it is adding unwanted characters (2 boxes) in the end
> of the file when I download it. Your help would be greatly appreciated!
This really is not related to Commons in any way that I can see, but...
I have a download.jsp file
This is your problem. Move your code to a servlet. The extra characters are
almost certainly spaces or line breaks from your JSP page.
--
Martin Cooper
, that calls a manage bean downloadfile.java to render a file, it is all
> working well with the excaption of downloading .CSV file. The problem is
> that when I try to download a CSV file, by clickin on the link, thats calls
> the Download.java bean which set the Response object, and opens up the
> "Download File Diaglog - Open/Save As", when I open/save as the CSV file, it
> added unwanted characters at the very bottom of the file. Even when I
> tested to upload a totally empty file, when I download it, it added the
> unwanted characters (2 Boxes) , where it should have been empty.
>
> Orignally, I have the <html><body>..etc tags on the download.jsp, and
> all of those HTML code were added to the end of the CSV file in addition to
> the unwanted file. So, I removed all of the unnesscary <html> tags, and the
> HTML codes are gone from the CSV file, but the unwanted characters are still
> being added at the end of the file. I have no idea how it got
> there. PLEASE HELP!
>
> ---------------------------------------------------
> Download.jsp:
> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
> <%@ page import="java.util.ResourceBundle" %>
>
> <%@ page import="com......bean.DownloadFileBean" %>
> <f:loadBundle basename=".....nl.Resource_en" var="msgBundle" />
>
> <%
> javax.faces.context.FacesContext facesContext = null;
> ResourceBundle messages = ResourceBundle.getBundle
> ("com......nl.ErrorMessages");
> LogServices LOG = LogServicesFactory.getInstance().createLogServices(
> DownloadFileBean.class);
>
> try{
>
> //get an instance from faceContext
> facesContext = javax.faces.context.FacesContext.getCurrentInstance();
> DownloadFileBean downloadFileBean =
> (DownloadFileBean)facesContext.getApplication().getVariableResolver().resolveVariable(facesContext,
> "downloadFileBean");
>
> String result = downloadFileBean.DownloadFile();
>
>
>
> } catch (Exception e) {
> LOG.error("Exception : RenderFile.jsp - " + e.getMessage(), e);
>
> } finally {
> if (facesContext != null) facesContext = null;
> }
> %>
>
>
> ---------------------------------------------------
> DownloadFile.Java:
>
> public boolean DownloadFile (){
>
> boolean result = false;
> FacesContext faces = null;
> HttpServletResponse response = null;
> File file = null;
> FileInputStream fileIn = null;
> ServletOutputStream output = null;
>
> try{
>
> MyFileBean myFileBean = (MyFileBean)getBean("myFileBean");
>
> String fileName = myFileBean.getRenderingFileName();
> String filePath = myFileBean.getRenderingFilePath();
> String fileFullPath = myFileBean.getRenderingFilePath() + fileName;
> LOG.debug("file full path = " + fileFullPath);
>
> if (fileFullPath == null) {
> LOG.debug("file path is null...");
> return result;
> }
>
> file = new File(fileFullPath);
>
> if(file.exists() == false){
> LOG.debug(fileName + " - file not found...");
> return result;
> }
> faces = FacesContext.getCurrentInstance();
> response = (HttpServletResponse) faces.getExternalContext
> ().getResponse();
>
> long startTime = System.currentTimeMillis();
>
> fileIn = new FileInputStream(file);
> output = response.getOutputStream();
> response.setContentType("application/octet-stream");
> response.setHeader("Content-Disposition","attachment; filename=\"" +
> file.getName() + "\"");
> IOUtils.copy(fileIn, output);
> output.flush();
> faces.responseComplete();
>
> long endTime = System.currentTimeMillis();
>
> result = true;
> LOG.debug("Total Time Taken to download: "+(endTime - startTime) + "
> ms");
>
> } catch (FileNotFoundException e) {
> LOG.error("FileNotFoundException : DownloadFileBean.java - " +
> e.getMessage(), e);
> } catch (IOException e) {
> LOG.error("IOException : DownloadFileBean.java - " + e.getMessage(),
> e);
> } catch (Exception e) {
> LOG.error("Exception : DownloadFileBean.java - " + e.getMessage(), e);
> } finally {
> //Close output and input resources.
> try {
>
> if (faces != null) faces = null;
> if (response != null) response = null;
> if (file != null) file = null;
> if (output != null) output.close();
> } catch (Exception e) {
> LOG.error("Exception : DownloadFileBean.java - Closing objects " +
> e.getMessage(), e);
> }
> }
> LOG.debug(">>>END - DownloadFileBean : downloadFile ");
> return result;
> }
>
> ----------------------------------------------------------
> Unwanted Code added to CSV File: (Used to be like this before I removed
> all the HTML tags from Download.jsp)
>
>
> <META http-equiv="Content-Type" content="text/html;
> charset=ISO-8859-1"> <META name="GENERATOR" content="IBM Software
> Development Platform"> <TITLE>Render
> File</TITLE> </HEAD> <body
> class="bodyWithinFrame"> <form id="RenderFileForm" method="post"
> action="/Download.jsp" enctype="multipart/form-data"> 4
> unwanted boxes...
>
> <input type="hidden" name="RenderFileForm" value="RenderFileForm"
> /></form> </body> </HTML>
>
>
> ----------------------------------------------------------
> Unwanted Code added to CSV File: (after I removed all the HTML tags from
> Download.jsp)
>
> 2 boxes (I can't copy and pasted the unwanted boxes into the email.)
>
>
> ---------------------------------
> Be a better Heartthrob. Get better relationship answers from someone who
> knows.
> Yahoo! Answers - Check it out.