Java equivalent of the VB Request.InputStream
- by Android Addict
I have a web service that I am re-writing from VB to a Java servlet. In the web service, I want to extract the body entity set on the client-side as such:
StringEntity stringEntity = new StringEntity(xml, HTTP.UTF_8);
stringEntity.setContentType("application/xml");
httppost.setEntity(stringEntity);
In the VB web service, I get this data by using:
Dim objReader As System.IO.StreamReader
objReader = New System.IO.StreamReader(Request.InputStream)
Dim strXML As String = objReader.ReadToEnd
and this works great. But I am looking for the equivalent in Java.
I have tried this:
ServletInputStream dataStream = req.getInputStream();
byte[] data = new byte[dataStream.toString().length()];
dataStream.read(data);
but all it gets me is an unintelligible string:
data = [B@68514fec
Please advise.
Edit
Per the answers, I have tried:
ServletInputStream dataStream = req.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int r;
byte[] data = new byte[1024*1024];
while ((r = dataStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, r);
}
buffer.flush();
byte[] data2 = buffer.toByteArray();
System.out.println("DATA = "+Arrays.toString(data2));
whichs yields: DATA = []
and when I try:
System.out.println("DATA = "+data2.toString());
I get:
DATA = [B@15282c7f
So what am I doing wrong? As stated earlier, the same call to my VB service gives me the xml that I pass in.