(其中:sendUrl为https://api.weixin.qq.com/wxa/img_sec_check?access_token=自己的access_token,imgUrl为服务器文件地址,比如/home/data/1.png)
/**
*
* @param sendUrl
* @param imgUrl
* @return
* @throws IOException
*/
public static String checkImgUrl(String sendUrl, String imgUrl) throws IOException {
String result = null;
URL urlObj = new URL(sendUrl);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod(“POST”);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestProperty(“Connection”, “Keep-Alive”);
con.setRequestProperty(“Charset”, “UTF-8”);
String BOUNDARY = “----------” + System.currentTimeMillis();
con.setRequestProperty(“Content-Type”, “multipart/form-data; boundary=” + BOUNDARY);
StringBuilder sb = new StringBuilder();
sb.append("–");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append(“Content-Disposition: form-data;name=“media”;filename=”" + imgUrl + “”\r\n");
sb.append(“Content-Type:application/octet-stream\r\n\r\n”);
byte[] head = sb.toString().getBytes(“utf-8”);
OutputStream out = new DataOutputStream(con.getOutputStream());
out.write(head);
FileInputStream fis = new FileInputStream(imgUrl);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream in = new DataInputStream(bis);
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
byte[] foot = ("\r\n–" + BOUNDARY + “–\r\n”).getBytes(“utf-8”);
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
return result;
}
最后返回成功,返回值{"errcode":0,"errmsg":"ok"}
{"errcode":87014,"errmsg":"risky content hint: [a1in802458622]"}
老哥,你这个代码贴上去一直在报41005啊
sb.append(“Content-Disposition: form-data;name=“media”;filename=”" + imgUrl + “”\r\n");
老哥 我复制的这里会报错啊。
中文的引号是该怎么用呢
{"errMsg":"invokeService:ok","data":{"Response":{"Error":{"Code":"FailedOperation.UnKnowError","Message":"内部错误"}, 咋回事呢?求解!
可以直接云调用不需要后台也可以,他这个文章可以:
https://mp.weixin.qq.com/s/o77bLIfCEkHq3acmqS4Tyw
楼主,参考了你的代码,但我的图片是存在数据库中,取出来是一个url,没有相对地址,使用你这里的代码,图片的地址中的https://XXX,就变成了https:/XXX,少了一个"/",请问有办法解决吗?
返回个这个什么意思啊
{"errcode":-1,"errmsg":"system error hint: [R7oLmA02662372]"}
为什么 我写本地图片和网络地址 都是报40015 错误
imgUrl 是本地图片文件吧,否则FileInputStream fis = new FileInputStream(imgUrl);这句会报错的,微信这个接口好像不支持网络图片安全检测
老哥,{"errcode":41005,"errmsg":"media data missing hint: [04622756]"} 我一直提示这个错误-
Content-Disposition:form-data;name="media";filename="image/fishing.jpg"
Content-Type:application/octet-stream 这个是请求的参数
中文引号就试试转移吧 要不就用英文的