java - 如何在 java servlet 中以分块响应发送 Http 预告片/页脚?

标签 java servlets httpclient chunked-encoding

基本上我的响应头包含

传输编码=分块,

Trailer=[我想发送的一些预告片,例如“SomeTrailer”]

一旦我将数据写入 Servlet 输出流,我就开始编写预告片 “SomeTrailer:[value]”,但这并没有被 httpclient 正确解析。 httpclient 将整个输入流(包括预告片)视为一个单一的 block 。 我还尝试在数据写入输出流后在响应 header 中写入预告片,但没有成功。

请帮忙

我还没有找到任何好的资料。

最佳答案

我最终为此编写了一个简单的单线程网络服务器。事实证明这很容易。服务器非常简单。虽然代码有点粗糙,但主要思想就在那里。

它的作用是将文件内容作为第一个 block 发送,并将文件的校验和作为页脚发送。

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;

public class ChunkedResponseServer implements Runnable {
  private static final Logger LOGGER = Logger.getLogger(ChunkedResponseServer.class);

  // Space ' '
  static final byte SP = 32;
  // Tab ' '
  static final byte HT = 9;
  // Carriage return
  static final byte CR = 13;
  // Line feed character
  static final byte LF = 10;

  final int port;

  private volatile boolean cancelled = false;

  public ChunkedResponseServer(int port) {
    LOGGER.info("Chunked response server running on port " + port);
    this.port = port;
  }

  @Override
  public void run() {
    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket(port);
      while (!cancelled) {
        final Socket connectionSocket = serverSocket.accept();
        handle(connectionSocket);
      }
    } catch (final IOException e) {
      throw new RuntimeException(e);
    }
  }

  public void cancel() {
    LOGGER.info("Shutting down Chunked response Server");
    cancelled = true;
  }

  private void handle(Socket socket) throws IOException {
    BufferedReader input = null;
    DataOutputStream output = null;
    try {
      input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      output = new DataOutputStream(socket.getOutputStream());

      addHeaders(output);
      addCRLR(output);

      final String filename = readFilename(input);
      final byte[] content = readContent(filename);
      addContentAsChunk(output, content);

      final String checksum = DigestUtils.md5Hex(content);
      addLastChunkAndChecksumFooter(output, checksum);
      addCRLR(output);

    } finally {
      IOUtils.closeQuietly(input);
      IOUtils.closeQuietly(output);
    }
  }

  private void addLastChunkAndChecksumFooter(DataOutputStream output, String checksum) throws IOException {
    output.writeBytes("0");
    addCRLR(output);
    output.writeBytes("checksum: " + checksum);
    addCRLR(output);
  }

  private void addContentAsChunk(DataOutputStream output, byte[] content) throws IOException {
    output.writeBytes(Integer.toHexString(content.length));
    addCRLR(output);
    output.write(content);
    addCRLR(output);
  }

  private void addCRLR(DataOutputStream output) throws IOException {
    output.writeByte(CR);
    output.writeByte(LF);
  }

  private void addHeaders(DataOutputStream output) throws IOException {
    output.writeBytes("HTTP/1.1 200 OK");
    addCRLR(output);
    output.writeBytes("Content-type: text/plain");
    addCRLR(output);
    output.writeBytes("Transfer-encoding: chunked");
    addCRLR(output);
    output.writeBytes("Trailer: checksum");
    addCRLR(output);
  }

  private String readFilename(BufferedReader input) throws IOException {
    final String initialLine = input.readLine();
    final String filePath = initialLine.split(" ")[1];
    final String[] components = filePath.split("/");
    return components[components.length - 1];
  }

  private byte[] readContent(String filename) throws IOException {
    final InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
    return IOUtils.toByteArray(in);
  }
}

关于java - 如何在 java servlet 中以分块响应发送 Http 预告片/页脚?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7851645/

相关文章:

java - 等待模式在 Selenium 2.0 中消失

java - 当设备进入休眠状态时如何继续服务?

java - struts-config.xml 中的 *.do 是什么

java - 如何将给定的字符串解析为有意义的 json?

java - 为什么 cd 命令无法使用 Java JSch?

java - 理解 dinic 算法有问题吗?

java - 在servlet中从表(oracle)中获取数据并传递给jsp

java - Servlet 返回 JSTL 结果或对象列表?

java - 如何在 Android 和/或 Java 中使用 HttpClient 管理 cookie?

ios - Angular http 客户端无法在 IOS chrome 浏览器中使用翻译?