解决微信返回码为40113,错误信息"errmsg":"unsupported file type

解决微信返回码为40113,错误信息"errmsg":"unsupported file type"

// PHP代码
$file_name = md5(microtime()*1000000);
		$file_type = 'image';
        $post_data = array(
            'media'=>new CurlFile(realpath($url),$file_type,$file_name),   
		);
		$user_info = $this->Post($url,$post_data);

这样微信会返回一个40113的错误,原因是因为$file_name没有添加后缀,微信不能辨别文件类型。改成以下就可以了:

$file_name = md5(microtime()*1000000).'jpg';

这样就可以了。

展开阅读全文

微信上传文件素材,接口返回"errcode":40113,"errmsg":"unsupported file type"

08-04

如题,下面是代码:rn[code=java]rn public String sendFileToWechat(String access_token, String fileType, File file, MultipartFile mfile) throws Exception rn String result = null;rn if (!file.exists() || !file.isFile()) rn throw new IOException("文件不存在");rn rn rn HttpURLConnection conn = null;rn String BOUNDARY = "----------" + System.currentTimeMillis();rn rn try rn rn URL urlObj = new URL("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=" + access_token + "&type=" + fileType + "");rn conn = (HttpURLConnection) urlObj.openConnection();rn conn.setDoInput(true);rn conn.setDoOutput(true);rn conn.setUseCaches(false); // post方式不能使用缓存rn // 设置请求头信息rn conn.setRequestMethod("POST"); // 以Post方式提交表单,默认 GET 方式rn conn.setRequestProperty("Connection", "Keep-Alive");rn conn.setRequestProperty("Charset", "UTF-8");rn conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); rn conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);rn rn // 获得输出流rn OutputStream out = new DataOutputStream(conn.getOutputStream());rn rn rn /*StringBuffer strBuf = new StringBuffer();rn strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");rn strBuf.append("Content-Disposition: form-data; name=\"type\"\r\n\r\n");rn strBuf.append(fileType);rn out.write(strBuf.toString().getBytes());*/rn rn rn StringBuilder sb = new StringBuilder();rn sb.append("\r\n").append("--").append(BOUNDARY).append("\r\n"); // 必须多两道线rn sb.append("Content-Disposition: form-data;name=\"media\";filename=\"" + mfile.getName() + "\"\r\n");rn String contentType = new MimetypesFileTypeMap().getContentType(file);rn sb.append("Content-Type:"+contentType+"\r\n\r\n");rn byte[] head = sb.toString().getBytes();rn // 输出表头rn out.write(head);rn rn // 文件正文部分rn // 把文件已流文件的方式 推入到url中rn DataInputStream in = new DataInputStream(new FileInputStream(file));rn int bytes = 0;rn byte[] bufferOut = new byte[1024];rn while ((bytes = in.read(bufferOut)) != -1) rn out.write(bufferOut, 0, bytes);rn rn in.close();rn rn // 结尾部分rn byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");// 定义最后数据分隔线rn out.write(foot);rn out.flush();rn out.close();rn rn StringBuffer buffer = new StringBuffer();rn BufferedReader reader = null;rn // 定义BufferedReader输入流来读取URL的响应rn reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));rn rn String line = null;rn while ((line = reader.readLine()) != null) rn buffer.append(line);rn rn if (result == null) rn result = buffer.toString();rn rn rn reader.close(); rn reader = null;rn rn catch (IOException e) rn e.printStackTrace();rn throw new IOException("数据读取异常");rn finally rn if (conn != null) rn conn.disconnect(); rn conn = null; rn rn rnrn return result;rn rn[/code] 论坛

没有更多推荐了,返回首页