0

I have an HTTP request body contained in a Buffer:

--ec7db176-a2fa-402c-84a5-7e24634be58a
Content-Disposition: form-data; name="data"
Content-Transfer-Encoding: binary
Content-Type: image/jpeg
Content-Length: 54509

������JFIF��    [...more data...]
--ec7db176-a2fa-402c-84a5-7e24634be58a--

I want to extract the image data into a File.

I don't know how to handle the buffer to cut the first 6 lines and the last one and copy the data into the file.

I tried to read the data as a string and then write it into an OutputSteam but some characters are misencoded.

File tempFile = this.file.get();
OutputStream os = new FileOutputStream(tempFile);

Buffer body = request.getBody();

String multipartBoundary = body.readUtf8Line();
String start = "\r\n\r\n";
String end = "\r\n" + multipartBoundary;
String stringBody = body.readUtf8();

byte[] bytes = StringUtils.substringBetween(stringBody, start, end)
                          .getBytes(Charset.forName("UTF-8"));
os.write(bytes);
os.close();

Here is the first hex bytes that are written: enter image description here

Upside is what I get and downside is the original file

Yohan D
  • 930
  • 2
  • 7
  • 24
  • Possible duplicate of [Reading binary file from URLConnection](http://stackoverflow.com/questions/3221979/reading-binary-file-from-urlconnection) – Mario Santini Sep 01 '16 at 12:33
  • hum I don't think so, your link doesn't explain how I could skip the first 6 lines and the last one – Yohan D Sep 01 '16 at 12:44
  • 1
    Right, it missed the multipart handling, please have a look http://stackoverflow.com/questions/11656436/read-multipart-mixed-response-in-java-groovy – Mario Santini Sep 01 '16 at 13:03

0 Answers0