如何使用Undertow返回文件以Java下载?

时间:2019-03-08 17:11:26

标签: java web undertow

我正试图允许我的游戏客户端下载运行客户端所需的缓存。在我的游戏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);
    }
}

但是,我收到文件必须是ByteBuffer的错误。如何退回文件以下载?

我的Web服务器如下:

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

我的APIVirtualHost看起来像这样:

public APIVirtualHost() {
    super(127.0.0.1);
    setDirectoryListingEnabled(false);
}

2 个答案:

答案 0 :(得分:1)

对于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();

send对象上的ServerExchange方法用于使用非阻塞方法发送响应数据-适用于文本或JSON。 ByteBuffer参数重载用于发送原始字节数据,这是Undertow在发送时将字符串转换为的数据。可能会引起误解。

快乐编码。

答案 1 :(得分:0)

尝试使用此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);
        }
    }
    ...
}
相关问题