Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

read Content

 
Ve Ku
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, again have a problem, i need to read the content of request, and obtain InputStream. It is possible that the content is not text, so i am trying like that :

This isn't working, nothing is displayed in the log, nor in the browser. Can somebody give me a hand please?
 
Ve Ku
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've rewrote it like that :

Down in my code i save the input in a file. When i open it with notepad++, i see, only nulls, something like that :nul nul nul nul....
May be, there is some encoding problem, what i am doing wrong?

 
Ravi C Kota
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Route what you actually read from Request to a console and see what you actually get and then you may find what might be missing. Did you try to debug the program ?

What do you see in buffer, after executing request.getInputStream().read(buffer) ;
 
Ve Ku
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nop, when i try to display the buffer in the browser i see zaros (0) or questionmarks(?).
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You say "It's possible the content is not text" but then you post some code which converts the content into a String. Obviously a bad idea for something which might not be text; but then you post some other code which doesn't do that.

However your other code has several problems:

First, you call the read() method, reading into a buffer, and you assume that it fills the buffer. This isn't a good assumption. The read() method returns the number of bytes it actually put into the buffer, but you disregarded that.

Second, you take the buffer, which supposedly is the bytes of the request, and you wrap it in a ByteArrayInputStream (why?) and then call the toString() method of the ByteArrayInputStream (why?) and then you do something with that nonsense string which has nothing to do with the request data.

However I'm at a loss to suggest what you should do instead, because you didn't say what you wanted to do. You just said that some things didn't happen and posted some weird code which didn't seem to be related to those things.
 
Ve Ku
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the weird code. I am trying to get the request content body and convert it in InputStream.
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this, then?
 
Ve Ku
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and i don't need to make a byte [] , read from buffer and so on?
 
Paul Clapham
Sheriff
Posts: 21316
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You said
Ve Ku wrote:I am trying to get the request content body and convert it in InputStream.


However the code I posted doesn't do any "converting". (And I don't understand what "convert it in InputStream" means anyway, I can't figure out a reasonable English interpretation of that phrase.)

The code I posted produces an InputStream. If you read from that InputStream, you will be reading the bytes that were in the request body. I don't know if that's what you were asking for or not.
 
Ve Ku
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes , you have the right, may be i have to clarify for my self, what exactly will come in the content body. The whole idea is that it will be a xml , which i have to parse.
Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is XML "not text"?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic