2012-05-31 86 views
0

我運行在Java中對GWT + GAE應用在谷歌應用程序引擎發送HTML5畫布圖像作爲嵌入圖像的電子郵件

我想找到自己的HTML5畫布的(GWT Canvas類)中的內容,並讓他們保存在永久和網絡尋址文件

例子:http://myserver.com/images/image_434.png

當我使用canvas2.toDataUrl()...

1是否有可能這些內容發佈到PHP在畫布內容Web API通過HTTP請求a然後使用PHP(在我的服務器上)解碼64位圖像並將其寫入文件並返回固定鏈接。

或者

2-是它在某種程度上能夠RPC的圖像數據發送到服務器側,將其保存到文件中(ImageIO的被阻止在GAE),然後嵌入該文件以某種方式在電子郵件和電子郵件它到我的服務器。

我很困惑,因爲:

方法1:我懷疑會工作,張貼那麼久,我不知道一個參數,但我有一種直覺,將無法正常工作。方法2:如果我能弄清楚如何在沒有固定文件URL的情況下在郵件中嵌入圖像(通過以某種方式直接將流寫入消息體)可能會起作用。

正如你所看到的,我通常對此感到困惑。這樣做不應該很難,我不能成爲唯一一個嘗試這樣做的人......儘管我現在一直在尋找3天。

由於

+1

你的意思是「Base64編碼圖像」,而不是'64位圖像'?實際上 –

+0

是的......我想它了剛纔......要編輯/回答這個問題 –

回答

0
  • 客戶端側(GWT):

1-得到base64編碼圖像URI

String imageData= canvas2.toDataUrl(); 

2-通過RPC調用向發送的圖像數據服務器端

jdbc.saveImage(imageData,callback); 
  • 服務器端(GAE):

3-做一個HTTP POST請求到Web服務器API

 URL url = new URL("http://myserver.com/my_images_folder/save_image.php"); 
     URLConnection conn = url.openConnection(); 
     conn.setReadTimeout(15000); //set a large time out since we're saving images 
     conn.setDoOutput(true); 
     OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
     wr.write(data); 
     wr.flush(); 

     // Get the response which contains the image file name 
     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      resa+=line; 
     } 
     wr.close(); 
     System.out.println("close1"); 
     rd.close(); 
     System.out.println("Received: "+line); 
  • 服務器端(你的Web服務器-php API):

4-將圖像保存到文件服務器並返回圖像文件名稱

if (isset($GLOBALS["HTTP_RAW_POST_DATA"])){ 
     $imageData=$GLOBALS['HTTP_RAW_POST_DATA']; 

     //using a timestamp to create unique file names 
     //you can pass file name in params if you like instead 
     $fileName='User_Images_'.time().'.png'; 

     // Remove the headers (data:,) part. 
     $filteredData=substr($imageData, strpos($imageData, ",")+1); 

     // Need to decode base64 encoded image 
     $unencodedData=base64_decode($filteredData); 

     $fp = fopen($fileName, 'wb'); 
     fwrite($fp, $unencodedData); 
     fclose($fp); 
    $fileName2='http://myserver.com/my_images_folder/'.$fileName; 

    //return the file name 
    echo($fileName); 
}else{ 
    echo('no data posted'); 
} 

現在,我對文件有一個很難的永久鏈接,我可以將它嵌入到電子郵件中,並用它來做其他事情。請參閱下面的參考3內嵌嵌入(這需要一個文件或URL,現在我們也很難URL到我們的Web服務器上的圖像,我們可以通過電子郵件發送出來)

+0

參考1:http://permadi.com/blog/2010/10/html5-saving-canvas-image-data-using -php和 - AJAX /#viewSource –

+0

文獻2:http://www.exampledepot.com/egs/java.net/post.html –

+0

文獻3:http://java.sun.com/developer/onlineTraining/ JavaMail的/ contents.html#IncludingImagesWithHTML –

相關問題