下载加速器在从后台bean提供下载时导致org.apache.catalina.connector.ClientAbor



我使用JSF并希望在我的页面中下载文件。我写了一些代码,但我得到ClientAbortException错误,当我使用一些下载管理器下载我的文件:

public class FileUtil  {

public static FacesContext getContext() {
    return FacesContext.getCurrentInstance();
}
public static void sendFile(File file, boolean attachment) throws IOException {
    sendFile(getContext(), file, attachment);
}
public static void sendFile(FacesContext context, File file, boolean attachment) throws IOException {
    sendFile(context, new FileInputStream(file), file.getName(), file.length(), attachment);
}
public static void sendFile(FacesContext context, byte[] content, String filename, boolean attachment) throws IOException {
    sendFile(context, new ByteArrayInputStream(content), filename, (long) content.length, attachment);
}
public static void sendFile(FacesContext context, InputStream content, String filename, boolean attachment) throws IOException {
    sendFile(context, content, filename, -1L, attachment);
}
private static void sendFile(FacesContext context, InputStream input, String filename, long contentLength, boolean attachment) throws IOException {
    ExternalContext externalContext = context.getExternalContext();
    externalContext.setResponseBufferSize(10240);
    externalContext.setResponseContentType(getMimeType(context, filename));
    externalContext.setResponseHeader("Content-Disposition", String.format("%s;filename="%2$s"; filename*=UTF-8''%2$s", new Object[]{attachment ? "attachment" : "inline", encodeURL(filename)}));
    if (((HttpServletRequest) externalContext.getRequest()).isSecure()) {
        externalContext.setResponseHeader("Cache-Control", "public");
        externalContext.setResponseHeader("Pragma", "public");
    }
    if (contentLength != -1L) {
        externalContext.setResponseHeader("Content-Length", String.valueOf(contentLength));
    }
    long size = stream(input, externalContext.getResponseOutputStream());
    if (contentLength == -1L) {
        externalContext.setResponseHeader("Content-Length", String.valueOf(size));
    }
    context.responseComplete();
}

public static String getMimeType(FacesContext context, String name) {
    String mimeType = context.getExternalContext().getMimeType(name);
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }
    return mimeType;
}
public static long stream(InputStream input, OutputStream output) throws IOException {

    ReadableByteChannel inputChannel = Channels.newChannel(input);
    Throwable var3 = null;
    try {
        WritableByteChannel outputChannel = Channels.newChannel(output);
        Throwable var5 = null;
        try {
            ByteBuffer buffer = ByteBuffer.allocateDirect(10240);
            long size = 0L;
            while (inputChannel.read(buffer) != -1) {
                buffer.flip();
                size += (long) outputChannel.write(buffer);
                buffer.clear();
            }
            long var9 = size;
            return var9;
        } catch (Throwable var33) {
            var5 = var33;
            throw var33;
        } finally {
            if (outputChannel != null) {
                if (var5 != null) {
                    try {
                        outputChannel.close();
                    } catch (Throwable var32) {
                        var5.addSuppressed(var32);
                    }
                } else {
                    outputChannel.close();
                }
            }
        }
    } catch (Throwable var35) {
        var3 = var35;
        throw var35;
    } finally {
        if (inputChannel != null) {
            if (var3 != null) {
                try {
                    inputChannel.close();
                } catch (Throwable var31) {
                    var3.addSuppressed(var31);
                }
            } else {
                inputChannel.close();
            }
        }
    }
}
public static String encodeURL(String string) {
    if (string == null) {
        return null;
    } else {
        try {
            return URLEncoder.encode(string, StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException var2) {
            throw new UnsupportedOperationException("UTF-8 is apparently not supported on this platform.", var2);
        }
    }
}

}

我无法理解的是,当下载是由本地chrome下载没有使用任何下载管理器如IDM或eagleget,我没有得到任何ClientAbortException,但当我使用这些下载管理器软件(启用他们的插件)我得到这些错误

发生了什么?我知道这个错误发生与一些连接丢失…但是我没有关闭我的页面或任何导致这个错误的事情!这是我的bean代码:

@ManagedBean(name = "bean")
@RequestScoped
public class MB implements Serializable {
public void MBdowan() throws IOException {

    File file = new File("E:\Animation\IA\Learning movies\webinar1\01_Aug_webinar_08\Aug08_edited_webinar_animation.mov");
    FileUtil.sendFile(file,true);

}

这是我的XHTML页面:

</h:head>
<h:body>
    <h:form>
        <p:commandButton value="Download file" ajax="false" actionListener="#{bean.MBdowan}"/>
    </h:form>
</h:body>

下载加速器(和媒体播放器!)期望文件通过GET和HEAD请求(即,当只是在浏览器的地址栏中输入URL时)是等价的,并且最好也支持HTTP Range请求(因此可以打开多个HTTP连接来同时下载部分)。JSF支持bean方法仅在POST请求时调用(例如,当提交带有method="post"的HTML表单时)。ClientAbortException的发生是因为下载加速器在嗅探HEAD和Range支持时没有得到预期的响应并中止了它。

如果这些文件是静态的,因此不是动态的,那么您最好的选择是创建一个单独的servlet,它支持HEAD,最好也支持HTTP Range请求。

既然您清楚地从OmniFaces Faces#sendFile()中窃取了源代码,那么我建议从另一个OmniFaces工件FileServlet中窃取源代码。您可以在这里找到快照展示和源代码链接:OmniFaces (2.2) FileServlet .

你可以这样使用它:

@WebServlet("/webinar_animation.mov")
public class YourFileServlet extends FileServlet {
    @Override
    protected File getFile(HttpServletRequest request) throws IllegalArgumentException {
        return new File("E:\Animation\IA\Learning movies\webinar1\01_Aug_webinar_08\Aug08_edited_webinar_animation.mov");
    }
}
<a href="#{request.contextPath}/webinar_animation.mov">Download file</a>

参见:

  • 如何使用Servlet流式传输MP3, MP4, AVI等音频/视频文件