當前位置: 首頁>>代碼示例>>Java>>正文


Java ServletOutputStream.write方法代碼示例

本文整理匯總了Java中javax.servlet.ServletOutputStream.write方法的典型用法代碼示例。如果您正苦於以下問題:Java ServletOutputStream.write方法的具體用法?Java ServletOutputStream.write怎麽用?Java ServletOutputStream.write使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.servlet.ServletOutputStream的用法示例。


在下文中一共展示了ServletOutputStream.write方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getOutputStream

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
@Override
public ServletOutputStream getOutputStream() throws IOException {

  final ServletOutputStream outputStream = d.getOutputStream();
  return new ServletOutputStream() {

    @Override
    public void write(int b) throws IOException {
      respBody.write(b);
      outputStream.write(b);
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {
      outputStream.setWriteListener(writeListener);
    }

    @Override
    public boolean isReady() {
      return outputStream.isReady();
    }
  };
}
 
開發者ID:dremio,項目名稱:dremio-oss,代碼行數:24,代碼來源:AccessLogFilter.java

示例2: copyRange

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).
 *
 * @param istream The input stream to read from
 * @param ostream The output stream to write to
 * @return Exception which occurred during processing
 */
protected IOException copyRange(InputStream istream,
                              ServletOutputStream ostream) {

    // Copy the input stream to the output stream
    IOException exception = null;
    byte buffer[] = new byte[input];
    int len = buffer.length;
    while (true) {
        try {
            len = istream.read(buffer);
            if (len == -1)
                break;
            ostream.write(buffer, 0, len);
        } catch (IOException e) {
            exception = e;
            len = -1;
            break;
        }
    }
    return exception;

}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:32,代碼來源:DefaultServlet.java

示例3: copyRange

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
/**
 * Copy the contents of the specified input stream to the specified output
 * stream, and ensure that both streams are closed before returning (even in
 * the face of an exception).
 *
 * @param istream
 *            The input stream to read from
 * @param ostream
 *            The output stream to write to
 * @return Exception which occurred during processing
 */
protected IOException copyRange(InputStream istream, ServletOutputStream ostream) {

	// Copy the input stream to the output stream
	IOException exception = null;
	byte buffer[] = new byte[input];
	int len = buffer.length;
	while (true) {
		try {
			len = istream.read(buffer);
			if (len == -1)
				break;
			ostream.write(buffer, 0, len);
		} catch (IOException e) {
			exception = e;
			len = -1;
			break;
		}
	}
	return exception;

}
 
開發者ID:how2j,項目名稱:lazycat,代碼行數:33,代碼來源:DefaultServlet.java

示例4: copyRange

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).
 *
 * @param istream The input stream to read from
 * @param ostream The output stream to write to
 * @return Exception which occurred during processing
 */
private IOException copyRange(InputStream istream,
                              ServletOutputStream ostream) {

    // Copy the input stream to the output stream
    IOException exception = null;
    byte buffer[] = new byte[input];
    int len = buffer.length;
    while (true) {
        try {
            len = istream.read(buffer);
            if (len == -1)
                break;
            ostream.write(buffer, 0, len);
        } catch (IOException e) {
            exception = e;
            len = -1;
            break;
        }
    }
    return exception;

}
 
開發者ID:c-rainstorm,項目名稱:jerrydog,代碼行數:32,代碼來源:DefaultServlet.java

示例5: doGet

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    Center center = ApplicationContextHelper.getBean(Constant.CENTERS_BEAN_NAME, Centers.class)
            .getCenterBySessionId(req.getParameter("sessionId"));
    ServletOutputStream out = resp.getOutputStream();
    try {
        // hex to bytes
        byte[] bytes = CoderUtils.hexStringToByteArray(req.getParameter("data"));
        // 解密
        String text = new String(RsaUtils.decryptByPublicKey(bytes, center.getPublicKey()));
        // 通過name選取方法並調用
        Object result = invokeMethodByName(req.getParameter("name"), text);
        byte[] respData = JSON.toJSONString(result).getBytes("utf-8");
        if (respData.length > center.getLength() - Constant.RSA_RESERVED_LENGTH) {
            throw new Exception("response data is too big");
        }
        // 返回加密
        respData = RsaUtils.encryptByPublicKey(respData, center.getPublicKey());
        out.write(respData);
    } catch (Exception e) {
        e.printStackTrace();
        out.write(500);
    } finally {
        out.close();
    }
}
 
開發者ID:cwdtom,項目名稱:hermes-java,代碼行數:27,代碼來源:HermesServlet.java

示例6: 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);
		}
	}
}
 
開發者ID:ForJ-Latech,項目名稱:fwm,代碼行數:29,代碼來源:Webservice1_0BSController.java

示例7: 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();
}
 
開發者ID:xbynet,項目名稱:crawler,代碼行數:11,代碼來源:HelloServlet.java

示例8: copy

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).
 *
 * @param cacheEntry The cache entry for the source resource
 * @param is The input stream to read the source resource from
 * @param ostream The output stream to write to
 *
 * @exception IOException if an input/output error occurs
 */
protected void copy(CacheEntry cacheEntry, InputStream is,
                  ServletOutputStream ostream)
    throws IOException {

    IOException exception = null;
    InputStream resourceInputStream = null;

    // Optimization: If the binary content has already been loaded, send
    // it directly
    if (cacheEntry.resource != null) {
        byte buffer[] = cacheEntry.resource.getContent();
        if (buffer != null) {
            ostream.write(buffer, 0, buffer.length);
            return;
        }
        resourceInputStream = cacheEntry.resource.streamContent();
    } else {
        resourceInputStream = is;
    }

    InputStream istream = new BufferedInputStream
        (resourceInputStream, input);

    // Copy the input stream to the output stream
    exception = copyRange(istream, ostream);

    // Clean up the input stream
    istream.close();

    // Rethrow any exception that has occurred
    if (exception != null)
        throw exception;

}
 
開發者ID:liaokailin,項目名稱:tomcat7,代碼行數:46,代碼來源:DefaultServlet.java

示例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;
	}
 
開發者ID:wkeyuan,項目名稱:DWSurvey,代碼行數:39,代碼來源:JcaptchaAction.java

示例10: exportFileByNIO

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
/**
 * @description 文件下載 nio 大緩存
 * @param response
 * @param file
 * @throws IOException
 */
public static void exportFileByNIO(HttpServletResponse response, File file) throws IOException {
    String filename = URLEncoder.encode(file.getName(), CharEncoding.UTF_8);
    response.setContentType(HttpUtil.CONTENT_TYPE_APPLICATION_OCTET_STREAM);
    response.setContentLength((int) file.length());
    response.setHeader(HttpUtil.CONTENT_DISPOSITION, "attachment;filename=" + filename);
    response.setHeader(HttpUtil.LOCATION, filename);
    ServletOutputStream op = response.getOutputStream();
    int bufferSize = 1024 * 128;
    FileInputStream fileInputStream = new FileInputStream(file.getPath());
    FileChannel fileChannel = fileInputStream.getChannel();
    ByteBuffer bb = ByteBuffer.allocateDirect(1024 * 1024 * 128);
    byte[] buffer = new byte[bufferSize];
    int nRead, nGet;
    while ((nRead = fileChannel.read(bb)) != -1) {
        if (nRead == 0) {
            continue;
        }
        bb.position(0);
        bb.limit(nRead);
        while (bb.hasRemaining()) {
            nGet = Math.min(bb.remaining(), bufferSize);
            bb.get(buffer, 0, nGet);
            op.write(buffer);
        }
        bb.clear();
    }
    fileChannel.close();
    fileInputStream.close();
}
 
開發者ID:tong12580,項目名稱:OutsourcedProject,代碼行數:36,代碼來源:FileManagementUtil.java

示例11: writeOutputStream

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
private void writeOutputStream(InputStream in, ServletOutputStream out) throws IOException {
    int length;
    byte[] bytes = new byte[512];

    while ((length = in.read(bytes)) != -1) {
        out.write(bytes, 0, length);
        out.flush();
    }
}
 
開發者ID:openNaEF,項目名稱:openNaEF,代碼行數:10,代碼來源:ForwarderServlet.java

示例12: copy

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
/**
 * Copy the contents of the specified input stream to the specified
 * output stream, and ensure that both streams are closed before returning
 * (even in the face of an exception).
 *
 * @param resourceInfo The resource information
 * @param ostream The output stream to write to
 *
 * @exception IOException if an input/output error occurs
 */
protected void copy(CacheEntry cacheEntry, InputStream is,
                  ServletOutputStream ostream)
    throws IOException {

    IOException exception = null;
    InputStream resourceInputStream = null;

    // Optimization: If the binary content has already been loaded, send
    // it directly
    if (cacheEntry.resource != null) {
        byte buffer[] = cacheEntry.resource.getContent();
        if (buffer != null) {
            ostream.write(buffer, 0, buffer.length);
            return;
        }
        resourceInputStream = cacheEntry.resource.streamContent();
    } else {
        resourceInputStream = is;
    }

    InputStream istream = new BufferedInputStream
        (resourceInputStream, input);

    // Copy the input stream to the output stream
    exception = copyRange(istream, ostream);

    // Clean up the input stream
    istream.close();

    // Rethrow any exception that has occurred
    if (exception != null)
        throw exception;

}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:45,代碼來源:DefaultServlet.java

示例13: sendData

import javax.servlet.ServletOutputStream; //導入方法依賴的package包/類
@Override
public IoFuture<Void> sendData(final ByteBuffer data) {
    try {
        final ServletOutputStream outputStream = response.getOutputStream();
        while (data.hasRemaining()) {
            outputStream.write(data.get());
        }
        return new FinishedIoFuture<>(null);
    } catch (IOException e) {
        final FutureResult<Void> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:15,代碼來源:ServletWebSocketHttpExchange.java

示例14: 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);
  }
}
 
開發者ID:rockscript,項目名稱:rockscript,代碼行數:11,代碼來源:ServerResponse.java

示例15: 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();

 }
 
開發者ID:opendaylight,項目名稱:fpc,代碼行數:35,代碼來源:NotificationService.java


注:本文中的javax.servlet.ServletOutputStream.write方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。