send image

Hi,I was wondering how I can create a image from a file on the servlet, and send it to a client through http? How convert a png image to a bytearray?best regards,Rune
[194 byte] By [runeroma] at [2007-11-27 2:32:32]
# 1

The [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/package-summary.html]java.io API[/url] ([url=http://java.sun.com/docs/books/tutorial/essential/io/]tutorials here[/url]) provides useful functionality at file level.

Check out the ByteArrayOutputStream to convert a File to a byte array.

BalusCa at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Is it just to write the byte array to the servletoutputstream and then receive it on the client side...?
runeroma at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Yes. Also see http://balusc.xs4all.nl/srv/dev-jep-img.html for some insights.
BalusCa at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
thank you for the help...
runeroma at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
I am using a tomcat-server for my servlet. Can I use the same methods for getting a image from file?
runeroma at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
Which problems are you occurring then?
BalusCa at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
how do I receive the image on the client. I have a mobile smartphone, htc tytn which shall receive and show the image transferred...rune
runeroma at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Just let the client invoke the request and retrieve the response.Which problems exactly are you occurring then?
BalusCa at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
I end up with a stringbuffer or a string. but I want an image out of it...
runeroma at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
Click at the links I provided you earlier and read them.
BalusCa at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
the client get the stream, but when I get the length of it, it is -1. something is wrong...
runeroma at 2007-7-12 2:48:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
I have this code to receive the image:dataInput = new DataInputStream( httpCon.openInputStream() );int len = (int) httpCon.getLength();byte[] data = new byte[ len ];int actual = dataInput.read(data);Image newImage = Image.createImage( data, 0, len );
runeroma at 2007-7-12 2:48:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

Whay exactly do you want to achieve? I understood that you want to load an local image and stream it to the response. But now it looks like that you want to load an remote image and stream it to the reponse? (as in: hotlinking the images).

Where is the image stored? Where should the image be sent to?

BalusCa at 2007-7-12 2:48:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14
I have a servlet on my tomcat server. The images is also stored on the server. The I have a client developed with J2ME which shall send a request to the servlet and get a image in return...
runeroma at 2007-7-12 2:48:19 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15
Well, the ImageServlet I've linked here above is exactly what you need.
BalusCa at 2007-7-21 20:28:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
can I still have the images in the c:\images folder or do I have to put them somwhere in the tomcat server folder?
runeroma at 2007-7-21 20:28:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17
Just read, understand and play with the code.
BalusCa at 2007-7-21 20:28:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 18
somehow it can't find the file, no matter what... I've tried many different paths and so... but it will not find the file...
runeroma at 2007-7-21 20:28:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 19
If it is stored in the WAR, use ServletContext#getRealPath().
BalusCa at 2007-7-21 20:28:14 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...