我試圖從OpenLDAP服務器保存映像。它採用二進制格式,我的所有代碼都可以正常工作,但是圖像已損壞。使用Groovy從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庫 「形象-JPEG」,實際上是二進制和不是一個字符串?是否有更好的簡單庫可用於從LDAP服務器讀取二進制數據?從查看Apache郵件列表someone else had a similar issue,但我找不到線程中的解決方案。
技術堆棧
- 的Grails 2.2.1
- Apache LDAP API 1.0.0 M16
我包含上面的PHP代碼,以顯示它確實是二進制,而不是Base64編碼。好的建議,但。 – arcdegree