【问题标题】:Grails - Loading image from the controllerGrails - 从控制器加载图像
【发布时间】:2011-10-24 11:44:39
【问题描述】:

我正在尝试从 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 中以字节格式检索图像(我可以使用文件输出流将其转换为文件),

现在我被困在将图像发送到 gsp 并显示它。非常感谢任何意见。

谢谢

【问题讨论】:

    标签: image grails loading


    【解决方案1】:

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

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

    【讨论】:

    • 是的,该图像将被多次使用,在未来的屏幕中也是如此。如果您有任何类似的用于将图像提取到本地文件夹的代码,请告诉我
    【解决方案2】:

    你可以

    • img src="${g.createLink(action: 'a', params: [p: p])}" 指向将在服务器端缓存图像的其他操作(使用createLink),
    • 或将其直接嵌入到 HTML 中,例如 in this question

    【讨论】:

    • 图片标签的action需要指定什么?需要显示控制器的命令吗?
    • byte[] image = ftpClientService.getThumbnailInZip(customer.ftpUser, customer.ftpPassword, customer.ftpHost, customer.ftpToWrapDirectory, fileName) FileOutputStream fos1 = new FileOutputStream("zip.img") try { fos1 .写(图像); } 最后 { fos1.close(); } return [command: this, fileName: fileName, applicationName: applicationName, contentProvider: contentProvider, operatingSystem: operatingSystem, handSets: handSets, zipImg:"zip.img" ];在 gsp
    • 更新了参考createLink的答案,我相信这就是你所需要的。具体的文件参考将付诸行动的params
    • 我的代码看起来像上面...获取图像作为字节数组并将其转换为文件,返回并尝试在 gsp 中显示。
    • 图片是临时字节数组,不是本地文件,而且只是一个小缩略图,最好采用第二种方法。
    【解决方案3】:

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

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

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

    您还需要指定是image/png还是其他格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多