2011-10-24 50 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()}"/>