You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Kaltura文件下载:服务器无响应时返回错误而非空白文件

解决方案:添加响应校验与内容有效性检查

我来帮你搞定这个空白文件的问题!你当前的代码直接打开URL流就开始写入,完全没校验服务器的响应状态和返回内容的有效性,这才导致了空白文件被下载的情况。咱们可以从几个关键环节优化代码,实现错误识别和拦截:

String s = "https://www.kaltura.com/p/588888/sp.....";
try {
    URL url = new URL(s);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    
    // 1. 先校验HTTP响应状态码
    int responseCode = connection.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        res.setStatus(responseCode);
        res.getWriter().write("服务器返回错误:" + connection.getResponseMessage());
        connection.disconnect();
        return null;
    }
    
    // 2. 提前检查内容长度(部分场景下能快速识别空内容)
    long contentLength = connection.getContentLengthLong();
    if (contentLength == 0) {
        res.setStatus(HttpURLConnection.HTTP_NO_CONTENT);
        res.getWriter().write("服务器返回空白内容");
        connection.disconnect();
        return null;
    }
    
    InputStream in = new BufferedInputStream(connection.getInputStream());
    res.setContentType("application/octet-stream");
    res.setHeader("Cache-Control", "no-cache");
    res.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    
    OutputStream out = res.getOutputStream();
    byte[] buf = new byte[8192 * 2];
    int bytesRead;
    long totalBytesRead = 0;
    
    // 3. 读取流时统计实际读取的字节数
    while ((bytesRead = in.read(buf)) != -1) {
        totalBytesRead += bytesRead;
        out.write(buf, 0, bytesRead); // 只写入实际读取的字节,避免多余空字节
    }
    
    // 4. 校验最终读取的总字节数,为空则返回错误
    if (totalBytesRead == 0) {
        res.reset(); // 重置响应,避免已写入的头部干扰
        res.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);
        res.getWriter().write("下载内容为空");
    } else {
        res.setStatus(200);
        out.flush();
    }
    
    // 关闭资源
    in.close();
    out.close();
    connection.disconnect();
} catch (Exception e) {
    e.printStackTrace();
    // 异常时给客户端返回明确错误
    res.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR);
    try {
        res.getWriter().write("下载失败:" + e.getMessage());
    } catch (IOException ioEx) {
        ioEx.printStackTrace();
    }
}
return null;

关键优化点说明

  • 响应状态校验:通过HttpURLConnection获取服务器的响应码,只有200(正常状态)才继续处理,其他状态直接返回对应错误信息。
  • 内容长度预检查:提前读取Content-Length头,若为0则直接拦截,避免无意义的流读取操作。
  • 实际字节数统计:读取流时记录总字节数,读完后判断是否为空,为空则重置响应并返回错误,不生成空白文件。
  • 写入逻辑修正:原来的代码直接写入整个缓冲区,最后一次读取可能不足缓冲区长度,会写入多余空字节,现在改为只写入实际读取的字节数。
  • 异常处理增强:捕获异常后不仅打印堆栈,还会给客户端返回500错误和具体异常信息,方便排查问题。

内容的提问来源于stack exchange,提问作者Rohit Aggarwal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:54:20