本文整理汇总了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();
}
};
}
示例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;
}
示例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;
}
示例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;
}
示例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();
}
}
示例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);
}
}
}
示例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();
}
示例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;
}
示例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: 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();
}
示例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();
}
}
示例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;
}
示例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();
}
}
示例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);
}
}
示例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();
}