gpt4 book ai didi

java - 为什么当文件大小大于 2 MB 时,Java servlet 会给出 ClientAbortException?

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:46 26 4
gpt4 key购买 nike

我正在尝试编写一个位于 Linux 服务器上的 java servlet,客户端可以使用它来下载视频文件。当文件大小较小(可能小于 2 MB)时它可以工作,但较大的文件大小会返回错误:org.apache.catalina.connector.ClientAbortException:java.io.IOException:损坏的管道

经过Google搜索,发现这个错误是在客户端断开连接时发生的。就我而言,我正在使用客户端,并且可以确认我没有做任何会中断连接的事情(至少不是故意的)——当发生此错误时,浏览器保持打开状态等。

知道可能是什么原因造成的(以及如何修复)吗?

public class GetFile extends HttpServlet {

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}

protected void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

String filename ="init_java";

try {

// get user parameters
filename = req.getParameter("fileId"); // complete path to video file
//res.setContentType("video/mp4"); //not working
res.setContentType("application/x-download");

File file=new File(filename);

if (file.exists()) {

res.setHeader("Content-Disposition", "inline; filename=\""+filename+"\"");
res.setHeader("Cache-Control", "cache, must-revalidate");
//res.setHeader("Pragma", "public"); // not sure when to use
returnFile(filename, res.getOutputStream());

} else {
//error handling goes here
}

} catch (Exception e) {
...
} finally {
...
}
}


private static void returnFile(String filename, OutputStream out) throws FileNotFoundException, IOException {
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(filename));
byte[] buf = new byte[4 * 1024]; // 4K buffer
int bytesRead;
while ((bytesRead = in.read(buf)) != -1) {
out.write(buf, 0, bytesRead);
}

} finally {
if (in != null) in.close();
}
}

}

更新 1

我在 mod_jk.log 文件(该文件将请求从 Apache Web 服务器传输到 GlassFish 应用程序服务器)中看到以下错误:

[info] init_jk::mod_jk.c (3383): mod_jk/1.2.40 initialized
[error] ajp_connection_tcp_get_message::jk_ajp_common.c (1313): wrong message format 0xcad5 from ::1:8009
[error] ajp_get_reply::jk_ajp_common.c (2204): (worker1) Tomcat is down or network problems. Part of the response has already been sent to the client
[info] ajp_service::jk_ajp_common.c (2673): (worker1) sending request to tomcat failed (recoverable), because of protocol error (attempt=1)
[info] ajp_process_callback::jk_ajp_common.c (2000): Writing to client aborted or client network problems
[info] ajp_service::jk_ajp_common.c (2673): (worker1) sending request to tomcat failed (unrecoverable), because of client write error (attempt=2)
[info] jk_handler::mod_jk.c (2799): Aborting connection for worker=worker1

它似乎在跟踪我观察到的情况,但我不是这方面的专家 - 这是否可以深入了解可能的根本原因?

最佳答案

这可能是由于您的服务器上设置的限制造成的。检查属性文件。我确信有人说文件不得大于 2MB。

关于java - 为什么当文件大小大于 2 MB 时,Java servlet 会给出 ClientAbortException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26430639/

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