`

Response的OutputStream和Writer

阅读更多

 

 

public ServletOutputStream getOutputStream() throws IOException {
  if (this.servletOutputStream == null) {
     this.servletOutputStream = new ServletOutputStreamAdapter(this.exchange.getResponseBody());
   }
    return this.servletOutputStream;
  }
  public PrintWriter getWriter() throws IOException {
    if (this.writer == null) {
      if (this.characterEncoding != null) {
        this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(this.exchange.getResponseBody(), this.characterEncoding)));
       }
     else {
       this.writer = new PrintWriter(this.exchange.getResponseBody());
       }
   }
    return this.writer;
}

 所以用完以后要关闭

 

分享到:
评论

相关推荐

    response设置的实例源码

    PrintWriter writer = response.getWriter(); writer.println("<h1>Hello, World!</h1>"); writer.close(); ``` 七、示例代码 以下是一个简单的Servlet实例,展示了上述各种设置: ```java import javax.servlet.*;...

    java.lang.IllegalStateException: Cannot call sendError() after the response has

    2. **在Servlet的OutputStream或Writer已经写入数据后调用sendError()**:如果在输出流或字符流中已经写入了内容,响应就已经被“提交”了,这时再调用`sendError()`会抛出异常。 3. **错误处理顺序**:在错误处理...

    HttpResponse的Output与OutputStream、Filter关系与区别介绍

    本文将详细介绍HttpResponse对象中的Output、OutputStream属性和Filter属性,解释它们之间的关系以及它们之间的区别。 首先,HttpResponse对象是***中用于处理响应的服务器端对象。它提供了多种属性和方法,以允许...

    JAVA 发送http请求工具类

    try (OutputStream os = connection.getOutputStream()) { BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); writer.write(getPostDataString(params)); ...

    【ASP.NET编程知识】asp.net C#实现下载文件的六种方法实例.docx

    BinaryWriter writer = new BinaryWriter(Response.OutputStream); while (iStream.Position ) { byte[] buffer = new byte[1024]; int bytesRead = iStream.Read(buffer, 0, 1024); writer.Write(buffer, 0, ...

    grails导出csv

    def writer = new CSVWriter(response.outputStream) writer.writeAll(users.collect { [it.name, it.age.toString(), it.city] }) // 假设User有name, age和city属性 writer.close() ``` 4. **处理异常** ...

    Windows 下整合 weblogic10 jsf1.2 hibernate3

    2. **Servlet response already use stream, Writer not possible** - **异常解释**:当 Servlet 响应已经使用了一个输出流(OutputStream),再尝试使用 PrintWriter 类写入文本时会出现此异常。这是因为一个响应...

    android3.0socket通讯实现代码.pdf

    5. **输入/输出流处理**:通过 `BufferedWriter` 和 `OutputStreamWriter` 对象向Socket的OutputStream写入数据,通过 `BufferedReader` 和 `InputStreamReader` 从Socket的InputStream读取数据。 6. **UI交互**:...

    SpringBoot导出Excel的四种方式

    OutputStream outputStream = response.getOutputStream(); JxlsHelper.getInstance() .processTemplate(templateStream, outputStream, Collections.singletonMap("dataList", dataList)); response....

    Asp.net开发技巧

    var writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); var htmlString = "<h1>报告标题</h1><p>这里是报告内容...</p>"; var htmlParser = new HTMLWorker(pdfDoc); htmlParser....

    Java Servlet输出中文乱码问题解决方案

    ServletOutputStream outputStream = response.getOutputStream(); outputStream.write(date.getBytes()); } ``` 这是因为服务器端和浏览器端的编码格式不一致所致。解决方法是使浏览器端和服务器端的编码格式...

    android socket通讯例子

    在客户端和服务器端的通信过程中,要确保正确关闭资源,如Socket、InputStream和OutputStream,以避免内存泄漏。例如,在完成通信后,可以使用`finally`块关闭它们: ```java try { // 通信操作... } finally { ...

    Socket简单代码

    PrintWriter writer = new PrintWriter(outputStream, true); // 发送一条消息 writer.println("Hello, Server!"); // 获取InputStream,用于接收服务器返回的数据 InputStream inputStream = socket....

    访问网络工具类

    OutputStream os = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(getUrlEncodedParams(params)); writer.flush(); writer....

    c# 二进流输出swf 到页面

    BinaryWriter writer = new BinaryWriter(response.OutputStream); writer.Write(buffer); writer.Flush(); response.End(); ``` 4. **权限控制**:在上述代码中,我们可以通过添加适当的条件语句来检查用户...

    java如何利用java.net.URLConnection发送HTTP.docx

    OutputStreamWriter writer = new OutputStreamWriter(outputStream, charset); writer.write("key=value&anotherKey=anotherValue"); writer.flush(); writer.close(); ``` 6. **接收响应**: 读取服务器...

    Servlet的应用实例.pdf

    在使用Writer或OutputStream之前,需要先设置头部,然后使用Writer或OutputStream将相应的主体部分发送给用户。完成后,需要关闭Writer或OutputStream,以便让服务器知道响应已经结束。 使用HttpServletRequest和...

    google gson 异步post访问服务器的例子。

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); writer.write(jsonString); writer.flush(); writer.close(); int responseCode = connection.getResponseCode...

    Android中的Socket编程(包含服务端代码)

    PrintWriter writer = new PrintWriter(outputStream, true); writer.println("客户端发送的数据"); InputStream inputStream = socket.getInputStream(); BufferedReader reader = new BufferedReader(new ...

Global site tag (gtag.js) - Google Analytics