本文整理汇总了Java中javax.servlet.ServletOutputStream.flush方法的典型用法代码示例。如果您正苦于以下问题:Java ServletOutputStream.flush方法的具体用法?Java ServletOutputStream.flush怎么用?Java ServletOutputStream.flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.servlet.ServletOutputStream
的用法示例。
在下文中一共展示了ServletOutputStream.flush方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: verify
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* 生成验证码的逻辑
*/
@RequestMapping("verify")
private ModelAndView verify(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String capText = captchaProducer.createText();
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
return null;
}
示例2: writeToResponse
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Write the given temporary OutputStream to the HTTP response.
* @param response current HTTP response
* @param baos the temporary OutputStream to write
* @throws IOException if writing/flushing failed
*/
protected void writeToResponse(HttpServletResponse response, ByteArrayOutputStream baos) throws IOException {
// Write content type and also length (determined via byte array).
response.setContentType(getContentType());
response.setContentLength(baos.size());
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
}
示例3: writeResponse
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
protected void writeResponse(Response res) throws Exception {
response.setStatus(res.getStatusCode(), res.getStatusMessage());
for (HeaderField header : res.getHeader().getAllHeaderFields()) {
if (header.getHeaderName().equals(Header.TRANSFER_ENCODING))
continue;
response.addHeader(header.getHeaderName().toString(), header.getValue());
}
ServletOutputStream out = response.getOutputStream();
res.getBody().write(new PlainBodyTransferrer(out));
out.flush();
response.flushBuffer();
exchange.setTimeResSent(System.currentTimeMillis());
exchange.collectStatistics();
}
示例4: image
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* 验证码
*/
@RequestMapping(value = "/captcha", method = RequestMethod.GET)
public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception {
if (StringUtils.isEmpty(captchaId)) {
captchaId = request.getSession().getId();
}
response.addHeader("vincent.li", "vincent.li");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream servletOutputStream = null;
try {
servletOutputStream = response.getOutputStream();
BufferedImage bufferedImage = (BufferedImage) imageCaptchaService.getChallengeForID(captchaId);
ImageIO.write(bufferedImage, "jpg", servletOutputStream);
servletOutputStream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(servletOutputStream);
}
}
示例5: generate
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* 生成验证码
*/
static void generate(HttpServletResponse response, String vCode) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
ServletOutputStream sos = null;
try {
drawGraphic(image, vCode);
sos = response.getOutputStream();
ImageIO.write(image, "JPEG", sos);
sos.flush();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(sos);
}
}
示例6: 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 definition = fileService.getUploadDefinition(id);
if (StringUtils.isEmpty(definition.getUploadProcessorKey())) {
throw new RuntimeException("Upload definition [" + id
+ "] has not set processor!");
}
InputStream in = fileService.getFile(definition);
BufferedInputStream bin = new BufferedInputStream(in);
ServletOutputStream out = res.getOutputStream();
try {
IOUtils.copy(bin, out);
} finally {
IOUtils.closeQuietly(in);
}
out.flush();
out.close();
return null;
}
示例7: doFilter
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
if(AuthUtil.getAuthFlag()){
arg2.doFilter(arg0, arg1);
return ;
}
try{
HttpServletRequest request = (HttpServletRequest) arg0;
WrapperResponse wrapper = new WrapperResponse((HttpServletResponse)arg1);
boolean flag = doGet(request, wrapper);
log.info("HTTPAuthFilter uri {} doGet flag : {}",request.getRequestURI(),flag);
if(flag){
arg2.doFilter(arg0, wrapper);
byte[] content = wrapper.getContent();
content = filterGroups(request, content);
if(ArrayUtils.isNotEmpty(content)){
ServletOutputStream out = arg1.getOutputStream();
out.write(content);
out.flush();
out.close();
}
}
}catch(Exception e){
log.error(e.getMessage(),e);
}finally{
AuthUtil.THREAD_LOCAL_USER.remove();
}
}
示例8: 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;
}
示例9: getVerifyCodeImage
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@RequestMapping("/captcha")
public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
// return a jpeg
response.setContentType("image/jpeg");
// create the text for the image
String capText = captchaProducer.createText();
// store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
// create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
// write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
System.out.println("******************验证码是: " + code + "******************");
}
示例10: writeImageCaptcha
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
private void writeImageCaptcha(HttpServletResponse response,String capText) throws IOException {
// 设置请求头
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control",
"no-store, no-cache, must-revalidate");
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
// 写出图片流
ServletOutputStream out = response.getOutputStream();
ImageIO.write(defaultKaptcha.createImage(capText),"jpg",out);
out.flush();
}
示例11: writeBodyBytes
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
private void writeBodyBytes(byte[] bytes) {
try {
headerContentLength(bytes.length);
ServletOutputStream out = response.getOutputStream();
out.write(bytes);
out.flush();
} catch (IOException e) {
throw new RuntimeException("Couldn't send body: "+e.getMessage(), e);
}
}
示例12: testLongProcess
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
/**
* Verify that the test result can be returned correctly even when the
* logic in the method to test takes a long time and thus it verifies that
* the test result is only returned after it has been written in the
* application scope on the server side.
*/
public void testLongProcess() throws Exception
{
ServletOutputStream os = response.getOutputStream();
os.print("<html><head><Long Process></head><body>");
os.flush();
// do some processing that takes a while ...
Thread.sleep(3000);
os.println("Some data</body></html>");
}
示例13: getVerifyCodeImage
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
@RequestMapping("/captcha")
public void getVerifyCodeImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
response.setDateHeader("Expires", 0);
//-- Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
//-- Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
//-- Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
//-- return a jpeg
response.setContentType("image/jpeg");
//-- create the text for the image
String capText = captchaProducer.createText();
//-- store the text in the session
session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
//-- create the image with the text
BufferedImage bi = captchaProducer.createImage(capText);
ServletOutputStream out = response.getOutputStream();
//-- write the data out
ImageIO.write(bi, "jpg", out);
try {
out.flush();
} finally {
out.close();
}
}
示例14: contextInitialized
import javax.servlet.ServletOutputStream; //导入方法依赖的package包/类
public void contextInitialized(ServletContextEvent sce) {
Map<String, AsyncContext> notificationStreams = new ConcurrentHashMap<String, AsyncContext>();
sce.getServletContext().setAttribute("notificationStreams", notificationStreams);
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
String clientId = null;
AsyncContext asyncContext = null;
while(true)
{
try {
Map.Entry<String, String> entry = blockingQueue.take();
clientId = entry.getKey();
asyncContext = notificationStreams.get(clientId);
if(notificationStreams.get(entry.getKey()) != null){
ServletOutputStream out = asyncContext.getResponse().getOutputStream();
out.write(entry.getValue().getBytes());
out.flush();
asyncContext.getResponse().flushBuffer();
}
} catch (Exception e){
ErrorLog.logError("Cannot write to client", e.getStackTrace());
asyncContext.complete();
notificationStreams.remove(clientId);
break;
}
}
}
});
thread.start();
}
示例15: 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();
}