public class HttpServerResponse extends HttpServerBase
Constructor and Description |
---|
HttpServerResponse(com.sun.net.httpserver.HttpExchange httpExchange)
构造
|
Modifier and Type | Method and Description |
---|---|
HttpServerResponse |
addHeader(String header,
String value)
添加响应头,如果已经存在,则追加
|
com.sun.net.httpserver.Headers |
getHeaders()
获得所有响应头,获取后可以添加新的响应头
|
OutputStream |
getOut()
获取响应数据流
|
PrintWriter |
getWriter()
获取响应数据流
|
HttpServerResponse |
send(int httpStatusCode)
发送HTTP状态码,Content-Length为0不定长度,会输出Transfer-encoding: chunked
|
HttpServerResponse |
send(int httpStatusCode,
long bodyLength)
发送HTTP状态码
|
HttpServerResponse |
send404(String content)
发送404错误页
|
HttpServerResponse |
sendError(int errorCode,
String content)
发送错误页
|
HttpServerResponse |
sendOk()
发送成功状态码
|
HttpServerResponse |
sendOk(int bodyLength)
发送成功状态码
|
HttpServerResponse |
setAttr(String name,
Object value)
设置属性
|
HttpServerResponse |
setCharset(Charset charset)
设置响应的编码
|
HttpServerResponse |
setContentLength(long contentLength)
设置Content-Length头
|
HttpServerResponse |
setContentType(String contentType)
设置Content-Type头,类似于:text/html;charset=utf-8
如果用户传入的信息无charset信息,自动根据charset补充,charset设置见 setCharset(Charset) |
HttpServerResponse |
setHeader(Header header,
String value)
设置响应头,如果已经存在,则覆盖
|
HttpServerResponse |
setHeader(String header,
List<String> value)
设置响应头,如果已经存在,则覆盖
|
HttpServerResponse |
setHeader(String header,
String value)
设置响应头,如果已经存在,则覆盖
|
HttpServerResponse |
setHeaders(Map<String,List<String>> headers)
设置所有响应头,如果已经存在,则覆盖
|
HttpServerResponse |
write(byte[] data)
写出数据到客户端
|
HttpServerResponse |
write(byte[] data,
String contentType)
写出数据到客户端
|
HttpServerResponse |
write(File file)
返回文件给客户端(文件下载)
|
HttpServerResponse |
write(File file,
String fileName)
返回文件给客户端(文件下载)
|
HttpServerResponse |
write(InputStream in)
写出数据到客户端
|
HttpServerResponse |
write(InputStream in,
int length)
写出数据到客户端
|
HttpServerResponse |
write(InputStream in,
int length,
String contentType)
返回数据给客户端
|
HttpServerResponse |
write(InputStream in,
int length,
String contentType,
String fileName)
返回文件数据给客户端(文件下载)
|
HttpServerResponse |
write(InputStream in,
String contentType)
返回数据给客户端
|
void |
write(InputStream in,
String contentType,
String fileName)
返回文件数据给客户端(文件下载)
|
HttpServerResponse |
write(String data)
写出数据到客户端
|
HttpServerResponse |
write(String data,
String contentType)
写出数据到客户端
|
close, getHttpContext, getHttpExchange
public HttpServerResponse(com.sun.net.httpserver.HttpExchange httpExchange)
httpExchange
- HttpExchange
public HttpServerResponse send(int httpStatusCode)
httpStatusCode
- HTTP状态码,见HttpStatuspublic HttpServerResponse sendOk()
public HttpServerResponse sendOk(int bodyLength)
bodyLength
- 响应体长度,默认0表示不定长度,会输出Transfer-encoding: chunkedpublic HttpServerResponse send404(String content)
content
- 错误页页面内容,默认text/html类型public HttpServerResponse sendError(int errorCode, String content)
errorCode
- HTTP错误状态码,见HttpStatuscontent
- 错误页页面内容,默认text/html类型public HttpServerResponse send(int httpStatusCode, long bodyLength)
httpStatusCode
- HTTP状态码,见HttpStatusbodyLength
- 响应体长度,默认0表示不定长度,会输出Transfer-encoding: chunkedpublic com.sun.net.httpserver.Headers getHeaders()
public HttpServerResponse addHeader(String header, String value)
header
- 头keyvalue
- 值public HttpServerResponse setHeader(Header header, String value)
header
- 头keyvalue
- 值public HttpServerResponse setHeader(String header, String value)
header
- 头keyvalue
- 值public HttpServerResponse setHeader(String header, List<String> value)
header
- 头keyvalue
- 值列表public HttpServerResponse setHeaders(Map<String,List<String>> headers)
headers
- 响应头mappublic HttpServerResponse setContentType(String contentType)
setCharset(Charset)
contentType
- Content-Type头内容public HttpServerResponse setContentLength(long contentLength)
contentLength
- Content-Length头内容public HttpServerResponse setCharset(Charset charset)
charset
- 编码public HttpServerResponse setAttr(String name, Object value)
name
- 属性名value
- 属性值public OutputStream getOut()
public PrintWriter getWriter()
public HttpServerResponse write(String data, String contentType)
data
- 数据contentType
- Content-Type类型public HttpServerResponse write(String data)
data
- 数据public HttpServerResponse write(byte[] data, String contentType)
data
- 数据contentType
- 返回的类型public HttpServerResponse write(byte[] data)
data
- 数据public HttpServerResponse write(InputStream in, String contentType)
in
- 需要返回客户端的内容contentType
- 返回的类型public HttpServerResponse write(InputStream in, int length, String contentType)
in
- 需要返回客户端的内容length
- 内容长度,默认0表示不定长度,会输出Transfer-encoding: chunkedcontentType
- 返回的类型public HttpServerResponse write(InputStream in)
in
- 数据流public HttpServerResponse write(InputStream in, int length)
in
- 数据流length
- 指定响应内容长度,默认0表示不定长度,会输出Transfer-encoding: chunkedpublic HttpServerResponse write(File file)
file
- 写出的文件对象public HttpServerResponse write(File file, String fileName)
file
- 写出的文件对象fileName
- 文件名public void write(InputStream in, String contentType, String fileName)
in
- 需要返回客户端的内容contentType
- 返回的类型fileName
- 文件名public HttpServerResponse write(InputStream in, int length, String contentType, String fileName)
in
- 需要返回客户端的内容length
- 长度contentType
- 返回的类型fileName
- 文件名Copyright © 2024. All rights reserved.