java开发微信上传永久视频素材接口开发

/**
     * 模拟form表单的形式 ,上传文件 以输出流的形式把文件写入到url中,然后用输入流来获取url的响应 
     * @param url 请求地址 form表单url地址 
     * @param filePath 文件在服务器保存路径 
     * @param title 视频标题
     * @param introduction	视频描述
     * @return
     */
    public static String uploadVideo(String url, String filePath, String title, String introduction) {
    	String result = null;  
        
        HttpURLConnection downloadCon = null;
    	InputStream inputStream = null;
		try {
			URL urlFile = new URL(filePath);
    		downloadCon = (HttpURLConnection) urlFile.openConnection();
    		inputStream = downloadCon.getInputStream();
	    		
			URL urlObj = new URL(url); 
			HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();
			conn.setConnectTimeout(5000);
			conn.setReadTimeout(30000);  
            conn.setDoOutput(true);  
            conn.setDoInput(true);  
            conn.setUseCaches(false);  
            conn.setRequestMethod("POST"); 
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestProperty("Cache-Control", "no-cache");
            String boundary = "-----------------------------"+System.currentTimeMillis();
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary="+boundary);
			            
			OutputStream output = conn.getOutputStream();
			output.write(("--" + boundary + "\r\n").getBytes());
			String regex = ".*/([^\\.]+)";
			output.write(String.format("Content-Disposition: form-data; name=\"media\"; filename=\"%s\"\r\n", filePath.replaceAll(regex, "$1")).getBytes());  
			output.write("Content-Type: video/mp4 \r\n\r\n".getBytes());
			int bytes = 0;
			byte[] bufferOut = new byte[1024];
			while ((bytes = inputStream.read(bufferOut)) != -1) {
				output.write(bufferOut, 0, bytes);
			}
			inputStream.close();
			
			output.write(("--" + boundary + "\r\n").getBytes());
			output.write("Content-Disposition: form-data; name=\"description\";\r\n\r\n".getBytes());
			output.write(String.format("{\"title\":\"%s\", \"introduction\":\"%s\"}",title,introduction).getBytes());
			output.write(("\r\n--" + boundary + "--\r\n\r\n").getBytes());
			output.flush();
			output.close();
			inputStream.close();
			InputStream resp = conn.getInputStream();
			StringBuffer sb = new StringBuffer();
			while((bytes= resp.read(bufferOut))>-1)
			sb.append(new String(bufferOut,0,bytes,"utf-8"));
			resp.close();
			result = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();  
            return null;  
		}
		return result;
	}

public static void main(String[] args) throws IOException {  
    	String videoUrl = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=video";	//ACCESS_TOKEN改成自己的
    	String filePath = "www.xxx.com/xxx.mp4";	//服务器路径,比如说localhost:8080/WeChat/file/xxx.mp4
    	System.out.println(uploadVideo(videoUrl, filePath,"标题","描述"));
    }  


我这是上传服务器的文件到微信接口,上传本地文件请看原文

原文地址如下:http://www.lanrenjz.com/news/show-1776.html


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛哥是个大帅比

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值