HttpResourceServlet
This commit is contained in:
@@ -250,7 +250,7 @@ public class HttpResourceServlet extends HttpServlet {
|
|||||||
if (watchThread == null && files.isEmpty()) {
|
if (watchThread == null && files.isEmpty()) {
|
||||||
entry = createFileEntry(uri);
|
entry = createFileEntry(uri);
|
||||||
} else { //有缓存
|
} else { //有缓存
|
||||||
entry = files.computeIfAbsent(uri, x -> createFileEntry(x));
|
entry = files.computeIfAbsent(uri, this::createFileEntry);
|
||||||
}
|
}
|
||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
if (logger.isLoggable(Level.FINER)) {
|
if (logger.isLoggable(Level.FINER)) {
|
||||||
@@ -259,8 +259,8 @@ public class HttpResourceServlet extends HttpServlet {
|
|||||||
finish404(request, response);
|
finish404(request, response);
|
||||||
} else {
|
} else {
|
||||||
//file = null 表示资源内容在内存而不是在File中
|
//file = null 表示资源内容在内存而不是在File中
|
||||||
//file = null 时必须传 filename
|
//file = null 时必须传 fileName
|
||||||
response.finishFile(entry.file == null ? entry.filename : null, entry.file, entry.content);
|
response.finishFile(entry.file == null ? entry.fileName : null, entry.file, entry.content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,7 +291,7 @@ public class HttpResourceServlet extends HttpServlet {
|
|||||||
|
|
||||||
protected static class FileEntry {
|
protected static class FileEntry {
|
||||||
|
|
||||||
protected final String filename;
|
protected final String fileName;
|
||||||
|
|
||||||
protected final File file; //如果所有资源文件打包成zip文件则file=null
|
protected final File file; //如果所有资源文件打包成zip文件则file=null
|
||||||
|
|
||||||
@@ -303,14 +303,14 @@ public class HttpResourceServlet extends HttpServlet {
|
|||||||
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();
|
this.fileName = file.getName();
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileEntry(final HttpResourceServlet servlet, String filename, ByteArray content) {
|
public FileEntry(final HttpResourceServlet servlet, String fileName, ByteArray content) {
|
||||||
this.servlet = servlet;
|
this.servlet = servlet;
|
||||||
this.file = null;
|
this.file = null;
|
||||||
this.filename = filename;
|
this.fileName = fileName;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.servlet.cachedLength.add(this.content.length());
|
this.servlet.cachedLength.add(this.content.length());
|
||||||
}
|
}
|
||||||
@@ -324,7 +324,7 @@ public class HttpResourceServlet extends HttpServlet {
|
|||||||
}
|
}
|
||||||
this.servlet = servlet;
|
this.servlet = servlet;
|
||||||
this.file = null;
|
this.file = null;
|
||||||
this.filename = filename;
|
this.fileName = filename;
|
||||||
this.content = out;
|
this.content = out;
|
||||||
this.servlet.cachedLength.add(this.content.length());
|
this.servlet.cachedLength.add(this.content.length());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1210,15 +1210,15 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
/**
|
/**
|
||||||
* 将指定文件句柄或文件内容按指定文件名输出,若fileBody不为null则只输出fileBody内容
|
* 将指定文件句柄或文件内容按指定文件名输出,若fileBody不为null则只输出fileBody内容
|
||||||
* file 与 fileBody 不能同时为空
|
* file 与 fileBody 不能同时为空
|
||||||
* file 与 filename 也不能同时为空
|
* file 与 fileName 也不能同时为空
|
||||||
*
|
*
|
||||||
* @param filename 输出文件名
|
* @param fileName 输出文件名
|
||||||
* @param file 输出文件
|
* @param file 输出文件
|
||||||
* @param fileBody 文件内容, 没有则输出file
|
* @param fileBody 文件内容, 没有则输出file
|
||||||
*
|
*
|
||||||
* @throws IOException IO异常
|
* @throws IOException IO异常
|
||||||
*/
|
*/
|
||||||
protected void finishFile(final String filename, final File file, ByteArray fileBody) throws IOException {
|
protected void finishFile(final String fileName, final File file, ByteArray fileBody) throws IOException {
|
||||||
if ((file == null || !file.isFile() || !file.canRead()) && fileBody == null) {
|
if ((file == null || !file.isFile() || !file.canRead()) && fileBody == null) {
|
||||||
finish404();
|
finish404();
|
||||||
return;
|
return;
|
||||||
@@ -1231,14 +1231,12 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
|
|||||||
//return;
|
//return;
|
||||||
}
|
}
|
||||||
this.contentLength = length;
|
this.contentLength = length;
|
||||||
if (filename != null && !filename.isEmpty() && file != null) {
|
if (Utility.isNotEmpty(fileName) && file != null) {
|
||||||
if (this.header.getValue("Content-Disposition") == null) {
|
if (this.header.getValue("Content-Disposition") == null) {
|
||||||
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
|
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (this.contentType == null) {
|
this.contentType = MimeType.getByFilename(Utility.isEmpty(fileName) && file != null ? file.getName() : fileName);
|
||||||
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
|
|
||||||
}
|
|
||||||
if (this.contentType == null) {
|
if (this.contentType == null) {
|
||||||
this.contentType = "application/octet-stream";
|
this.contentType = "application/octet-stream";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user