This commit is contained in:
@@ -62,7 +62,7 @@ public final class HttpResourceServlet extends HttpServlet {
|
|||||||
files.remove(uri);
|
files.remove(uri);
|
||||||
} else if (event.kind() == ENTRY_MODIFY) {
|
} else if (event.kind() == ENTRY_MODIFY) {
|
||||||
FileEntry en = files.get(uri);
|
FileEntry en = files.get(uri);
|
||||||
if (en != null) {
|
if (en != null && en.file != null) {
|
||||||
long d; //等待update file完毕
|
long d; //等待update file完毕
|
||||||
for (;;) {
|
for (;;) {
|
||||||
d = en.file.lastModified();
|
d = en.file.lastModified();
|
||||||
@@ -205,7 +205,9 @@ public final class HttpResourceServlet extends HttpServlet {
|
|||||||
if (finest) logger.log(Level.FINEST, "Not found resource (404), request = " + request);
|
if (finest) logger.log(Level.FINEST, "Not found resource (404), request = " + request);
|
||||||
response.finish404();
|
response.finish404();
|
||||||
} else {
|
} else {
|
||||||
response.finishFile(entry.file, entry.content);
|
//file = null 表示资源内容在内存而不是在File中
|
||||||
|
//file = null 时必须传 filename
|
||||||
|
response.finishFile(entry.file == null ? entry.filename : null, entry.file, entry.content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,19 +230,30 @@ public final class HttpResourceServlet extends HttpServlet {
|
|||||||
|
|
||||||
protected static class FileEntry {
|
protected static class FileEntry {
|
||||||
|
|
||||||
protected final File file;
|
protected final String filename;
|
||||||
|
|
||||||
private final HttpResourceServlet servlet;
|
protected final File file; //如果所有资源文件打包成zip文件则file=null
|
||||||
|
|
||||||
ByteBuffer content;
|
protected final HttpResourceServlet servlet;
|
||||||
|
|
||||||
|
protected ByteBuffer content;
|
||||||
|
|
||||||
public FileEntry(final HttpResourceServlet servlet, File file) {
|
public FileEntry(final HttpResourceServlet servlet, File file) {
|
||||||
this.servlet = servlet;
|
this.servlet = servlet;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
|
this.filename = file.getName();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public FileEntry(final HttpResourceServlet servlet, String filename, ByteBuffer content) {
|
||||||
|
this.servlet = servlet;
|
||||||
|
this.file = null;
|
||||||
|
this.filename = filename;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
|
if (this.file == null) return;
|
||||||
if (this.content != null) {
|
if (this.content != null) {
|
||||||
this.servlet.cachedLength.add(0L - this.content.remaining());
|
this.servlet.cachedLength.add(0L - this.content.remaining());
|
||||||
this.content = null;
|
this.content = null;
|
||||||
|
|||||||
@@ -525,6 +525,8 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 将指定文件句柄或文件内容按指定文件名输出,若fileBody不为null则只输出fileBody内容
|
* 将指定文件句柄或文件内容按指定文件名输出,若fileBody不为null则只输出fileBody内容
|
||||||
|
* file 与 fileBody 不能同时为空
|
||||||
|
* file 与 filename 也不能同时为空
|
||||||
*
|
*
|
||||||
* @param filename 输出文件名
|
* @param filename 输出文件名
|
||||||
* @param file 输出文件
|
* @param file 输出文件
|
||||||
@@ -533,19 +535,20 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
* @throws IOException IO异常
|
* @throws IOException IO异常
|
||||||
*/
|
*/
|
||||||
protected void finishFile(final String filename, final File file, ByteBuffer fileBody) throws IOException {
|
protected void finishFile(final String filename, final File file, ByteBuffer fileBody) throws IOException {
|
||||||
if (file == null || !file.isFile() || !file.canRead()) {
|
if ((file == null || !file.isFile() || !file.canRead()) && fileBody == null) {
|
||||||
finish404();
|
finish404();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (fileBody != null) fileBody = fileBody.duplicate().asReadOnlyBuffer();
|
if (fileBody != null) fileBody = fileBody.duplicate().asReadOnlyBuffer();
|
||||||
final long length = file.length();
|
final long length = file == null ? fileBody.remaining() : file.length();
|
||||||
final String match = request.getHeader("If-None-Match");
|
final String match = request.getHeader("If-None-Match");
|
||||||
if (match != null && (file.lastModified() + "-" + length).equals(match)) {
|
final String etag = (file == null ? 0L : file.lastModified()) + "-" + length;
|
||||||
|
if (match != null && etag.equals(match)) {
|
||||||
finish304();
|
finish304();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.contentLength = fileBody == null ? file.length() : fileBody.remaining();
|
this.contentLength = length;
|
||||||
if (filename != null && !filename.isEmpty()) {
|
if (filename != null && !filename.isEmpty() && file != null) {
|
||||||
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
||||||
}
|
}
|
||||||
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
|
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
|
||||||
@@ -559,14 +562,14 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
int pos = range.indexOf('-');
|
int pos = range.indexOf('-');
|
||||||
start = pos == 0 ? 0 : Integer.parseInt(range.substring(0, pos));
|
start = pos == 0 ? 0 : Integer.parseInt(range.substring(0, pos));
|
||||||
long end = (pos == range.length() - 1) ? -1 : Long.parseLong(range.substring(pos + 1));
|
long end = (pos == range.length() - 1) ? -1 : Long.parseLong(range.substring(pos + 1));
|
||||||
long clen = end > 0 ? (end - start + 1) : (file.length() - start);
|
long clen = end > 0 ? (end - start + 1) : (length - start);
|
||||||
this.status = 206;
|
this.status = 206;
|
||||||
addHeader("Accept-Ranges", "bytes");
|
addHeader("Accept-Ranges", "bytes");
|
||||||
addHeader("Content-Range", "bytes " + start + "-" + (end > 0 ? end : length - 1) + "/" + length);
|
addHeader("Content-Range", "bytes " + start + "-" + (end > 0 ? end : length - 1) + "/" + length);
|
||||||
this.contentLength = clen;
|
this.contentLength = clen;
|
||||||
len = end > 0 ? clen : end;
|
len = end > 0 ? clen : end;
|
||||||
}
|
}
|
||||||
this.addHeader("ETag", file.lastModified() + "-" + length);
|
this.addHeader("ETag", etag);
|
||||||
ByteBuffer hbuffer = createHeader();
|
ByteBuffer hbuffer = createHeader();
|
||||||
hbuffer.flip();
|
hbuffer.flip();
|
||||||
if (fileBody == null) {
|
if (fileBody == null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user