1 Reply Latest reply on Jun 30, 2007 6:10 PM by Joan Horta Tosas

    3 Extra cr-lf downloading files

    Roberto Scienza Newbie

      Hey all, my problem is i am downloading some files via http using Jboss 4.0 and i get 3 extra cr-lf at the end of the files i download (zip, pdf, txt... all the files have 6 extra bytes at the end).
      My code is in a jsp:

      <%@ page import="java.io.File,
      java.io.InputStream,
      java.io.FileInputStream,
      java.io.OutputStream"%>
      <%@ page session="false" %>
      <%
      String contentType = (String)application.getAttribute("fileupload_type");
      String fileName = (String)application.getAttribute("fileupload_name");

      String allowCache = "true";
      String openDirectly = "false";

      if(allowCache == null || allowCache.equalsIgnoreCase("false"))
      {
      response.setHeader("pragma", "no-cache");
      response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");
      response.setHeader("Expires", "01 Apr 1995 01:10:10 GMT");
      }

      if(contentType!=null)
      {
      response.setContentType(contentType);
      }

      if(fileName != null)
      {
      fileName = fileName.substring(fileName.lastIndexOf('\\')+1);
      fileName = fileName.substring(fileName.lastIndexOf('/')+1);

      StringBuffer contentDisposition = new StringBuffer();

      if(openDirectly==null || openDirectly.equalsIgnoreCase("false"))
      {
      contentDisposition.append("attachment;");
      }

      contentDisposition.append("filename=\"");
      contentDisposition.append(fileName);
      contentDisposition.append("\"");

      response.setHeader ("Content-Disposition", contentDisposition.toString());
      }

      byte[] bytes = (byte[])application.getAttribute("fileupload_bytes");
      if (bytes != null)
      {
      response.setContentLength(bytes.length);
      response.getOutputStream().write(bytes);
      response.getOutputStream().flush();
      response.getOutputStream().close();
      response.flushBuffer();
      %>

      Thanks for your help.

        • 1. Re: 3 Extra cr-lf downloading files
          Joan Horta  Tosas Novice

          Your problem is due to using a JSP. Between the JSP "directives" you have CRLF that is send to the client, that is, between

          <%@ page import="java.io.File,
          java.io.InputStream,
          java.io.FileInputStream,
          java.io.OutputStream"%>
          
          and
          <%@ page session="false" %>
          you have a CRLF, as well as in other places... You could take them off, but you will get one CRLF (the last one)... So it's not a good practice to download files through JSP; better use a Servlet or you will get corrupted data most of the times.