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

使用XEP-0363向Openfire上传文件触发Connection reset错误如何解决

问题原因
  1. 未携带XEP-0363 slot返回的必填请求头:从返回的slot XML可以看到,put节点下明确要求携带Authorization和Cookie两个请求头,现有代码只构造了HttpPut对象和文件实体,没有添加这两个头,上传服务端未识别到合法身份直接断开连接,是报错的核心原因。
  2. Openfire日志对应的5222端口是XMPP的C2S连接端口,该报错是客户端主程序在HTTP上传抛出异常后直接退出,未正常关闭XMPP长连接导致的,属于次生错误,和上传逻辑本身无关。
  3. 其他可能的次要原因:SSL证书校验不通过(如果上传域名用的是自签证书)、文件大小超出Openfire HTTP上传组件的配置上限、客户端网络无法正常访问上传服务的端口。
解决方案
  • 解析slot XML中的put节点下的所有header,构造HttpPut的时候全部添加到请求中,修改后的代码参考:
private void apacheUpload(File file,URI url, Map<String, String> headers) throws Exception {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPut put = new HttpPut(url);
    // 新增:添加所有slot返回的请求头
    if (headers != null) {
        for (Map.Entry<String, String> header : headers.entrySet()) {
            put.addHeader(header.getKey(), header.getValue());
        }
    }
    FileEntity reqEntity = new FileEntity(file, ContentType.APPLICATION_OCTET_STREAM);
    put.setEntity(reqEntity);
    // 可选:添加超时配置避免长时间卡住
    RequestConfig requestConfig = RequestConfig.custom()
        .setConnectTimeout(5000)
        .setSocketTimeout(30000)
        .build();
    put.setConfig(requestConfig);
    HttpResponse response = null;
    try {
        response = httpclient.execute(put);
    } catch (IOException e) {
        throw new Exception(e.getMessage());
    } finally {
        httpclient.close();
    }
    if(response == null) {
        return ;
    }
    int responseCode = response.getStatusLine().getStatusCode();
    if (responseCode < 200 || responseCode >= 300)
         throw new Exception("bad status code: " + responseCode);
    HttpEntity entity = response.getEntity();
    String responseString = EntityUtils.toString(entity);
    System.out.println("response");
    System.out.println(responseString);
}
  • 如果上传域名使用自签SSL证书,需要给HttpClient配置信任该证书,调试阶段可暂时关闭SSL证书校验。
  • 检查Openfire的HTTP上传插件配置,确认上传文件大小上限大于当前上传的文件大小。
  • 主程序捕获上传异常后不要直接退出,保持XMPP连接的正常存活,避免出现5222端口的连接重置报错。
  • 调试阶段可先用curl命令验证上传链路是否正常,确认问题不在网络层面:
curl -X PUT "https://upload.montague.tld/4a771ac1-f0b2-4a4a-9700-f2a26fa2bb67/tr%C3%A8s%20cool.jpg" \
  -H "Authorization: Basic Base64String==" \
  -H "Cookie: foo=bar; user=romeo" \
  --data-binary "@本地文件路径"

如果curl上传正常,说明问题集中在Java代码逻辑,按上述修改即可。

内容的提问来源于stack exchange,提问作者hussein wehbe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 06:54:03