Image Grails-从控制器加载图像

Image Grails-从控制器加载图像,image,grails,loading,Image,Grails,Loading,我试图从FTP中检索zip文件,解压缩它,从中获取xml文件和图像文件,并解析xml,显示xml和图像的内容 byte[] image = ftpClientService.getThumbnailInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToWrapDirectory, fileName) FileOutputStream fos1 = new FileOutpu

我试图从FTP中检索zip文件,解压缩它,从中获取xml文件和图像文件,并解析xml,显示xml和图像的内容

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中检索(我可以使用file outputstream将其转换为文件)

现在,我被困在发送图像到gsp和显示。非常感谢您的任何意见

谢谢

你可以

  • img src=“${g.createLink(action:'a',params:[p:p])}”指向将在服务器端缓存映像的其他操作(with)
  • 或者直接嵌入HTML,比如

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


如果仍需要该图像文件供其他http请求使用,则应将图像提取到文件夹中,并将img路径列表发送到gsp。

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

将模型中的变量
image
发送到gsp,类型为
byte[]
,这是将其呈现为HTML的方法:

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


您还需要指定它是
图像/png
还是其他格式。

在图像标记的操作中需要指定什么?我需要显示控制器的命令吗?byte[]image=ftpClientService.getThumbnailInZip(customer.ftpUser,customer.ftpPassword,customer.ftpHost,customer.ftptorwrapdirectory,fileName)FileOutputStream fos1=newfileoutputstream(“zip.img”)尝试{fos1.write(image);}最后{fos1.close();}return[命令:this,文件名:fileName,applicationName:applicationName,contentProvider:contentProvider,operatingSystem:operatingSystem,handes:handes,zipImg:“zip.img”];在gsp更新了reference
createLink
的答案后,我相信这就是您所需要的。具体的文件引用将进入操作的
参数
。我的代码如上图所示…获取图像作为字节数组并将其转换为文件,返回并尝试在gsp中显示。使用图像作为临时字节数组,而不是本地文件,并且给定它只是一个小缩略图,你最好采取第二种方法。是的,图像将被多次使用,在未来的屏幕上也是如此。您能告诉我您是否有类似的代码用于将图像提取到本地文件夹吗