__问题一:__小程序-内容安全 security.imgSecCheck 报错一直为47001,请帮忙看能否查一下是什么原因?{“errcode”:47001,“errmsg”:“data format error hint: [pmW6Ia0575c393]”}
问题二:security.imgSecCheck 这个接口中的图片是不是要与发起post连接的域名要一致?
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定义数据分隔线
String BOUNDARY = "============" + "1234567890" + "============";
// 服务器的域名
URL url = new URL(urls); // 建立http连接
System.out.println("url:"+url); // 创建url资源
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 设置允许输出
conn.setRequestMethod("POST"); // 设置维持长连接
conn.setDoOutput(true); // 设置允许输入
conn.setDoInput(true); // 设置不用缓存
conn.setUseCaches(false); // 设置传递方式
conn.setRequestProperty("Connection", "Keep-Alive"); // 设置文件字符集:
conn.setRequestProperty("Charset", "UTF-8"); // 转换为字节数组
conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
//获取图片URL
imgUrl = new String(readFromUrl(storageUrl),"UTF-8");
byte[] imgByte = readFromUrl(imgUrl);
System.out.println("imgUrl:" + imgUrl);
// 上传文件
StringBuilder sb = new StringBuilder();
StringBuffer sb1=new StringBuffer();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
// 文件参数
sb.append("Content-Disposition: form-data;name=\"image\";filename=\"" + imgUrl +"\"");
sb.append(newLine);
sb.append("Content-Type:"+URLConnection.guessContentTypeFromName(imgUrl));
// 参数头设置完以后需要两个换行,然后才是参数内容
sb.append(newLine);
sb.append(newLine);
System.out.println("-----111111-----");
conn.connect();
OutputStream out = new DataOutputStream(conn.getOutputStream());
byte[] head = sb.toString().getBytes("utf-8");
out.write(head);
System.out.println("sb:\n\r");
System.out.println(sb.toString());
InputStream in = new URL(imgUrl).openStream();
byte[] bufferOut = new byte[1024];
int bytesRead = -1;
while ((bytesRead = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytesRead);
}
in.close();
System.out.println(imgUrl);
byte[] foot = ("\r\n" + boundaryPrefix + BOUNDARY + boundaryPrefix + "\r\n").getBytes("utf-8");
System.out.println("\r\n" + boundaryPrefix + BOUNDARY + boundaryPrefix + "\r\n");
out.write(foot);
out.flush();
out.close();
LOG是:
tomcat default url:https://api.weixin.qq.com/wxa/msg_sec_check?access_token=31_XXXXXXXXNIQ
tomcat default imgUrl:https://XXX/9D9000001B78E1F0014601F49.jpg
tomcat default -----111111-----
tomcat default sb:
tomcat default
tomcat default --============1234567890============
tomcat default Content-Disposition: form-data;name="image";filename="https://XXX/9D9000001B78E1F0014601F49.jpg"
tomcat default Content-Type:image/jpeg
tomcat default
tomcat default https://XXX/9D9000001B78E1F0014601F49.jpg
tomcat default
tomcat default --============1234567890============--