commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bibs L <>
Subject Help! Download .CSV file using SurvletResponse, it added unwanted characters in the end of the file!!
Date Tue, 17 Jul 2007 13:47:08 GMT
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 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 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!
  <%@ taglib uri="" prefix="f"%>
  <%@ page import="java.util.ResourceBundle" %> 
<%@ page import="com......bean.DownloadFileBean" %>
  <f:loadBundle basename="" var="msgBundle" />
  javax.faces.context.FacesContext facesContext = null; 
  ResourceBundle messages = ResourceBundle.getBundle(""); 
  LogServices LOG = LogServicesFactory.getInstance().createLogServices(DownloadFileBean.class);
  //get an instance from faceContext
  facesContext = javax.faces.context.FacesContext.getCurrentInstance(); 
  DownloadFileBean downloadFileBean = (DownloadFileBean)facesContext.getApplication().getVariableResolver().resolveVariable(facesContext,
  String result = downloadFileBean.DownloadFile(); 
  } catch (Exception e) {
  LOG.error("Exception : RenderFile.jsp - " + e.getMessage(), e); 
  } finally {
  if (facesContext != null) facesContext = null; 
   public boolean DownloadFile (){ 
  boolean result = false;  
  FacesContext faces = null;
  HttpServletResponse response = null;  
  File file = null;  
  FileInputStream fileIn = null;   
  ServletOutputStream output = null;   
   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.setHeader("Content-Disposition","attachment; filename=\"" + file.getName() + "\"");
   IOUtils.copy(fileIn, output);   
   long endTime = System.currentTimeMillis();
   result = true; 
   LOG.debug("Total Time Taken to download: "+(endTime - startTime) + " ms");            
  } catch (FileNotFoundException e) {                
   LOG.error("FileNotFoundException : - " + e.getMessage(), e);    
  } catch (IOException e) {      
   LOG.error("IOException : - " + e.getMessage(), e);
  } catch (Exception e) {
   LOG.error("Exception : - " + 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 : - 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. 
  • Unnamed multipart/alternative (inline, 8-Bit, 0 bytes)
View raw message