gpt4 book ai didi

Java 仅使用 Java api 在没有 servlet 的情况下实现字节范围服务

转载 作者:可可西里 更新时间:2023-11-01 16:30:11 24 4
gpt4 key购买 nike

我用 Java 编写 http 服务器已经有一段时间了(现在快两年了?),但我仍然无法让字节范围请求正常工作。我只使用套接字的输入和输出流进行原始字节数据传输(即文件下载/上传),以及 PrintWriter 将响应 header /字符串发送到连接客户端(例如 HTTP/1.1 200 OK 等等)。

我不想使用任何 servlet 或 api(例如 HTTPURLConnection 或其他)。我想做“ Vanilla 风格”。

我可以很好很快速地服务于正常的网页(比如文件浏览、上传和下载、看电影、听音乐、查看pdf文件、文本、图片、gif文件等),所以这是不是问题。

但是,每当我尝试实现字节范围请求时,服务器都会完美地接收客户端的请求,解析给定的数据,准备要发送的文件输入流,然后当我将数据发送给客户端时,客户端总是丢弃与软件的连接导致连接中止:套接字写入错误。(我需要字节范围请求来处理以下情况:观看一个小时长的视频,然后该死的 wifi 信号消失,你不想重新-从第一个方 block 开始观看整个视频,恢复“暂停的下载”等)

这真的让我很困惑,我确实搜索了服务字节范围请求的 java 示例,但当我尝试实现它们时,所有这些示例都失败了。我什至尝试过从头开始并测试它,它产生了相同的结果。以下是与我要实现的目标相关的代码片段:

发送和接收正常的网页(工作正常,这是一个例子):

    ...
OutputStream outStream = client.getOutputStream();
PrintWriter out = new PrintWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8), true);
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html; charset=\"UTF-8\"");
String responseStr = "<!DOCTYPE html><html><head><title>Hello, world!</title></head><body><string>This is a simple example webpage!</string></body></html>";
if(acceptEncoding.contains("gzip") && responseStr.length() > 33) {
out.println("Content-Encoding: gzip");
byte[] r = StringUtil.compressString(responseStr, "UTF-8");
out.println("Content-Length: " + r.length);
out.println("");
if(protocol.equalsIgnoreCase("GET")) {
outStream.write(r);
}
} else {
out.println("Content-Length: " + responseStr.length());
out.println("");
if(request.protocol.equalsIgnoreCase("GET")) {
out.println(responseStr);
}
}
out.flush();
//followed by closing resources, etc.
...

服务字节范围(解析客户端请求数据后):

public static final long copyInputStreamToOutputStream(InputStream input, OutputStream output, long startBytes, long endBytes) throws IOException {
byte[] buffer = new byte[20480];//1024
long count = 0;
int read;

input.skip(startBytes);
long toRead = (endBytes - startBytes) + 1;

while((read = input.read(buffer)) > 0) {
if((toRead -= read) > 0) {
output.write(buffer, 0, read);//Socket error happens on this line mostly
output.flush();
} else {
output.write(buffer, 0, (int) toRead + read);//but also on this line sometimes
output.flush();
break;
}
count += read;
}
return count;
}

对于任何感兴趣的人,运行在这个基本代码上的服务器在 redsandbox.no-ip.org 上在线(指向我的服务器),目前我用 Accept-Ranges 禁用了字节请求: none 而不是 Accept-Ranges: bytes,但我可以再次打开它进行测试。

如果我需要添加更多代码,请告诉我!感谢您的时间。或者,如果您想查看我服务器的完整代码,可以在 github 上查看: https://github.com/br45entei/JavaWebServer

最佳答案

这个读写循环能正常工作吗,看起来很流线型?这是我的头脑,所以可能会发现轻微的语法错误。我希望 startIdx+endIdx 都是包容性指标。

public static final long copyInputToOutput(InputStream input, OutputStream output, long startIdx, long endIdx) throws IOException {
final long maxread = 24*1024;
byte[] buffer = new byte[(int)maxread];
input.skip(startIdx);
long written=0;
long remaining = endIdx-startIdx+1;
while(remaining>0) {
int read = input.read(buffer, 0, (int)Math.min(maxread, remaining));
output.write(buffer, 0, read);
remaining -= read;
written += read;
}
output.flush();
return written;
}

关于Java 仅使用 Java api 在没有 servlet 的情况下实现字节范围服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33181702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com