发送文件作为响应而不下载它
Send file in response without downloading it
我尝试构建一个 http 服务器以使用 HLS 流式传输视频。我已经处理了如下响应。
private void handleResponse(HttpExchange exchange, String fileNameValue) {
OutputStream responseStream = exchange.getResponseBody();
File file = new File(fileNameValue);
try {
String encoding = "UTF-8";
String response = FileUtils.readFileToString(file, encoding);
exchange.getResponseHeaders().set("Content-Type", "application/x-mpegURL");
exchange.getResponseHeaders().set("Accept-Ranges", "bytes");
exchange.getResponseHeaders().set("Cache-Control", "max-age=0, no-cache, no-store");
exchange.sendResponseHeaders(200, response.length());
responseStream.write(response.getBytes());
responseStream.flush();
responseStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但是浏览器总是下载文件而不是播放文件。 VLC Media Player 也不播放它。
我想得到这样的结果。
你能告诉我怎么做吗?也感谢一些用于研究的关键字。
我发现他们的网站使用 xhr 发送请求,所以文件出现在网络部分。
之后我使用 hls.js 并得到相同的结果:)
我尝试构建一个 http 服务器以使用 HLS 流式传输视频。我已经处理了如下响应。
private void handleResponse(HttpExchange exchange, String fileNameValue) {
OutputStream responseStream = exchange.getResponseBody();
File file = new File(fileNameValue);
try {
String encoding = "UTF-8";
String response = FileUtils.readFileToString(file, encoding);
exchange.getResponseHeaders().set("Content-Type", "application/x-mpegURL");
exchange.getResponseHeaders().set("Accept-Ranges", "bytes");
exchange.getResponseHeaders().set("Cache-Control", "max-age=0, no-cache, no-store");
exchange.sendResponseHeaders(200, response.length());
responseStream.write(response.getBytes());
responseStream.flush();
responseStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但是浏览器总是下载文件而不是播放文件。 VLC Media Player 也不播放它。
我想得到这样的结果。
你能告诉我怎么做吗?也感谢一些用于研究的关键字。
我发现他们的网站使用 xhr 发送请求,所以文件出现在网络部分。
之后我使用 hls.js 并得到相同的结果:)