本文整理匯總了Java中javax.servlet.ServletResponse.setContentLength方法的典型用法代碼示例。如果您正苦於以下問題:Java ServletResponse.setContentLength方法的具體用法?Java ServletResponse.setContentLength怎麽用?Java ServletResponse.setContentLength使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.servlet.ServletResponse
的用法示例。
在下文中一共展示了ServletResponse.setContentLength方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: showStatus
import javax.servlet.ServletResponse; //導入方法依賴的package包/類
/**
* Show the status of the conf and the filter to the user.
*
* @param request
* to get status info from
* @param response
* response to show the status on.
* @throws java.io.IOException
* if the output cannot be written
*/
private void showStatus(final HttpServletRequest request, final ServletResponse response) throws IOException {
log.debug("showing status");
if (allowConfSwapViaHttp) {
String newConfPath = request.getParameter("conf");
if (!StringUtils.isBlank(newConfPath)) {
confPath = newConfPath;
loadUrlRewriterLocal();
}
}
Status status = new Status(confLastLoaded, this);
status.displayStatusInContainer(request);
response.setContentType("text/html; charset=UTF-8");
response.setContentLength(status.getBuffer().length());
final PrintWriter out = response.getWriter();
out.write(status.getBuffer().toString());
out.close();
}
示例2: serviceInitRequest
import javax.servlet.ServletResponse; //導入方法依賴的package包/類
private void serviceInitRequest(ServletResponse response, InputStream is) throws IOException
{
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int n;
while ((n = is.read(buffer)) >= 0)
{
outBuffer.write(buffer, 0, n);
}
String url = outBuffer.toString(JrpipServiceRegistry.ENCODE_STRING);
synchronized (this.registeredUrls)
{
if (!this.registeredUrls.contains(url))
{
JrpipServiceRegistry.getInstance().registerWebAppAtUrl(this.webapp, url);
this.registeredUrls.add(url);
}
}
int id = CLIENT_ID.incrementAndGet();
long vmAndClientId = vmId | (long) id;
String clientCount = String.valueOf(vmAndClientId);
response.setContentLength(clientCount.length());
response.getWriter().write(clientCount);
}
示例3: sendStreamToClient
import javax.servlet.ServletResponse; //導入方法依賴的package包/類
/**
* Writes the entire stream from the method to the response stream.
*
* @param response
* Response to send data to
* @throws IOException
* An IOException is thrown when we are having problems with
* reading the streams
*/
protected void sendStreamToClient(ServletResponse response)
throws IOException {
InputStream streamFromServer = method.getResponseBodyAsStream();
OutputStream responseStream = null;
if (streamFromServer != null) {
byte[] buffer = new byte[1024];
int read = streamFromServer.read(buffer);
while (read > 0) {
if (responseStream == null) {
responseStream = response.getOutputStream();
}
responseStream.write(buffer, 0, read);
read = streamFromServer.read(buffer);
}
streamFromServer.close();
}
// pock: if we dont't have any content set the length to 0 (corrects the
// 302 Moved Temporarily processing)
if (responseStream == null) {
response.setContentLength(0);
} else {
responseStream.flush();
responseStream.close();
}
}
示例4: downloadAttachment
import javax.servlet.ServletResponse; //導入方法依賴的package包/類
public String downloadAttachment() throws IOException, MessagingException
{
if (_attachmentToDownload == null)
{
_LOG.severe("No attachment available");
return null;
}
_message.getMessage().getFolder().open(Folder.READ_ONLY);
InputStream in = _attachmentToDownload.getInputStream();
FacesContext context = FacesContext.getCurrentInstance();
// Get the ServletResponse; nothing on ExternalContext is sufficient
ServletResponse response = (ServletResponse)
context.getExternalContext().getResponse();
response.setContentType(_attachmentToDownload.getContentType());
// If the size of the attachment is known, pass that on.
int size = _attachmentToDownload.getSize();
if (size >= 0)
{
response.setContentLength(size);
}
if (_LOG.isLoggable(Level.INFO))
_LOG.info("Downloading content+ [size=" + size +",contentType=" +
_attachmentToDownload.getContentType() + "]");
if (response instanceof HttpServletResponse)
{
String filename = _attachmentToDownload.getFileName();
if (filename != null)
{
((HttpServletResponse) response).setHeader(
"Content-disposition",
"attachment; filename=\"" + filename + "\"");
}
}
// Pass the text along, 128K at a time.
try
{
OutputStream out = response.getOutputStream();
try
{
byte[] buffer = new byte[131072];
while (true)
{
int count = in.read(buffer);
if (count < 0)
break;
out.write(buffer, 0, count);
}
}
// Close up the response
finally
{
// And tell JSF that we handled everything
context.responseComplete();
out.close();
}
}
// And make sure the folder got closed
finally
{
_message.getMessage().getFolder().close(false);
}
return null;
}