HTML <img> tag and src attribute as a byte[]

Hi everybody

I have a question is it possible to send a byte[] to the src attribute in a <img> HTML tag. What i would like to gain is a way of reading an img file from the jar in a custom tag which is no problem. Problems start when i want to send a byte[] which I have read from the InputStream to the StringBuffer which contains the HTML code.

byte[] dataByte =null;

String charEncoding ="UTF-8";

InputStream in = imageURL.openStream();//InputStream to the img file inside a jar

StringBuffer buffer =new StringBuffer();

StringBuffer sb =new StringBuffer();

String imgData ="";

int data;

while((data = in.read()) != -1)

{

buffer.append(new Integer(data).toString(), charEncoding);

}

imgData = buffer.toString();

dataByte = imgData.getBytes();

The problem starts in this line, i think i need some encoding to do this.

sb.append("<img src=\""+dataByte.toString()+"\">");

thx in advance

[1421 byte] By [alien_01a] at [2007-11-27 10:44:26]
# 1

You can certainly output that, but a web browser isn't going to interpret it as an image file and display that image. If the reader is a custom application then, I can think of better ways of sending that data then wrapping in HTML a href tags.

bsampieria at 2007-7-28 20:06:51 > top of Java-index,Java Essentials,Java Programming...
# 2

I know that u can do something like that :

<html>

<body>

</body>

<IMG

SRC="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONmOZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiOSpa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7"

ALT="Larry">

</html>

I want to do exactly the same thing but via custom tags. The problem is as u say i have some data in the src attribute but the web browser doesn't interpret it right. sory for the long html source.

alien_01a at 2007-7-28 20:06:51 > top of Java-index,Java Essentials,Java Programming...
# 3

IE doesn't support data: URI's.

What do you mean custom tags? Is this HTML to a browser or XML that you are going to parse? If the latter, you should still encode the bytes as base64 or some similar encoding, otherwise you don't know what's in the data that could muck with the XML parsing. In which case, typically, I'd define it as CDATA in a tag... <data>[the encoded data]</data>

bsampieria at 2007-7-28 20:06:51 > top of Java-index,Java Essentials,Java Programming...
# 4

The custom tags are generating HTML and then they are printing it via pageContext

pageContext.getOut().print(someHtmlCode);

Thx for the tip with IE i didn't know that :)

alien_01a at 2007-7-28 20:06:51 > top of Java-index,Java Essentials,Java Programming...
# 5

Custom tags as in JSP tags...?

If it's truly dynamic output, you could have the image data on the server with some filename and write the filename to the IMG tag, or some servlet + param that could return the file or something.

bsampieria at 2007-7-28 20:06:51 > top of Java-index,Java Essentials,Java Programming...
# 6

no.

You can't mix mime types in a single outputstream (or you can but the result will not be what you probably expect it to be).

jwentinga at 2007-7-28 20:06:51 > top of Java-index,Java Essentials,Java Programming...