This commit is contained in:
RedKale
2016-04-19 09:56:11 +08:00
parent c85fc73e02
commit c7e0d93727

View File

@@ -7,7 +7,7 @@ package org.redkale.net.http;
import java.io.*; import java.io.*;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.net.HttpCookie; import java.net.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.*; import java.nio.channels.*;
import java.nio.file.*; import java.nio.file.*;
@@ -24,7 +24,7 @@ import org.redkale.util.*;
* Http响应包 与javax.servlet.http.HttpServletResponse 基本类似。 * Http响应包 与javax.servlet.http.HttpServletResponse 基本类似。
* 同时提供发送json的系列接口: public void finishJson(Type type, Object obj) * 同时提供发送json的系列接口: public void finishJson(Type type, Object obj)
* RedKale提倡http+json的接口风格 所以主要输出的数据格式为json 同时提供异步接口。 * RedKale提倡http+json的接口风格 所以主要输出的数据格式为json 同时提供异步接口。
* * <p>
* <p> * <p>
* 详情见: http://redkale.org * 详情见: http://redkale.org
* *
@@ -35,7 +35,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
/** /**
* HttpResponse.finish 方法内调用 * HttpResponse.finish 方法内调用
* 主要给@HttpCacheable使用 * 主要给@HttpCacheable使用
* * <p>
*/ */
protected static interface BufferHandler { protected static interface BufferHandler {
@@ -302,7 +302,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
/** /**
* 以304状态码输出 * 以304状态码输出
* * <p>
*/ */
public void finish304() { public void finish304() {
super.finish(buffer304.duplicate()); super.finish(buffer304.duplicate());
@@ -310,7 +310,7 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
/** /**
* 以404状态码输出 * 以404状态码输出
* * <p>
*/ */
public void finish404() { public void finish404() {
super.finish(buffer404.duplicate()); super.finish(buffer404.duplicate());
@@ -422,7 +422,18 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @throws IOException IO异常 * @throws IOException IO异常
*/ */
public void finish(File file) throws IOException { public void finish(File file) throws IOException {
finishFile(file, null); finishFile(null, file, null);
}
/**
* 将文件按指定文件名输出
*
* @param filename 输出文件名
* @param file 输出文件
* @throws IOException IO异常
*/
public void finish(final String filename, File file) throws IOException {
finishFile(filename, file, null);
} }
/** /**
@@ -433,6 +444,18 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
* @throws IOException IO异常 * @throws IOException IO异常
*/ */
protected void finishFile(final File file, ByteBuffer fileBody) throws IOException { protected void finishFile(final File file, ByteBuffer fileBody) throws IOException {
finishFile(null, file, fileBody);
}
/**
* 将指定文件句柄或文件内容按指定文件名输出若fileBody不为null则只输出fileBody内容
*
* @param filename 输出文件名
* @param file 输出文件
* @param fileBody 文件内容, 没有则输出file
* @throws IOException IO异常
*/
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()) {
finish404(); finish404();
return; return;
@@ -445,7 +468,10 @@ public class HttpResponse extends Response<HttpContext, HttpRequest> {
return; return;
} }
this.contentLength = fileBody == null ? file.length() : fileBody.remaining(); this.contentLength = fileBody == null ? file.length() : fileBody.remaining();
this.contentType = MimeType.getByFilename(file.getName()); if (filename != null && !filename.isEmpty()) {
addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
}
this.contentType = MimeType.getByFilename(filename == null || filename.isEmpty() ? file.getName() : filename);
if (this.contentType == null) this.contentType = "application/octet-stream"; if (this.contentType == null) this.contentType = "application/octet-stream";
String range = request.getHeader("Range"); String range = request.getHeader("Range");
if (range != null && (!range.startsWith("bytes=") || range.indexOf(',') >= 0)) range = null; if (range != null && (!range.startsWith("bytes=") || range.indexOf(',') >= 0)) range = null;