本文共 7257 字,大约阅读时间需要 24 分钟。
获取二维码流程如下,重新整理总结:(java)
转自 :http://www.wxapp-union.com/forum.php?mod=viewthread&tid=3584&extra= 1.后台向微信发送请求,返回的为图片流
2.将微信返回的图片保存到服务器
3.将图片的地址返回到前台
4.前台处理就相当于处理 获取服务器的普通图片文件
如果图片有文件
但是提示格式不对或太大等 将文件格式转为txt 看看返回的信息 可能是token失效(2个小时) ,参数传递失败等
HttpClientConnectionManager 自定义工具类
- package cn.edu.hbcf.plugin.wx.utils;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.List;
- import java.util.Map;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.message.BasicHeader;
- import org.apache.http.protocol.HTTP;
-
- public class HttpClientConnectionManager {
-
-
-
-
-
-
-
-
-
- public static String httpPostWithJSON(String url, String json,String id)
- throws Exception {
- String result = null;
-
- String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(url);
- httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
- StringEntity se = new StringEntity(json);
- se.setContentType("application/json");
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
- httpPost.setEntity(se);
-
- HttpResponse response = httpClient.execute(httpPost);
- if (response != null) {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null) {
- InputStream instreams = resEntity.getContent();
-
-
-
- String uploadSysUrl = "D:\\upload"+"/";
- File saveFile = new File(uploadSysUrl+id+".jpg");
-
- if (!saveFile.getParentFile().exists()) {
-
- saveFile.getParentFile().mkdirs();
- }
- saveToImgByInputStream(instreams, uploadSysUrl, id+".jpg");
- }
- }
- return result;
- }
-
-
-
-
-
-
-
-
- public static int saveToImgByInputStream(InputStream instreams,String imgPath,String imgName) throws FileNotFoundException{
-
- int stateInt = 1;
- File file=new File(imgPath,imgName);
- FileOutputStream fos=new FileOutputStream(file);
- if(instreams != null){
- try {
-
- byte[] b = new byte[1024];
- int nRead = 0;
- while ((nRead = instreams.read(b)) != -1) {
- fos.write(b, 0, nRead);
- }
-
- } catch (Exception e) {
- stateInt = 0;
- e.printStackTrace();
- } finally {
-
- try {
- fos.flush();
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- return stateInt;
- }
- public static boolean exists(String imgPath){
- File saveFile = new File(imgPath);
- if (!saveFile.getParentFile().exists()) {
- return false;
- }else{
-
- if(saveFile.length()>0){
- System.out.println("--------------------------------"+saveFile.length());
- return true;
- }else{
- return false;
- }
- }
-
- }
-
-
-
-
-
-
-
-
-
- public static String sendGet(String url, String param) {
- String result = "";
- BufferedReader in = null;
- try {
- String urlNameString = url + "?" + param;
- System.out.println(urlNameString+"........");
- URL realUrl = new URL(urlNameString);
-
- URLConnection connection = realUrl.openConnection();
-
- connection.setRequestProperty("accept", "*/*");
- connection.setRequestProperty("connection", "Keep-Alive");
- connection.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
-
- connection.connect();
-
- Map<String, List<String>> map = connection.getHeaderFields();
-
- for (String key : map.keySet()) {
- System.out.println(key + "--->" + map.get(key));
- }
-
- in = new BufferedReader(new InputStreamReader(
- connection.getInputStream()));
- String line;
- while ((line = in.readLine()) != null) {
- result += line;
- }
- } catch (Exception e) {
- System.out.println("发送GET请求出现异常!" + e);
- e.printStackTrace();
- }
-
- finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return result;
- }
-
-
-
- public static Object httpPostWithJSON2(String url, String json,String id)
- throws Exception {
-
- InputStream instreams = null;
- String encoderJson = URLEncoder.encode(json, HTTP.UTF_8);
- DefaultHttpClient httpClient = new DefaultHttpClient();
- HttpPost httpPost = new HttpPost(url);
- httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
- StringEntity se = new StringEntity(json);
- se.setContentType("application/json");
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"UTF-8"));
- httpPost.setEntity(se);
-
- HttpResponse response = httpClient.execute(httpPost);
- if (response != null) {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null) {
- instreams = resEntity.getContent();
-
- }
- }
- return instreams;
- }
-
- }
调用工具类中的方法 获取图片 参数需要前台传给
-
-
-
-
-
-
-
- @RequestMapping(value = "/createwxaqrcode")
- @ResponseBody
- public Object createwxaqrcode(String access_token,String path,String width, String id ){
- AgentDTO agentDTO = new AgentDTO();
- String URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode";
-
- String agentImgDownloadUrl = "D:\\upload";
- try {
- AgentReqView agentResView = new AgentReqView();
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("path", path);
- map.put("width", width);
- JSONObject json = JSONObject.fromObject(map);
- HttpClientConnectionManager.httpPostWithJSON(URL+"?access_token="+ access_token, json.toString(),id );
- String downloadUrl = agentImgDownloadUrl+ id+ "/";
-
- String downloadfileUrl = downloadUrl + id + ".jpg";
- agentResView.setDownloadfileUrl(downloadfileUrl);
- agentDTO.setResultCode("200");
- agentDTO.setDesc("成功");
- agentDTO.setBody(agentResView);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return agentDTO;
- }
以下为返回值格式定义,可自由定义
- import java.util.List;
-
- public class AgentDTO {
- private String resultCode;
- private String desc;
- private AgentReqView body;
-
- public AgentDTO (){
-
- }
-
- public AgentDTO (String resultCode,String desc){
- this.setResultCode(resultCode);
- this.setDesc(desc);
-
- }
- public String getResultCode() {
- return resultCode;
- }
- public void setResultCode(String resultCode) {
- this.resultCode = resultCode;
- }
- public String getDesc() {
- return desc;
- }
- public void setDesc(String desc) {
- this.desc = desc;
- }
-
- public AgentReqView getBody() {
- return body;
- }
-
- public void setBody(AgentReqView body) {
- this.body = body;
- }
-
-
-
- }
- public class AgentReqView {
-
- private String downloadfileUrl;
-
- public String getDownloadfileUrl() {
- return downloadfileUrl;
- }
-
- public void setDownloadfileUrl(String downloadfileUrl) {
- this.downloadfileUrl = downloadfileUrl;
- }
-
-
- }
转载地址:http://ifmwb.baihongyu.com/