如何使用Undertow返回要在Java中下载的文件?

如何使用Undertow返回要在Java中下载的文件?,java,web,undertow,Java,Web,Undertow,我正在尝试允许我的游戏客户端下载客户端运行所需的缓存。在我的游戏web服务器中,我正在执行以下操作: @RouteManifest(template="/cache", method="GET") public class APICacheRoute extends RouteHttpHandler<JadePreprocessor> { @Override public void handleRequest(HttpServerExchange exchange)

我正在尝试允许我的游戏客户端下载客户端运行所需的缓存。在我的游戏web服务器中,我正在执行以下操作:

@RouteManifest(template="/cache", method="GET")
public class APICacheRoute extends RouteHttpHandler<JadePreprocessor> {
    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        Path path = Paths.get("./cache/Alterscape.zip");
        File file = path.toFile();
        exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/octet-stream");
        exchange.getResponseSender().send(file);
    }
}
我的APIVirtualHost如下所示:

public static void initialize() {
    ServerController server = new ServerController("localhost", 8080, 8443);
    server.register(new APIVirtualHost());
    server.inititialize();
}
public APIVirtualHost() {
    super(127.0.0.1);
    setDirectoryListingEnabled(false);
}

使用Undertow时,需要将处理程序从I/O线程中分派出来,以使用阻塞操作。在需要执行阻塞操作的任何时候,都需要执行此操作,例如读取请求正文、接受文件上载流,当然还有发送文件

在handle()方法的顶部,使用此命令将请求分派到阻塞线程,并退出非阻塞I/O线程:

if (serverExchange.isInIoThread()) {
    serverExchange.dispatch(this);
    return;
}
然后在准备发送缓冲区时开始阻塞:

serverExchange.startBlocking();
然后,要发送文件,可以使用缓冲流:

serverExchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/octet-stream");

final File file = new File("/location/to/your/file");
final OutputStream outputStream = serverExchange.getOutputStream();
final InputStream inputStream = new FileInputStream(file);

byte[] buf = new byte[8192];
int c;
while ((c = inputStream.read(buf, 0, buf.length)) > 0) {
    outputStream.write(buf, 0, c);
    outputStream.flush();
}

outputStream.close();
inputStream.close();
ServerExchange
对象上的
send
方法用于使用非阻塞方法发送响应数据-适用于文本或JSON。
ByteBuffer
参数重载用于发送原始字节数据,这是Undertow在发送时将字符串转换为的数据。这可能有点误导


愉快的编码。

尝试用这个
io.undertow.server.handlers.BlockingHandler来包装您自己的处理程序。在执行阻塞操作之前,它已经包含了必要的检查

public final class BlockingHandler implements HttpHandler {
    ...
    @Override
    public void handleRequest(final HttpServerExchange exchange) throws Exception {

        exchange.startBlocking();
        if (exchange.isInIoThread()) {
            exchange.dispatch(handler);
        } else {
            handler.handleRequest(exchange);
        }
    }
    ...
}