下面列出了怎么用org.eclipse.jetty.server.HttpOutput的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void afterCompletion(
HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws java.io.IOException {
String emailUser = request.getUserPrincipal().getName();
ServiceResponse<User> serviceResponse = userService.findByEmail(emailUser);
String url = request.getRequestURL().toString();
HttpOutput out = (HttpOutput) response.getOutputStream();
if (serviceResponse.isOk()) {
LOGGER.info(MessageFormat.format("{0} {1} {2} {3}",
serviceResponse.getResult().getTenant().getDomainName(),
url,
response.getStatus(),
out.getWritten()));
}
}
@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
ByteBuffer input = dataBuffer.asByteBuffer();
int len = input.remaining();
ServletResponse response = getNativeResponse();
((HttpOutput) response.getOutputStream()).write(input);
return len;
}
@Override
protected int writeToOutputStream(DataBuffer dataBuffer) throws IOException {
ByteBuffer input = dataBuffer.asByteBuffer();
int len = input.remaining();
ServletResponse response = getNativeResponse();
((HttpOutput) response.getOutputStream()).write(input);
return len;
}
/**
* @param response Http response.
* @param type Type.
* @param path Path to file.
* @throws IOException If failed.
*/
private static void handleRequest(HttpServletResponse response, String type, String path) throws IOException {
Path path0 = Paths.get(path);
response.setContentType(type);
response.setHeader("Content-Disposition", "attachment; filename=\"" + path0.getFileName() + "\"");
try (HttpOutput out = (HttpOutput)response.getOutputStream()) {
out.sendContent(FileChannel.open(path0, StandardOpenOption.READ));
}
}
/**
* @param response Http response.
* @param type Type.
* @param stream Stream.
* @param attachmentName Attachment name.
* @throws IOException If failed.
*/
private static void handleRequest(HttpServletResponse response, String type, InputStream stream,
String attachmentName) throws IOException {
response.setContentType(type);
response.setHeader("Content-Disposition", "attachment; filename=\"" + attachmentName + "\"");
try (HttpOutput out = (HttpOutput)response.getOutputStream()) {
out.sendContent(stream);
}
}
private OutputStream wrapOutput(OutputStream out) {
try {
if (out instanceof HttpOutput) {
out = new JettyOutputStream((HttpOutput)out);
}
} catch (Throwable t) {
//ignore
}
return out;
}
JettyOutputStream(HttpOutput o) {
super(o);
out = o;
}