调用企业微信api获取头像传到大华监控人脸库,大华sdk版本General_NetSDK_ChnEng_JAVA_Linux64_IS_V3.055.0000000.0.R.210602。
错误信息:
{error code: (0x80000000|2).参考 NetSDKLib.java } - {error info:网络错误,可能是因为网络超时}
//上传企业微信头像图片
String wxtokenMsg = HttpUtils.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken", "corpid=XXXX&corpsecret=XXXX");
String accessToken= (String) JSONObject.parseObject(wxtokenMsg).get("access_token");
String userMsg = HttpUtils.sendGet("https://qyapi.weixin.qq.com/cgi-bin/user/get", "access_token=" + accessToken + "&userid=XXXX");
String avatar= (String) JSONObject.parseObject(userMsg).get("thumb_avatar");//.get("avatar");
HttpURLConnection conn = (HttpURLConnection) new URL(avatar).openConnection();
conn.setReadTimeout(30000);
conn.setConnectTimeout(30000);
// 从网络连接读取数据
conn.setDoInput(true);
conn.setRequestMethod("GET");
String fileName = "";
FileItem item = null;
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
//获取文件名称
String newUrl = conn.getURL().getFile();
if (newUrl != null || newUrl.length() <= 0) {
newUrl = java.net.URLDecoder.decode(newUrl, "UTF-8");
int pos = newUrl.indexOf('?');
if (pos > 0) {
newUrl = newUrl.substring(0, pos);
}
pos = newUrl.lastIndexOf('/');
fileName = newUrl.substring(pos + 1);
}
BufferedInputStream bis = null;
HttpURLConnection conn1 = (HttpURLConnection) new URL(avatar).openConnection();
bis = new BufferedInputStream(conn1.getInputStream());
String type = HttpURLConnection.guessContentTypeFromStream(bis);
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "downloadFile"; //此处任务取值
if (!fileName.contains(".") && type != null) {
fileName = fileName + "." + type;//FileUtils.getFileType(type);
}
System.out.println("文件名为:" + fileName + " 大小" + (conn.getContentLength() / 1024) + "KB" + " contentType=" + type);
item = factory.createItem(textFieldName, type, false, fileName);
OutputStream os = item.getOutputStream();
int bytesRead;
if(conn.getContentLength()>0) {
byte[] buffer = new byte[1000000];
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
os.close();
is.close();
}
if (item != null) {
Memory memory=new Memory(1000000);
DiskFileItem fi = (DiskFileItem)item;
File file = fi.getStoreLocation();
ToolKits.ReadAllFileToMemory(file.getPath(),memory);//file.getPath(),memory);
boolean test = FaceRecognitionModule.addPerson("1", memory, "test", 1, false, "", 0, "");
}
错误原因是:Memory memory=new Memory(1000000) 的size设置太大。
其他可能导致错误的问题:
1、设备没有网络
2、接口超时时间设置小了
3、图片太大