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 3984436
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:46:11+00:00 2026-05-20T05:46:11+00:00

I have something like this in my code: String boundary = thisIsMyBoundaryString; StringBuilder body

  • 0

I have something like this in my code:

String boundary = "thisIsMyBoundaryString";
StringBuilder body = new StringBuilder();  
...  
byte[] data = bos.toByteArray();  
body.append(new String(data));  
body.append("\r\n--" + boundary + "--\r\n");  
String entity = body.toString();  

I’m building a POST request and need to insert some binary data (JPEG compressed bitmap).

After appending byteArray, I want to append newLine and boundary string,
but the StringBuilder gets corrupted – seems that the bytes from the added boundary
become misaligned from that point on
and they are no longer being interpreted as my original string but as some random characters.

Is this what is happening here?

How can I resolve this? Maybe I should add a byte of padding to byteArray before appending boundary?

What is the proper way of adding binary data to POST request?
Please note that using Base64 encoding is not an option here.

 
 
[edit]
Solution was to use MultipartEntity to make such requests
and forget about adding boundaries manually.

Here is relevant code snippet:

FileBody bin = new FileBody(new File(Environment.getExternalStorageDirectory() + "/" + FILE_NAME));
MultipartEntity entity = new MultipartEntity();
entity.addPart("message", new StringBody("A message that goes with request"));
entity.addPart("file", bin);

(Third line has to be inside of a try/catch block but I removed it for readability)

  • 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-20T05:46:11+00:00Added an answer on May 20, 2026 at 5:46 am

    I think you should use something like Apache HttpClient (or maybe it’s in their http-mime module, I haven’t used it myself) that can do multipart posts. See this question, for example. Binary data like a JPEG is not character data and really shouldn’t be put in a String.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have see code like this Dim s as something = new something Dim
In our code we used to have something like this: *(controller->bigstruct) = ( struct
In a bunch o' places in my code, I have something like this: public
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
Let's say I have some code like this if(isset($_GET['foo'])) //do something if(isset($_GET['bar'])) //do something
I have something like this: barProgress.BeginAnimation(RangeBase.ValueProperty, new DoubleAnimation( barProgress.Value, dNextProgressValue, new Duration(TimeSpan.FromSeconds(dDuration))); Now, how
So I have something like this: var xmlStatement:String = xmlObject.node[3].@thisValue; What mystery function do
I have a string which looks something like this: $fetched = name=myName zip=420424 country=myCountry;
I have something like this in my code: namespace A { namespace B {
I have something like this: <node TEXT= txt A /> <node TEXT= txt X

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.