Image 如何将图像上载到cloudant数据库?
我正在尝试使用cloudant客户端-2.0.0将图像上载到cloudant数据库 我的代码如下所示:Image 如何将图像上载到cloudant数据库?,image,upload,attachment,cloudant,Image,Upload,Attachment,Cloudant,我正在尝试使用cloudant客户端-2.0.0将图像上载到cloudant数据库 我的代码如下所示: public static void main(String[] args) throws MalformedURLException, Exception { try { CloudantClient client = ClientBuilder.account("uri").username(user).password(password).build()
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();
}
}
// A Java type that can be serialized to 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 is = new FileInputStream(file);
long length = file.length();
if (length > Integer.MAX_VALUE)
{
// File is too 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 completely read file " + file.getName());
}
is.close();
return bytes;
}
publicstaticvoidmain(字符串[]args)引发畸形的异常异常
{
尝试
{
CloudantClient=ClientBuilder.account(“uri”).username(user).password(password).build();
System.out.println(“服务器版本:+client.serverVersion());
Database db=client.Database(“rss_新闻”,false);
JSONObject obj=新的JSONObject();
obj.put(“_attachments”,新的附件(encodeFileToBase64Binary(“C:/Users/Public/Pictures/Sample Pictures/Desert.jpg”),“image/jpeg”);
obj.put(“名称”、“bhanwar”);
db.save(obj);
}
捕获(例外e)
{
e、 printStackTrace();
}
}
//可以序列化为JSON的Java类型
私有静态字符串encodeFileToBase64Binary(字符串文件名)引发IOException
{
文件=新文件(文件名);
System.out.println(file.getName());
字节[]字节=加载文件(文件);
byte[]encoded=Base64.encodeBase64(字节);
System.out.println(字节.长度);
字符串编码字符串=新字符串(已编码);
System.out.println(“--encodedString----”+encodedString);
返回编码环;
}
私有静态字节[]加载文件(文件文件)引发IOException
{
InputStream is=新文件InputStream(文件);
long length=file.length();
if(长度>整数最大值)
{
//文件太大
}
字节[]字节=新字节[(int)长度];
整数偏移=0;
int numRead=0;
而(偏移量=0)
{
偏移量+=numRead;
}
if(偏移量<字节长度)
{
抛出新IOException(“无法完全读取文件”+file.getName());
}
is.close();
返回字节;
}
但这是行不通的
该对象是在cloudant数据库上创建的,但架构不像文档中提到的那样是“\u附件”
此外,length和revops等属性为0
请建议