【问题标题】:Reading and saving binary image from OpenLDAP server using Groovy使用 Groovy 从 OpenLDAP 服务器读取和保存二进制图像
【发布时间】:2013-03-29 16:43:16
【问题描述】:

我正在尝试从 OpenLDAP 服务器保存图像。它是二进制格式,我的所有代码似乎都可以工作,但是图像已损坏。

然后我尝试在 PHP 中执行此操作并成功,但我想在 Grails 项目中执行此操作。

PHP 示例(作品)

<?php
    $conn = ldap_connect('ldap.example.com') or die("Could not connect.\n");
    ldap_set_option($conn, LDAP_OPT_PROTOCOL_VERSION, 3);
    $dn = 'ou=People,o=Acme';
    $ldap_rs = ldap_bind($conn) or die("Can't bind to LDAP");
    $res = ldap_search($conn,$dn,"someID=123456789");
    $info = ldap_get_entries($conn, $res);
    $entry = ldap_first_entry($conn, $res);
    $jpeg_data = ldap_get_values_len( $conn, $entry, "someimage-jpeg");
    $jpeg_filename = '/tmp/' . basename( tempnam ('.', 'djp') );
    $outjpeg = fopen($jpeg_filename, "wb");
    fwrite($outjpeg, $jpeg_data[0]);
    fclose ($outjpeg);
    copy ($jpeg_filename, '/some/dir/test.jpg');
    unlink($jpeg_filename);
?>

Groovy 示例(不起作用)

def ldap = org.apache.directory.groovyldap.LDAP.newInstance('ldap://ldap.example.com/ou=People,o=Acme')

ldap.eachEntry (filter: 'someID=123456789') { entry ->

    new File('/Some/dir/123456789.jpg').withOutputStream {
        it.write entry.get('someimage-jpeg').getBytes()  // File is created, but image is corrupted (size also doesn't match the PHP version)
    }

}

如何告诉 Apache LDAP 库“image-jpeg”实际上是二进制而不是字符串?是否有更好的简单库可用于从 LDAP 服务器读取二进制数据?通过查看 Apache 邮件列表,someone else had a similar issue,但我在线程中找不到解决方案。

技术栈

【问题讨论】:

    标签: grails groovy ldap apacheds


    【解决方案1】:

    你检查过图片属性值是否是base-64编码的吗?

    【讨论】:

    • 我包含了上面的 PHP 代码以表明它确实是二进制而不是 Base64 编码的。不过建议很好。
    【解决方案2】:

    我找到了答案。 Apache Groovy LDAP 库在后台使用 JNDI。使用 JNDI 时,某些条目会自动读取为二进制文件,但如果您的 LDAP 服务器使用自定义名称,库将不知道它是二进制文件。

    对于那些在使用 Grails 时遇到此问题的人,以下是将特定条目设置为二进制格式的步骤。

    • 创建一个名为“jndi.properties”的新属性文件并将其添加到您的 grails-app/conf 目录(此文件夹中的所有属性文件都自动包含在类路径中)

    • 在属性文件中添加一行图像变量的名称:

      java.naming.ldap.attributes.binary=some_custom_image

    • 保存文件并运行 Grails 应用程序

    这里是一些将二进制条目保存到文件的示例代码。

    def ldap = LDAP.newInstance('ldap://some.server.com/ou=People,o=Acme')      
    ldap.eachEntry (filter: 'id=1234567') { entry ->             
        new File('/var/dir/something.jpg').withOutputStream {          
            it.write entry.image          
        }         
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-28
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      相关资源
      最近更新 更多