0

i have a array of bytes store in disk using ZK Framework later i need to retrieve this array of bytes and create a file the real file i need later pass it to the a gallery on image using Javascript jquery plugin which uses the following pattern to show the images.

<h:a href="http://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bohinjsko_jezero_2.jpg/800px-Bohinjsko_jezero_2.jpg">
<h:img                     src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bohinjsko_jezero_2.jpg/100px-Bohinjsko_jezero_2.jpg"/>

what have i solved so far?

i have create the file using the array of bytes using CommonsIO is OK. i was wondering if java7 have a mehod to create the file from a arrayofBytes which is not important for now.

as you can see the main important thing here is the plugin needs to source to my file image

<h:img                    src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/46/Bohinjsko_jezero_2.jpg/100px-Bohinjsko_jezero_2.jpg"/>

i have create a file in temp directory but this directory is unreacheable to the browser i guess because is firing an error image not found my solution is to create the file image inside image folder inside WEBCONTENT/image folder which it works using static images my question is..

how can i get the full path to my app directory programmatically? something like

C:\app\eclipse\myproject\WebContent\img\temporals 

for later to create here the temporal file

there is a better approach i only have the array of bytes of the image at start..

thanks a lot..

chiperortiz
  • 4,751
  • 9
  • 45
  • 79

1 Answers1

1

I believe it is faux pas to write data, during the lifetime of your web application, to the web application directory itself. It's even worse to try to reference these files by their path. These files will be deleted when the application is redeployed and in a servlet container you rarely reference files by their actual path, you reference the servlet's path.

Depending on your setup, you probably don't need to write the file to the disk at all. In the end, your server needs to read that file and stream it to the client anyway.

Your best option is probably to provide a servlet which just streams back the byte array with the correct mime type (eg: image/jpeg).
This answer suggests something similar.
This answer discusses implementation.
This is ideal in your case if you're working with the byte array anyway.

Another option, leveraging ZK, would be to create an AImage with the byte array and render that directly.

You can also consider serving static content from your server (probably not straight forward if you're using a servlet container like Tomcat) or use a simpler server like Apache Server for hosting your images.

It's tough to recommend anything specifically without knowing the context of your question.

Community
  • 1
  • 1
Sean Connolly
  • 5,692
  • 7
  • 37
  • 74