Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6753819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:12:26+00:00 2026-05-26T13:12:26+00:00

Basically my response headers contain Transfer-encoding=chunked, Trailer=[some trailer I want to send say e.g

  • 0

Basically my response headers contain

Transfer-encoding=chunked,

Trailer=[some trailer I want to send say e.g “SomeTrailer”]

Once I’m done writing the data to the Servlet outputstream, I’m writing the trailer
“SomeTrailer:[value]”, but this is not being parsed by the httpclient correctly.
The httpclient considers the whole of inputstream (including the trailer) as a single
chunk.
I’ve also tried writing the trailer in a response header after the data has been written to the outputstream but without success.

Please help

I haven’t found any good sources on this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:12:27+00:00Added an answer on May 26, 2026 at 1:12 pm

    I ended up writing a simple single threaded webserver for this. Turned out it was quite easy. The server is pretty simple. The code’s a bit rough though, but the main idea is there.

    What it does it sends the filecontents as the first chunk and the checksum of the file as a footer.

    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);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this form. Basically what I want is to send a auto-response with
I'm interested if it is possible to send HTTP GET headers with Java. Basically
Basically what I want to do it this: a pdb file contains a location
Basically I have some code to check a specific directory to see if an
Here is the request and response headers http://www.example.com/get/pdf GET /~get/pdf HTTP/1.1 Host: www.example.com User-Agent:
Basically I have a web site that renders HTML preview of some documents (mainly
I'm basically trying to work on server response sent in html. I'd like to
I have a view called send_confirmation email - which basically sends emails. I want
I'm currently using the httplib library in Python 2.7 to obtain some headers from
Consider the following code; it's basically a form that sends some data via HTTP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.