I want to send an image from the J2ME client to the servlet.
I can get a byte array of images and send it using HTTP post.
conn = (HttpConnection) Connector.open (URL, connector.READ_WRITE, true); Conn.setRequestMethod (HttpConnection.POST); Conn.setRequestProperty ("content-type", "app / x-www-form-urxed"); Os.write (bytes, 0, bytes length); // bytes = byte array image
This is the servlet code:
string line; BufferedReader r1 = new BufferedReader (new InputStreamReader (in)); While {(line = r1) radlen ()) = null} {System.out.println ("line =" + line); buf.append (line);} string s = buf.toString (); byte [ ] Img_byte = s.getBytes ();
But the problem I've found is that, when I send bytes from J2ME client, some bytes are lost, their value is 0A
and 0D
are hex, carriage returns and line feeds.
In this way, POST method or readLine ()
to 0A
and 0D are not able to accept
value.
How to use it, or how to use any other method?
That's because you get the binary stream < BufferedReader
Content on CRLF readline ()
basically partition CRL is not included in individual lines now.
Do not use BufferedReader
for binary streams, this does not mean just received < Type outputstream
of any taste to code> InputStream , e.g. FileOutputStream
.
inputstream input = null; OutputStream Output = Null; Try {input = request.getInputStream (); Output = new FileOutputStream ("/ path / to / file.ext"); Byte [] buffer = new byte [10240]; (Full length = 0; (length = input. Reed (buffer ()) gt;) {output.write (buffer, 0, length);}} Finally {if (output! = Zero) output.close ( ); If (input! = Null) input.close ();}
that has said, content type
is the technology you are using The form is incorrect. You are not sending the WWW-Form URL-encoded values in the request body. You are sending a binary stream. It may be application / octet-stream
or Image
. This is of this problem O No, but it's just plain wrong.
Comments
Post a Comment