How to upload an image to cloudant database? -
i trying upload image cloudant database, using cloudant client - 2.0.0
here's code looks :
public static void main(string[] args) throws malformedurlexception, exception { try { cloudantclient client = clientbuilder.account("uri").username(user).password(password).build(); system.out.println("server version: " + client.serverversion()); database db = client.database("rss_news", false); jsonobject obj = new jsonobject(); obj.put("_attachments", new attachment(encodefiletobase64binary("c:/users/public/pictures/sample pictures/desert.jpg"), "image/jpeg")); obj.put("name", "bhanwar"); db.save(obj); } catch (exception e) { e.printstacktrace(); } } // java type can serialized json private static string encodefiletobase64binary(string filename) throws ioexception { file file = new file(filename); system.out.println(file.getname()); byte[] bytes = loadfile(file); byte[] encoded = base64.encodebase64(bytes); system.out.println(bytes.length); string encodedstring = new string(encoded); system.out.println("---encodedstring---" + encodedstring); return encodedstring; } private static byte[] loadfile(file file) throws ioexception { inputstream = new fileinputstream(file); long length = file.length(); if (length > integer.max_value) { // file large } byte[] bytes = new byte[(int) length]; int offset = 0; int numread = 0; while (offset < bytes.length && (numread = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numread; } if (offset < bytes.length) { throw new ioexception("could not read file " + file.getname()); } is.close(); return bytes; }
but not work.
the object gets created on cloudant db, schema not "_attachments" mentioned in doc.
also, attributes length , revops 0.
please suggest.
Comments
Post a Comment