gpt4 book ai didi

java - 面对 ClientAbortException

转载 作者:行者123 更新时间:2023-12-01 04:23:37 27 4
gpt4 key购买 nike

有一个 servlet 代码用于将 excel/zip 文件从生产服务器下载到本地计算机。当我单击生产服务器上的“保存”或“打开”按钮时,它会抛出 ClientAbortException。相同的代码在 Dev 和 UAT 中运行良好。而且这个错误并不总是出现。下面是相同的代码 -

        response.setContentType("application/download"); 
response.setHeader("Content-Disposition", "attachment; filename= \""+ fileName +"\"");
fin = new FileInputStream(f);
int size = fin.available();
response.setContentLength(size);
byte[] ab = new byte[size];

os = response.getOutputStream();

int bytesread;

do{
bytesread = fin.read(ab,0,size);
if(bytesread >-1) {
os.write(ab,0,bytesread );
}
} while(bytesread >-1);
os.flush();
os.close();

最佳答案

您可能与 HTML 防火墙设备发生冲突,该设备存在于生产环境中,但不存在于您的测试环境中。

此外,您的代码无法很好地扩展。如果 500 个用户同时尝试下载 50 MB 的文件,您的服务器会发生什么情况?

您可以构建一个可扩展的循环,如下所示:

byte[] buffer = new byte[bufferSize];
int bytesRead = input.read(buffer);
while (bytesRead > 0) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}

选择适当的bufferSize,例如20480或左右。

关于java - 面对 ClientAbortException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18712965/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com