Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove request header from HttpInputStream

I need help with a servlet.

I need to read a inputStream in one request and write a tiff file. The inputStream come with request header and i dont know how remove that bytes and write only the file.

See initial bytes from the writen file.

-qF3PFkB8oQ-OnPe9HVzkqFtLeOnz7S5Be
Content-Disposition: form-data; name=""; filename=""
Content-Type: application/octet-stream; charset=ISO-8859-1
Content-Transfer-Encoding: binary

I want to remove that and write only bytes from the tiff file. PS: sender of file its not me.

like image 291
fhgomes_ti Avatar asked Dec 05 '25 10:12

fhgomes_ti


2 Answers

I'm not sure why you're not using HttpServletRequest's getInputStream() method to get the content without its headers, either way you have the option to start reading the input stream and ignoring the content until you find two consecutive CRLF's, which defines the end of the headers.

One way of doing that is like this:

String headers = new java.util.Scanner(inputStream).next("\\r\\n\\r\\n");
// Read rset of input stream
like image 147
iddo Avatar answered Dec 07 '25 23:12

iddo


Apache commons solve 90% of your problems... only need know what keywords use in search :)
"parse multipart request" and google say: http://www.oreillynet.com/onjava/blog/2006/06/parsing_formdata_multiparts.html

int boundaryIndex = contentType.indexOf("boundary=");
byte[] boundary = (contentType.substring(boundaryIndex + 9)).getBytes();

ByteArrayInputStream input = new ByteArrayInputStream(buffer.getBytes());
MultipartStream multipartStream =  new MultipartStream(input, boundary);

boolean nextPart = multipartStream.skipPreamble();
while(nextPart) {
  String headers = multipartStream.readHeaders();
  System.out.println("Headers: " + headers);
  ByteArrayOutputStream data = new ByteArrayOutputStream();
  multipartStream.readBodyData(data);
  System.out.println(new String(data.toByteArray());

  nextPart = multipartStream.readBoundary();
}
like image 40
fhgomes_ti Avatar answered Dec 07 '25 23:12

fhgomes_ti