我使用XML-RPC上传图片,这对我有用,但速度太慢了! 图像大小为3.5M,上传需要3分钟
HashMap map = new HashMap();
map.put("res_model", res_model);
map.put("res_name", res_name);
map.put("type", "binary");
map.put("res_field","image");
map.put("res_id", res_id);
map.put("name", "image");
map.put("datas", image2Base64(imagePath));
models.execute("execute_kw", asList(
db, uid, password,
"ir.attachment", "create",
asList(map)
));
这是image2Base64方法:
private String image2Base64(String path){
File file = new File(path);
String str = null;
try {
// Reading a Image file from file system
FileInputStream imageInFile = new FileInputStream(file);
byte imageData[] = new byte[(int) file.length()];
imageInFile.read(imageData);
imageInFile.close();
str = Base64.encode(imageData);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return str;
}