This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes read Content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "read Content" Watch "read Content" New topic
Author

read Content

Ve Ku
Greenhorn

Joined: Sep 17, 2010
Posts: 19
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

Joined: Sep 17, 2010
Posts: 19
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

Joined: Jan 29, 2008
Posts: 61
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) ;


Thanks & Regards, Ravi C.Kota
SCJP 5.0, OCDJWS 5.0
Ve Ku
Greenhorn

Joined: Sep 17, 2010
Posts: 19
nop, when i try to display the buffer in the browser i see zaros (0) or questionmarks(?).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 17, 2010
Posts: 19
Sorry for the weird code. I am trying to get the request content body and convert it in InputStream.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Like this, then?
Ve Ku
Greenhorn

Joined: Sep 17, 2010
Posts: 19
and i don't need to make a byte [] , read from buffer and so on?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Sep 17, 2010
Posts: 19
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

Joined: Jan 10, 2002
Posts: 60810
    
  65

How is XML "not text"?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: read Content
 
Similar Threads
Write a String to ByteArrayOutputStream
upload file through applet
How to Upload a image or video where it will be attached or keep in email?
How to encode image in parts?
Blob Inputstream Reading