1

I am trying to retrieve zip file from FTP, unzip it, and get xml file and image file from them and parse the xml, display the contents of xml and image.

byte[] image = ftpClientService.getThumbnailInZip(customer.ftpUser, 
    customer.ftpPassword, customer.ftpHost, customer.ftpToWrapDirectory, 
    fileName) 
FileOutputStream fos1 = new FileOutputStream("zip.img") 
try { 
    fos1.write(image); 
} finally { 
    fos1.close(); 
} 
return [
    command: this, 
    fileName: fileName, 
    applicationName: applicationName, 
    contentProvider: contentProvider, 
    operatingSystem: operatingSystem, 
    handSets: handSets, 
    zipImg:"zip.img" ]

I could finish the xml part successfully and image also I am able to retrieve from the zip in a byte format( i could convert it to a file using file outputstream),

Now I am stuck in sending the image to gsp and to display that. Any inputs are much appreciated.

Thanks

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
Techie
  • 1,611
  • 3
  • 15
  • 24

3 Answers3

2

If you want to use the image only once, meaning it should always be extract from the zip file, then embedding the img in base64 format into the webpage is a good option here because you don't need to worry about the image file after sending that base64 encoding value to gsp.

If you still need that image file to be used by other http requests then you should extract the images to a folder and send the list of img paths to gsp.

Bao Nhan
  • 180
  • 7
  • yeah the image would be used multiple times, in the future screens as well. could you please let me know if you have any similar code for extracting images to local folder – Techie Oct 24 '11 at 12:06
0

Browsers can render byte arrays if you specify the format.

Having a variable image in the model sent to the gsp of type byte[], this is the way to render it HTML:

<img src="data:image/png;base64,${image.encodeBase64()}"/>

You also need to specify if it's image/png or other format.

IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
0

You can either

  • point img src="${g.createLink(action: 'a', params: [p: p])}" to some other action (with createLink) that will cache the image on your server side,
  • or embed it right into HTML, like in this question.
Community
  • 1
  • 1
Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
  • what do I need to specify in the action of the image tag? Do i need to display the command of the controller? – Techie Oct 24 '11 at 11:58
  • byte[] image = ftpClientService.getThumbnailInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToWrapDirectory, fileName) FileOutputStream fos1 = new FileOutputStream("zip.img") try { fos1.write(image); } finally { fos1.close(); } return [command: this, fileName: fileName, applicationName: applicationName, contentProvider: contentProvider, operatingSystem: operatingSystem, handSets: handSets, zipImg:"zip.img" ]; In gsp Application Image – Techie Oct 24 '11 at 12:23
  • Updated the answer to reference `createLink`, I believe that's all what you need. The specific file reference will go into action's `params`. – Victor Sergienko Oct 24 '11 at 12:25
  • my code looks like above...getting image as a byte array and converting it to file, returning and trying to display in the gsp. – Techie Oct 24 '11 at 12:26
  • With image as transient byte array, not as a local file, and given it's just a small thumbnail, you'd better take the second approach. – Victor Sergienko Oct 24 '11 at 12:30
  • thanks for the responses, sorry I am still new to this and I have converted the byte array to a file as above code and to use that in gsp, in the action part of it , what do i need to specify( i have written the code in DeliveryController.groovy and in DeliveryConfirmationCommand part of it. – Techie Oct 24 '11 at 12:38
  • 1. Whatever it takes *you* to render the file. If you cache the file anyway (like the `zip.img` in yuor code), you don't have to reference another action, and just print an external URL to that file ("`zip.img`") to `img src`. Where do you save the thumbs now, btw? 2. Still, if you don't need to cache the thumbs on your server, I suggest you embed them in HTML. – Victor Sergienko Oct 24 '11 at 14:29
  • the contents of the zip file would be saved by default in the tmp folder of the "c" drive. will try to print an external url and will update you. – Techie Oct 24 '11 at 14:40
  • still i am not able to load the image. could you please provide me a sample code depending on my above code – Techie Oct 24 '11 at 15:25
  • Thanks for all the help...I have finished the image loading part. – Techie Oct 25 '11 at 11:20