2011-10-24 51 views
1

我想从FTP检索zip文件,解压缩它,并从它们获取xml文件和图像文件并解析xml,显示xml和图像的内容。Grails - 从控制器加载图像

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" ] 

我能完成成功的XML部分和形象还我能够从zip检索一个字节格式(我可以使用文件的OutputStream将其转换为文件),

现在我坚持将图像发送给gsp并显示该图像。任何投入都非常感谢。

感谢

回答

0

您可以

  • img src="${g.createLink(action: 'a', params: [p: p])}"一些其他行动(与createLink),将缓存在服务器端的图像,
  • 或将其嵌入权为HTML,像in this question
+0

我需要在图片标签的动作中指定什么?我是否需要显示控制器的命令? – Techie

+0

字节[]图像= ftpClientService.getThumbnailInZip(customer.ftpUser,customer.ftpPassword,customer.ftpHost,customer.ftpToWrapDirectory,文件名) FileOutputStream中FOS1 =新FileOutputStream中( “zip.img”) 尝试{ fos1.write(图片); } finally { fos1.close(); } \t return [command:this,fileName:fileName,applicationName:applicationName,contentProvider:contentProvider,operatingSystem:operatingSystem,handSets:handSets,zipImg:“zip.img”]; 在GSP ​​Application Image Techie

+0

更新了答案参考'createLink',我认为这就是你所需要的。具体的文件引用将进入行动的'params'。 –

2

如果您只想使用图片一次,意味着它应该始终从zip文件中提取,那么将img以base64格式嵌入到网页中是一个不错的选择,因为您不必担心在将该base64编码值发送给gsp后,关于映像文件。

如果您仍然需要该图片文件被其他http请求使用,那么您应该将图片提取到一个文件夹并将img路径列表发送给gsp。

+0

是的,图像将被多次使用,在未来的屏幕中也是如此。如果您有任何类似的提取图像到本地文件夹的代码,请让我知道 – Techie

0

如果指定格式,浏览器可以呈现字节数组。

具有发送到byte[]类型的GSP模型中的变量image,这是使其HTML的方式:你还需要指定它是否image/png或其他格式

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