我正在使用以下代码下载文件,它适用于小文件,但是当我尝试下载大小> 11GB 的文件时,代码不起作用并出现java.lang.NegativeArraySizeException
异常
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();;
}
}
}
}
错误在这一行:
byte[] dataRead = new byte[(int)downzip.length()];
对于大文件,该值(int)downzip.length()
变为负数并给出java.lang.NegativeArraySizeException
异常。