HttpClient获取Accesstoken以及上传多媒体文件

使用 HttpClient4.3版本进行微信开发。使用了httpclient的get请求,以及上传文件。



import java.io.File;
import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 对应微信公众平台API的:基础支持
 * 
 * @author zyk
 * 
 */
@SuppressWarnings("deprecation")
public class BaseSupport {
	/**
	 * 获取微信access_token
	 * 
	 * @param apiurl
	 *            微信APIurl
	 * @param appid
	 *            微信appid
	 * @param secret
	 *            微信secret
	 * @return access_token字符串
	 * @throws IOException
	 */
	public static String GetAccesstoken(String apiurl, String appid,
			String secret) throws IOException {
		String turl = String.format(
				"%s?grant_type=client_credential&appid=%s&secret=%s", apiurl.trim(),
				appid.trim(), secret.trim());
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(turl);
		String access_token = null;
		CloseableHttpResponse httpResponse = null;
		try {
			httpResponse = httpClient.execute(httpGet);
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				HttpEntity entity = httpResponse.getEntity();
				String entityString = EntityUtils.toString(entity);
				JSONObject fromObject = JSONObject.fromObject(entityString);
				access_token = fromObject.get("access_token").toString();
				
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(httpResponse != null){
				httpResponse.close();
			}
			if(httpClient!= null){
				httpClient.close();
			}
		}
		return access_token;
	}
	
	/**
	 * 上传多媒体文件
	 * @param access_token
	 * @param type 图片(image)、语音(voice)、视频(video)和缩略图(thumb)
	 * 图片(image): 1M,支持JPG格式
	 * 语音(voice):2M,播放长度不超过60s,支持AMR\MP3\SPEEX格式
	 * 视频(video):10MB,支持MP4格式
	 * 缩略图(thumb):64KB,支持JPG格式
	 * @param fileUrl
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings("deprecation")
	public static String uploadMedia(String access_token, String type,
			String fileUrl) throws IOException {
		String media_id = null;
		CloseableHttpResponse response = null;
		String url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="
				+ access_token + "&type=" + type;
		CloseableHttpClient httpclient = null;
		httpclient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		FileBody bin = new FileBody(new File(fileUrl));
		MultipartEntity mpEntity = new MultipartEntity(); // 文件传输
		mpEntity.addPart("media", bin);
		httpPost.setEntity(mpEntity);
		try {
			response = httpclient.execute(httpPost);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				String jsonString = EntityUtils.toString(entity);
				JSONObject fromObject = JSONObject.fromObject(jsonString);
				Object media_idObject = fromObject.get("media_id");
				if (media_idObject != null) {
					media_id = media_idObject.toString();
				} else {
					System.out.println(fromObject.toString());
				}
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(response != null){
				response.close();
			}
			if(httpclient!= null){
				httpclient.close();
			}
		}
		return media_id;
	}
}
MultipartEntity 已经被废弃,暂时没有找到新的。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值