Results 1 to 5 of 5
  1. #1
    Ziden is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default Downloading a file thro Struts via response OutputStreaming

    Hello there. Im making a application to download files. The problem im having is: Some files like PDF or DOC are downloaded correctly, but a .exe or a .dat or .txt are nawt. Im not really sure why is this happening, ive tryed many things. Heres the download part witch i belive is wrong somehow.

    Java Code:
    public void download(int indice) throws Throwable {   
      
            if(!validate()) throw new Exception("No response !");   
      
            response.setContentType("application/octet-stream");           
            String path = getDownloader().getDir() + getDownloader().getListaArquivos().get(indice); // example "C:/folder/file.txt"   
               
            File file = new File(path);   
            FileInputStream fileIn = new FileInputStream(file);   
            ServletOutputStream out = response.getOutputStream();   
               
            byte[] outputByte = new byte[2*4096];   
               
            while(fileIn.read(outputByte, 0, 2*4096) != -1) {   
                out.write(outputByte, 0, 2*4096);   
            }   
            fileIn.close();   
            out.flush();   
            out.close();   
        }
    Ive tryed to add this following line:

    response.setHeader("Content-disposition", "attachment;"); , it does not work as well. If i add this in the begining of the page, it makes me download the .JSP, if i make this inside download method, it does nothing.

    Does anyone knows how to solve this ?

    Thanx for any responses.

    []s Ziden

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Java Code:
    out.write(outputByte, 0, 2*4096);
    You are writing out 8k everytime...even if the read did not read in 8k. Which would be the likely case at the end of a document.

    You'll need to get the byte count from the read() call and use that.

  3. #3
    Ziden is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    How could i count the bytes im reading ?

    I could make this work making this:

    Java Code:
      public void download(int indice,  HttpServletResponse resp) throws Throwable {
                  resp.addHeader("Content-Disposition","Attachment;Filename="+getDownloader().getListaArquivos().get(indice));
                   String caminho = getDownloader().getDir() + getDownloader().getListaArquivos().get(indice);
            File file = new File(caminho);
            FileInputStream fileIn = new FileInputStream(file);
            ServletOutputStream out = resp.getOutputStream();
            
            byte[] outputByte = new byte[2*4096]; 
    
            while(fileIn.read(outputByte, 0, 2*4096) != -1) {
                out.write(outputByte, 0, 2*4096);
            }
            
            // THIS IS BAD
            fileIn.close();
            out.flush();
            out.close(); 
            
        }
    The thing is, the THIS IS BAD comment is only runned after the user accepts the download, IF it does. In cases the user dont accept the download, the program gets locked for a few seconds and if i try to download another file thro that, it gets me a socket write error.

    Thanx for your help.

    []s Ziden

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default

    Java Code:
    fileIn.read(outputByte, 0, 2*4096)
    returns an int...that is the number of bytes read (or -1 if you've read everything).
    So assign that value to an int.
    Java Code:
    int bytecount = 0;
    while((bytecount  = fileIn.read(outputByte, 0, 2*4096)) != -1) {
    etc etc

  5. #5
    Ziden is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Oh i see... i got that right but still dont know about the guys 'refusing' download, since the past-while code only executes if the user ACCEPTS the download

    is there somehow to make downloads not acceptables ? Making they start w/ no Save/Open etc ? U click, download start.

Similar Threads

  1. File Download .zip is downloading weird
    By hardcorebadger in forum New To Java
    Replies: 0
    Last Post: 02-25-2011, 08:08 PM
  2. problem in downloading file from a URL.
    By lalit.wientech in forum Networking
    Replies: 11
    Last Post: 03-26-2010, 12:27 PM
  3. Downloading a jar file
    By jojo in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2009, 11:04 PM
  4. Downloading a file from web server
    By rlaknar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-13-2009, 09:15 AM
  5. JSP + Struts + XML + response.setHeader()
    By rcv007 in forum Web Frameworks
    Replies: 0
    Last Post: 08-22-2008, 02:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •