2

我正在尝试从 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 邮件列表,其他人也遇到了类似的问题,但我在线程中找不到解决方案。

技术栈

4

2 回答 2

1

您是否检查过图像属性值是否是 base-64 编码的?

于 2013-03-30T13:38:44.453 回答
0

我找到了答案。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          
    }         
}
于 2013-04-01T16:19:42.290 回答