我使用JSF并希望在我的页面中下载文件。我写了一些代码,但是当我使用一些下载管理器下载我的文件时,我得到ClientAbortException错误:
=DSUM(J3:L55, "Charge", {{"Category";"Coffee"},{"Split";"Yes"}})
}
我无法理解的是当下载是通过本机chorome下载完成而不使用任何下载管理器(如IDM或eagleget)时,我没有得到任何ClientAbortException,但是当我使用这些下载管理器软件(启用它们的AddOns)时我收到这些错误
会发生什么?我知道这个错误发生在一些连接丢失...但我没有关闭我的页面或任何导致此错误的事情! 这是我的bean代码:
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);
}
}
}
}
这是我的xhtml页面:
@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);
答案 0 :(得分:2)
下载加速器(和媒体播放器!)通过GET和HEAD请求(即只需在浏览器的地址栏中键入URL),并且最好还支持HTTP Range
请求时,可以预期文件可以通过...可以打开多个HTTP连接以同时下载部件。仅在POST请求时调用JSF支持bean方法(即,在提交带有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>