上传附件接口说明(https://docs.lanxin.cn/base.html#nav3-2-1)
步骤
1. 定义数据分隔线boundary( 长度建议10位以上 )
2. 与微/蓝信服务器建立连接(HttpURLConnection)
3. 获取要上传的文件输出流,构造请求体等相关参数开始向微/蓝信服务器写数据(form-data中媒体文件标识,有filename、filelength、content-type等信息,其中file的表单名称为media )。
4. 上传文件后读取微/蓝信服务器返回的内容,并解析为json格式返回(定义BufferedReader输入流来读取URL的响应)
方案一(JAVA代码)
(适用于微信和蓝信)
/** * 上传蓝信附件 * @param accessToken * @param filename * @return */ public JSONObject uploadMedia(String accessToken,String filename) { JSONObject jsonObject = null; String UPLOAD_WECHAT_URL = "https://login.lanxin.cn/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"; String last_lanx_url = UPLOAD_WECHAT_URL.replace("ACCESS_TOKEN", accessToken); // 定义数据分割符 String boundary = "----------sunlight"; // try { File file=new File(filename); // URL url = new URL(last_lanx_url); URL url = new URL(null,last_lanx_url,new sun.net.www.protocol.https.Handler()); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); //发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); OutputStream out = new DataOutputStream(conn.getOutputStream()); byte[] end_data = (" --" + boundary + "-- ").getBytes();// 定义最后数据分隔线 StringBuilder sb = new StringBuilder(); sb.append("--"); sb.append(boundary); sb.append(" "); sb.append("Content-Disposition: form-data;name="media";filename="" + file.getName() + "" "); sb.append("Content-Type:application/octet-stream "); byte[] data = sb.toString().getBytes(); out.write(data); DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = 0; byte[] bufferOut = new byte[1024 * 8]; while ((bytes = in.read(bufferOut)) != -1) { out.write(bufferOut, 0, bytes); } out.write(" ".getBytes()); // 多个文件时,二个文件之间加入这个 in.close(); out.write(end_data); out.flush(); out.close(); // 定义BufferedReader输入流来读取URL的响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = null; StringBuffer buffer = new StringBuffer(); while ((line = reader.readLine()) != null) { buffer.append(line); } System.out.println("请求蓝信上传附件接口响应报文内容为:"+buffer.toString()); jsonObject = JSONObject.parseObject(buffer.toString()); } catch (Exception e) { System.out.println("POST请求蓝信上传附件接口出现异常");
e.printStackTrace(); return null; } return jsonObject; }
方案二 (JAVA代码)
(适用于微信/蓝信)
import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import net.sf.json.JSONObject; public class WXUpload { private static final String upload_wechat_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"; public static JSONObject upload(String accessToken, String type, File file) { JSONObject jsonObject = null; String last_wechat_url = upload_wechat_url.replace("ACCESS_TOKEN", accessToken).replace("TYPE", type); // 定义数据分割符 String boundary = "----------sunlight"; try { URL uploadUrl = new URL(last_wechat_url); HttpURLConnection uploadConn = (HttpURLConnection) uploadUrl.openConnection(); uploadConn.setDoOutput(true); uploadConn.setDoInput(true); uploadConn.setRequestMethod("POST"); // 设置请求头Content-Type uploadConn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); // 获取媒体文件上传的输出流(往微信服务器写数据) OutputStream outputStream = uploadConn.getOutputStream(); // 从请求头中获取内容类型 String contentType = "Content-Type: " + getContentType(); // 请求体开始 outputStream.write(("--" + boundary + " ").getBytes()); outputStream.write(String.format("Content-Disposition: form-data; name="media"; filename="%s" ", file.getName()).getBytes()); outputStream.write(String.format("Content-Type: %s ", contentType).getBytes()); // 获取媒体文件的输入流(读取文件) DataInputStream in = new DataInputStream(new FileInputStream(file)); byte[] buf = new byte[1024 * 8]; int size = 0; while ((size = in.read(buf)) != -1) { // 将媒体文件写到输出流(往微信服务器写数据) outputStream.write(buf, 0, size); } // 请求体结束 outputStream.write((" --" + boundary + "-- ").getBytes()); outputStream.close(); in.close(); // 获取媒体文件上传的输入流(从微信服务器读数据) InputStream inputStream = uploadConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer buffer = new StringBuffer(); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); // 释放资源 inputStream.close(); inputStream = null; uploadConn.disconnect(); // 使用json解析 jsonObject = JSONObject.fromObject(buffer.toString()); System.out.println(jsonObject); } catch (Exception e) { System.out.println("上传文件失败!"); e.printStackTrace(); } return jsonObject; } // 获取文件的上传类型,图片格式为image/png,image/jpeg等。非图片为application /octet-stream private static String getContentType() throws Exception { return "application/octet-stream"; } }