【问题标题】:Sending HTML5 Canvas image as embedded image in email over google app engine通过谷歌应用引擎将 HTML5 Canvas 图像作为嵌入图像发送到电子邮件中
【发布时间】:2012-05-31 21:57:24
【问题描述】:

我在 GWT + GAE 上运行 Java 应用程序

我想获取我的 html5 Canvas(GWT Canvas 类)的内容并将它们保存在一个永久的 Web 可寻址文件中

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

当我使用 canvas2.toDataUrl() 获取画布内容时 ...

1- 是否可以通过 HTTP 请求将这些内容发布到 PHP Web API,然后使用 PHP(在我的服务器上)解码 64 位图像并将其写入文件并返回永久链接。

或者

2- 是否有可能以某种方式将 RPC 的图像数据发送到服务器端,将其保存到文件中(ImageIO 在 GAE 中被阻止),然后以某种方式将该文件嵌入到电子邮件中并将其通过电子邮件发送到我的服务器。

我很困惑,因为:

方法1:我怀疑是否行得通,将参数发布那么长,我不确定,但我有一种直觉,行不通。

方法 2:如果我能弄清楚如何在没有可靠文件 URL 的情况下将图像嵌入邮件中(通过以某种方式直接将流写入邮件正文)可能会起作用。

如您所见,我通常对此感到困惑。这样做应该不难,而且我不能是唯一一个尝试这样做的人......尽管我已经搜索了 3 天。

谢谢

【问题讨论】:

  • 您的意思是“Base64 编码图像”而不是“64 位图像”吗?
  • 是的实际上...我刚刚想通了...要编辑/回答它

标签: java html google-app-engine gwt canvas


【解决方案1】:
  • 客户端(GWT):

1- 获取base64编码的图片URI

String imageData= canvas2.toDataUrl();

2- 通过 RPC 调用将图像数据发送到服务器端

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

3- 向您的 Web 服务器 API 发出 HTTP Post 请求

        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,我们可以通过电子邮件将其发送出去)

猜你喜欢
  • 2013-02-26
  • 2017-07-28
  • 1970-01-01
  • 2012-03-03
  • 2012-06-13
  • 2018-09-20
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
相关资源
最近更新 更多