Java code to download a file or a zip file
<p>I am using below code to download a file and it works fine for small files, but when I tried to download a file whose size > 11GB code is not working and giving <code>java.lang.NegativeArraySizeException</code> exception</p>

<pre><code>public String downloadDirectory()
{

     OutputStream myOut = null;
     FileInputStream fileInputStream = null;
     File downzip = new File(dirName+"/"+dir+".zip");


   getServletResponse().setContentType("TEXT/HTML");
   getServletResponse().setHeader("Content-Disposition","attachment; filename=\"" + dir +".zip" + "\"");
   getServletResponse().setContentLength((int)downzip.length());
    System.out.println("length  "+(int)downzip.length());
     //READ DATA FROM FILE

            byte[] dataRead = new byte[(int)downzip.length()];
               fileInputStream = new FileInputStream(downzip);
               fileInputStream.read(dataRead,0,(int)downzip.length());
    //WRITE DATA TO OUTFILE
               myOut = getServletResponse().getOutputStream();


          myOut.write(dataRead);

           if (fileInputStream != null) {                       
               fileInputStream.close();
           }



    }
    catch(Exception e)
    {
        e.printStackTrace();
         Execute.rmdirscript(dirName);
        return "exception";
    }
     finally{
        System.out.println("finally downloaddir");
    if(myOut!=null){
        try{
        myOut.close();}
        catch(Exception e){
            e.printStackTrace();;
        }
    }
    if (fileInputStream != null) {                       
               try{
        fileInputStream.close();}
        catch(Exception e){
            e.printStackTrace();;
        }
           }
}


}
</code></pre>

<p>The error is at this line:</p>

<pre><code>byte[] dataRead = new byte[(int)downzip.length()];
</code></pre>

<p>The value <code>(int)downzip.length()</code> becomes negative for big files and gives <code>java.lang.NegativeArraySizeException</code> exception. </p>
