本文整理汇总了Java中javax.servlet.ServletOutputStream.close方法的典型用法代码示例。如果您正苦于以下问题:Java ServletOutputStream.close方法的具体用法?Java ServletOutputStream.close怎么用?Java ServletOutputStream.close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletOutputStream
的用法示例。
在下文中一共展示了ServletOutputStream.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doService
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
service.setRemoteHost(request.getRemoteHost());
InputStream inputStream = request.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[65536];
int numberOfBytes;
while ((numberOfBytes = inputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, numberOfBytes);
}
inputStream.close();
String responseBody = service.doRequest(byteArrayOutputStream.toString("UTF-8"));
byte[] responseBytes = responseBody.getBytes("UTF-8");
response.setContentType("text/plain; charset=UTF-8");
response.addHeader("Access-Control-Allow-Origin", "*");
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(responseBytes);
servletOutputStream.close();
}
示例2: doService
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public void doService(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
InputStream inputStream = request.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[65536];
int numberOfBytes;
while ((numberOfBytes = inputStream.read(buffer)) > 0) {
byteArrayOutputStream.write(buffer, 0, numberOfBytes);
}
inputStream.close();
String responseBody = service.doRequest(byteArrayOutputStream.toString("UTF-8"));
byte[] responseBytes = responseBody.getBytes("UTF-8");
response.setContentType("application/json; charset=UTF-8");
response.addHeader("Access-Control-Allow-Origin", "*");
ServletOutputStream servletOutputStream = response.getOutputStream();
servletOutputStream.write(responseBytes);
servletOutputStream.close();
}
示例3: viewXml
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
private void viewXml(RenderContext info, HttpServletResponse response) throws IOException
{
response.setContentType("text/xml");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
outputStream.println(itemXsltService.getXmlForXslt(info, AbstractParentViewItemSection.getItemInfo(info))
.toString());
outputStream.flush();
outputStream.close();
}
示例4: getNullImage
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@RequestMapping(value = "/webservice1_0bs/multimediaImage/null", method = RequestMethod.GET)
@ResponseBody
public void getNullImage(HttpServletResponse response) {
try {
byte[] b = new byte[0];
InputStream i = App.retGlobalResource("/src/main/ui/no_image_icon.png").openStream();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/" + "png");
ServletOutputStream responseOutputStream = response.getOutputStream();
byte[] bytes = new byte[1024];
int read = 0;
while ((read = i.read(bytes)) != -1) {
responseOutputStream.write(bytes, 0, read);
}
responseOutputStream.flush();
responseOutputStream.close();
} catch (IOException e1) {
log.error(e1);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (Exception e) {
log.error(e);
}
}
}
示例5: getImage
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@RequestMapping(value = "/webservice1_0bs/multimediaImage/{imageFile}.{ender}", method = RequestMethod.GET)
@ResponseBody
public void getImage(@PathVariable("imageFile") String imageFile, @PathVariable("ender") String end,
HttpServletResponse response) {
try {
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
File img = App.worldFileUtil.findMultimedia(imageFile + "." + end);
log.debug(img.getAbsolutePath());
log.debug(img.exists());
BufferedImage image = ImageIO.read(img);
ImageIO.write(image, end, jpegOutputStream);
byte[] imgByte = jpegOutputStream.toByteArray();
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/" + end);
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(imgByte);
responseOutputStream.flush();
responseOutputStream.close();
} catch (IOException e1) {
log.error(e1);
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (Exception e) {
log.error(e);
}
}
}
示例6: doGet
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/plain");
Enumeration<String> e = request.getHeaders("Accept-Encoding");
while (e.hasMoreElements()) {
String name = e.nextElement();
out.println(name);
if (name.indexOf("gzip") != -1) {
out.println("gzip supported -- able to compress");
}
else {
out.println("gzip not supported");
}
}
out.println("Compression Filter Test Servlet");
out.println("Minimum content length for compression is 128 bytes");
out.println("********** 32 bytes **********");
out.println("********** 32 bytes **********");
out.println("********** 32 bytes **********");
out.println("********** 32 bytes **********");
out.close();
}
开发者ID:sunmingshuai,项目名称:apache-tomcat-7.0.73-with-comment,代码行数:29,代码来源:CompressionFilterTestServlet.java
示例7: doTrace
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Called by the server (via the <code>service</code> method)
* to allow a servlet to handle a TRACE request.
*
* A TRACE returns the headers sent with the TRACE
* request to the client, so that they can be used in
* debugging. There's no need to override this method.
*
* @param req the {@link HttpServletRequest} object that
* contains the request the client made of
* the servlet
*
* @param resp the {@link HttpServletResponse} object that
* contains the response the servlet returns
* to the client
*
* @exception IOException if an input or output error occurs
* while the servlet is handling the
* TRACE request
*
* @exception ServletException if the request for the
* TRACE cannot be handled
*/
protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
int responseLength;
String CRLF = "\r\n";
StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI())
.append(" ").append(req.getProtocol());
Enumeration<String> reqHeaderEnum = req.getHeaderNames();
while( reqHeaderEnum.hasMoreElements() ) {
String headerName = reqHeaderEnum.nextElement();
buffer.append(CRLF).append(headerName).append(": ")
.append(req.getHeader(headerName));
}
buffer.append(CRLF);
responseLength = buffer.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(buffer.toString());
out.close();
return;
}
示例8: onAuthenticationFailure
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public void onAuthenticationFailure (HttpServletRequest request,
HttpServletResponse response, AuthenticationException exception)
throws IOException, ServletException
{
response.setContentType ("text/plain");
response.setStatus (HttpServletResponse.SC_UNAUTHORIZED);
ServletOutputStream outputStream = response.getOutputStream ();
outputStream.print (exception.getMessage ());
outputStream.close ();
}
示例9: execute
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
public String execute() throws Exception {
HttpServletRequest request = Struts2Utils.getRequest();
HttpServletResponse response = Struts2Utils.getResponse();
ByteArrayOutputStream out = null;
byte[] captchaChallengeAsJpeg = null;
// the output stream to render the captcha image as jpeg into
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// get the session id that will identify the generated captcha.
// the same id must be used to validate the response, the session id
// is a good candidate!
String captchaId = request.getSession().getId();
// call the ImageCaptchaService getChallenge method
BufferedImage challenge = imageCaptchaService.getImageChallengeForID(captchaId, request.getLocale());
// a jpeg encoder
/*** jdk1.7之后默认不支持了 **/
// JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
// jpegEncoder.encode(challenge);
// 换成新版图片api
ImageIO.write(challenge, "jpg", jpegOutputStream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
// flush it in the response
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
return null;
}
示例10: service
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
final String path = req.getPathInfo();
if( path.equals("/refresh") )
{
languageService.refreshBundles();
resp.setContentType("text/plain");
ServletOutputStream out = resp.getOutputStream();
out.print("Bundles refreshed");
out.close();
return;
}
Matcher m = Pattern.compile("^/([a-zA-Z_]*)/([a-zA-Z-]+)\\.properties$").matcher(path);
if( !m.matches() || m.groupCount() != 2 )
{
throw new ServletException("Bundle Group name is invalid: " + path);
}
final ResourceBundle resourceBundle = languageService.getResourceBundle(LocaleUtils.parseLocale(m.group(1)),
m.group(2));
final Properties text = new Properties();
for( String key : resourceBundle.keySet() )
{
text.put(key, resourceBundle.getString(key));
}
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "inline; filename=" + path + ".properties");
text.store(resp.getOutputStream(), null);
}
示例11: doGet
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletOutputStream out = resp.getOutputStream();
Spider s=new GithubCrawler().createSpider();
SpiderManager.get().add(s);
out.write(("add spider of "+s.getName()).getBytes());
out.flush();
out.close();
}
示例12: doTrace
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Called by the server (via the <code>service</code> method) to allow a
* servlet to handle a TRACE request.
*
* A TRACE returns the headers sent with the TRACE request to the client, so
* that they can be used in debugging. There's no need to override this
* method.
*
* @param req
* the {@link HttpServletRequest} object that contains the
* request the client made of the servlet
*
* @param resp
* the {@link HttpServletResponse} object that contains the
* response the servlet returns to the client
*
* @exception IOException
* if an input or output error occurs while the servlet is
* handling the TRACE request
*
* @exception ServletException
* if the request for the TRACE cannot be handled
*/
protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int responseLength;
String CRLF = "\r\n";
StringBuilder buffer = new StringBuilder("TRACE ").append(req.getRequestURI()).append(" ")
.append(req.getProtocol());
Enumeration<String> reqHeaderEnum = req.getHeaderNames();
while (reqHeaderEnum.hasMoreElements()) {
String headerName = reqHeaderEnum.nextElement();
buffer.append(CRLF).append(headerName).append(": ").append(req.getHeader(headerName));
}
buffer.append(CRLF);
responseLength = buffer.length();
resp.setContentType("message/http");
resp.setContentLength(responseLength);
ServletOutputStream out = resp.getOutputStream();
out.print(buffer.toString());
out.close();
return;
}
示例13: doRenderOutput
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
private void doRenderOutput(Document xmlOutput, HttpServletResponse res, XSLTransformer trans)
{
res.setContentType("text/html");
try
{
ServletOutputStream out = res.getOutputStream();
if (xmlOutput == null)
{
res.setStatus(500);
out.println("Session aborded") ;
}
else
{
if (trans == null)
{
out.println("Erreur interne") ;
res.setStatus(500);
}
if (!trans.doTransform(xmlOutput, out))
{
out.println("Erreur interne") ;
out.close() ;
res.setStatus(500);
}
}
out.close();
}
catch (IOException e)
{
res.setStatus(500);
}
}
示例14: doGet
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String payload = String.format("{\"enabled\": %s}%n", getFilter().isEnabled());
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
ServletOutputStream outputStream = response.getOutputStream();
try {
outputStream.print(payload);
} finally {
outputStream.close();
}
}
示例15: doHandleRequest
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
protected ModelAndView doHandleRequest(HttpServletRequest req,HttpServletResponse res) throws Exception {
String id = req.getParameter(ID_KEY);
if (!StringUtils.hasText(id)) {
id = (String) req.getAttribute(ID_KEY);
}
if(StringUtils.isEmpty(id)){
throw new RuntimeException("Upload definition id can not be null!");
}
UploadDefinition uploadDef=fileService.getUploadDefinition(id);
if(StringUtils.isEmpty(uploadDef.getUploadProcessorKey())){
throw new RuntimeException("Upload definition ["+id+"] has not set processor!");
}
String fileName = uploadDef.getFileName();
fileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
res.setContentType("application/octet-stream");
res.setHeader("Connection", "close");// 表示不能用浏览器直接打开
res.setHeader("Accept-Ranges", "bytes");// 告诉客户端允许断点续传多线程连接下载
long p = 0;
if (req.getHeader("Range") != null) {// 客户端请求的下载的文件块的开始字节
res.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
p = Long.parseLong(req.getHeader("Range").replaceAll("bytes=", "").replaceAll("-", ""));
}
res.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
InputStream in = fileService.getFile(uploadDef);
BufferedInputStream bin = new BufferedInputStream(in);
bin.skip(p);
long fileTotalLong = uploadDef.getSize();
// 下载的文件(或块)长度响应的格式是Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
res.setHeader("Content-Length", new Long(fileTotalLong - p).toString());
if (p != 0) {
// 如果不是从最开始下载,那么设置响应格式Content-Range: bytes [文件块的开始字节]-[文件的总大小
// -1]/[文件的总大小]
res.setHeader("Content-Range", "bytes " + new Long(p).toString() + "-"
+ (fileTotalLong - 1) + "/" + fileTotalLong);
}
ServletOutputStream out = res.getOutputStream();
try {
IOUtils.copy(bin, out);
} finally {
IOUtils.closeQuietly(in);
}
out.flush();
out.close();
return null;
}