From 8b3658143ad9a8ce0fbbe9a75a8df91b213c7560 Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Mon, 25 Sep 2017 11:40:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHttpResponse.finishFile?= =?UTF-8?q?=E4=BC=A0=E8=BE=93=E5=81=B6=E5=B0=94=E5=BC=82=E5=B8=B8=E7=9A=84?= =?UTF-8?q?BUG?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/net/http/HttpResponse.java | 41 ++++++++++++---------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/org/redkale/net/http/HttpResponse.java b/src/org/redkale/net/http/HttpResponse.java index a11e4605c..7729d526a 100644 --- a/src/org/redkale/net/http/HttpResponse.java +++ b/src/org/redkale/net/http/HttpResponse.java @@ -1026,54 +1026,59 @@ public class HttpResponse extends Response { private long count;//读取文件的字节数 - private long position = 0; + private long readpos = 0; - private boolean next = false; + private boolean hdwrite = true; //写入Header - private boolean read = true; + private boolean read = false; public TransferFileHandler(File file) throws IOException { this.file = file; this.filechannel = AsynchronousFileChannel.open(file.toPath(), options, ((HttpContext) context).getExecutor()); - this.position = 0; + this.readpos = 0; this.max = file.length(); } public TransferFileHandler(File file, long offset, long len) throws IOException { this.file = file; this.filechannel = AsynchronousFileChannel.open(file.toPath(), options, ((HttpContext) context).getExecutor()); - this.position = offset <= 0 ? 0 : offset; + this.readpos = offset <= 0 ? 0 : offset; this.max = len <= 0 ? file.length() : len; } @Override public void completed(Integer result, ByteBuffer attachment) { - //(Thread.currentThread().getName() + "-----------" + file + "-------------------result: " + result + ", max = " + max + ", count = " + count); + //(Utility.now() + "---" + Thread.currentThread().getName() + "-----------" + file + "-------------------result: " + result + ", max = " + max + ", readpos = " + readpos + ", count = " + count + ", " + (hdwrite ? "正在写Header" : (read ? "准备读" : "准备写"))); if (result < 0 || count >= max) { failed(null, attachment); return; } - if (!next && attachment.hasRemaining()) { //Header还没写完 + if (hdwrite && attachment.hasRemaining()) { //Header还没写完 + channel.write(attachment, attachment, this); + return; + } + if (hdwrite) { + //(Utility.now() + "---" + Thread.currentThread().getName() + "-----------" + file + "-------------------Header写入完毕, 准备读取文件."); + hdwrite = false; + read = true; + result = 0; + } + if (read) { + count += result; + } else { + readpos += result; + } + if (read && attachment.hasRemaining()) { //Buffer还没写完 channel.write(attachment, attachment, this); return; } - if (next && read && attachment.hasRemaining()) { //Buffer还没写完 - channel.write(attachment, attachment, this); - return; - } if (read) { read = false; - if (next) { - position += result; - } else { - next = true; - } attachment.clear(); - filechannel.read(attachment, position, attachment, this); + filechannel.read(attachment, readpos, attachment, this); } else { read = true; - count += result; if (count > max) { attachment.limit((int) (attachment.position() + max - count)); }