getting binary data from HTTP Request

S

Shlomi

Hi.

i have a servlet that receives a binary data in one of its parameters.
i cannot read this binary data to a byte array for some reason
when i am trying to use servletInputStream.available() i am getting 0.
any suggestions ?
what am i donig wrong ?

here is my code :
(don't look for syntax mistakes, i pasted the relevant code only)

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
throws ServletException, IOException
{
try
{
// get POSTED data
// use servlet input stream
ServletInputStream ins = request.getInputStream();
//count length of data
int len = request.getContentLength();
//allocate bytes buffer
byte[] buf = new byte[len];
int offset = 0;
do
{
int inputLen = ins.read(buf, offset, len - offset);
// MY PROBLEM ----- inputLen always equals -1
if (DEBUG)
System.out.println("POST Input Length = " + inputLen);

if (inputLen <= 0)
{
String msg = "read finished early - read " +
offset + " of " + len + " bytes(contentLength)" ;
throw new IOException(msg);
}
offset += inputLen;
}
}
 
M

Michiel Konstapel

Hi.

i have a servlet that receives a binary data in one of its parameters.
i cannot read this binary data to a byte array for some reason when i am
trying to use servletInputStream.available() i am getting 0. any
suggestions ? what am i donig wrong ?

Don't use available(), just read. available() only tells you how much can
*certainly* be read without blocking, so returning 0 all the time is in
fact a valid implementation. read() will block until some data has been
read; just continue until it returns -1 (end of stream).
However, I see you're actually doing that in the posted code, and I don't
know why that isn't working :-/
Michiel
here is my code : (don't look for syntax mistakes, i pasted the relevant
code only)

public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
throws ServletException, IOException
{
try
{
// get POSTED data
// use servlet input stream
ServletInputStream ins = request.getInputStream();
//count length of data
int len = request.getContentLength();
//allocate bytes buffer byte[] buf = new byte[len];
int offset = 0;
do
{
int inputLen = ins.read(buf, offset, len - offset);
// MY PROBLEM ----- inputLen always equals -1 if (DEBUG)
System.out.println("POST Input Length = " + inputLen);

if (inputLen <= 0)
{
String msg = "read finished early - read " +
offset + " of " + len + " bytes(contentLength)" ;
throw new IOException(msg);
}
offset += inputLen;
}
}
 
I

Igor N. Kolomiyets

Hi,

how binary data is being passed to the servlet? What is tha value in len
variable (getContentLength())?

Best regards,
Igor.
 
S

Shlomi

Igor N. Kolomiyets said:
Hi,

how binary data is being passed to the servlet? What is tha value in len
variable (getContentLength())?


The len of the variable is somewhere between 300 - 350 bytes.
i am integrating two programs that work on the web, i have another
application that pass the binary data to my servlet.

Thank you so much for your help !
Shlomi

Best regards,
Igor.
Hi.

i have a servlet that receives a binary data in one of its parameters.
i cannot read this binary data to a byte array for some reason
when i am trying to use servletInputStream.available() i am getting 0.
any suggestions ?
what am i donig wrong ?

here is my code :
(don't look for syntax mistakes, i pasted the relevant code only)

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
throws ServletException, IOException
{
try
{
// get POSTED data
// use servlet input stream
ServletInputStream ins = request.getInputStream();
//count length of data
int len = request.getContentLength();
//allocate bytes buffer
byte[] buf = new byte[len];
int offset = 0;
do
{
int inputLen = ins.read(buf, offset, len - offset);
// MY PROBLEM ----- inputLen always equals -1
if (DEBUG)
System.out.println("POST Input Length = " + inputLen);

if (inputLen <= 0)
{
String msg = "read finished early - read " +
offset + " of " + len + " bytes(contentLength)" ;
throw new IOException(msg);
}
offset += inputLen;
}
}
 
I

Igor N. Kolomiyets

Shlomi,

my question was actually what format is your another application sending
data to your servlet? Can you post a sample of the POST request?

Best regards,
Igor.
Igor N. Kolomiyets said:
Hi,

how binary data is being passed to the servlet? What is tha value in len
variable (getContentLength())?



The len of the variable is somewhere between 300 - 350 bytes.
i am integrating two programs that work on the web, i have another
application that pass the binary data to my servlet.

Thank you so much for your help !
Shlomi


Best regards,
Igor.
Hi.

i have a servlet that receives a binary data in one of its parameters.
i cannot read this binary data to a byte array for some reason
when i am trying to use servletInputStream.available() i am getting 0.
any suggestions ?
what am i donig wrong ?

here is my code :
(don't look for syntax mistakes, i pasted the relevant code only)

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
throws ServletException, IOException
{
try
{
// get POSTED data
// use servlet input stream
ServletInputStream ins = request.getInputStream();
//count length of data
int len = request.getContentLength();
//allocate bytes buffer
byte[] buf = new byte[len];
int offset = 0;
do
{
int inputLen = ins.read(buf, offset, len - offset);
// MY PROBLEM ----- inputLen always equals -1
if (DEBUG)
System.out.println("POST Input Length = " + inputLen);

if (inputLen <= 0)
{
String msg = "read finished early - read " +
offset + " of " + len + " bytes(contentLength)" ;
throw new IOException(msg);
}
offset += inputLen;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,000
Messages
2,570,252
Members
46,848
Latest member
CristineKo

Latest Threads

Top