1、上传临时素材文件,获取media_id
废话不多说,直接上代码:
@Override
public Result<?> mediaUpload(MultipartFile file,String touser) {
//使用forest去获取token
String fetchToKen = sanXiaXingYunClient.fetchToken(corpid,corpsecret);
String accessToken = JSON.parseObject(fetchToKen).get("access_token").toString();
String line = null;//接口返回的结果
try {
// 换行符
final String newLine = "\r\n";
final String boundaryPrefix = "--";
// 定义数据分隔线
String BOUNDARY = "========7d4a6d158c9";
//使用时注意更换ip域名
String upUrl = "https://wwlocal.qq.com/cgi-bin/media/upload?access_token="+accessToken+"&type=image";
// 服务器的域名
URL url = new URL(upUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设置为POST情
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求头参数
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);
conn.setRequestProperty("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");
OutputStream out = new DataOutputStream(conn.getOutputStream());
// 上传文件
StringBuilder sb = new StringBuilder();
sb.append(boundaryPrefix);
sb.append(BOUNDARY);
sb.append(newLine);
// 文件参数,photo参数名可以随意修改
sb.append("Content-Disposition: form-data;name=\"image\";filename=\""
+ file.getOriginalFilename() + "\"" + newLine);
sb.append("Content-Type:application/octet-stream");
// 参数头设置完以后需要两个换行,然后才是参数内容
sb.append(newLine);
sb.append(newLine);
// 将参数头的数据写入到输出流中
out.write(sb.toString().getBytes());
// 读取文件数据
out.write(file.getBytes());
// 最后添加换行
out.write(newLine.getBytes());
// 定义最后数据分隔线,即--加上BOUNDARY再加上--。
byte[] end_data = (newLine + boundaryPrefix + BOUNDARY
+ boundaryPrefix + newLine).getBytes();
// 写上结尾标识
out.write(end_data);
out.flush();
out.close();
// 定义BufferedReader输入流来读取URL的响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
while ((line = reader.readLine()) != null) {
//通过JSON获取响应码中的media_id
String media_id = JSON.parseObject(line).get("media_id").toString();
return Result.OK(media_id);
}
} catch (IOException e) {
e.printStackTrace();
}
return Result.OK("发送失败");
}
2、获取临时素材
代码:
@Override
public File imageFetch(String mediaId) {
//通过forest获取token
String fetchToKen = sanXiaXingYunClient.fetchToken(corpid,corpsecret);
String toKen = JSON.parseObject(fetchToKen).get("access_token").toString();
try {
String url = null;
//GET_TMP_MATERIAL为你访问的地址
url = String.format(GET_TMP_MATERIAL, toKen, mediaId);
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("POST");
conn.connect();
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
String content_disposition = conn.getHeaderField("content-disposition");
//微信服务器生成的文件名称
String file_name ="";
String[] content_arr = content_disposition.split(";");
if(content_arr.length == 2){
String tmp = content_arr[1];
int index = tmp.indexOf("\"");
file_name =tmp.substring(index+1, tmp.length()-1);
}
//生成不同文件名称
File file = new File(file_name);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
byte[] buf = new byte[2048];
int length = bis.read(buf);
while(length != -1){
bos.write(buf, 0, length);
length = bis.read(buf);
}
bos.close();
bis.close();
return file;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
这是我之前看过的方法,实测可用,分享给大家!