| | |
| | | import net.sf.json.JSONException; |
| | | import net.sf.json.JSONObject; |
| | | import org.apache.commons.codec.binary.Base64; |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.apache.commons.io.IOUtils; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | |
| | | //生成小程序二维码地址(方形) |
| | | public static final String MAKE_TWOCODE_SQUARE_URL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="; |
| | | //生成小程序二维码地址(圆形) |
| | | public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token="; |
| | | public static final String MAKE_TWOCODE_ROUND_URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}"; |
| | | //生成无限二维码 |
| | | public static final String URL_UNLIMIT_SQUARE = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}"; |
| | | ////////////////////////////////////////////////// |
| | |
| | | |
| | | return imgUrl; |
| | | } |
| | | |
| | | /**生成有限二维码,返回临时文 |
| | | * @param at 微信token |
| | | * @param page 跳转链接 |
| | | * @param width 宽度 |
| | | * @param autoColor 默认false |
| | | * @param lineColor 默认null |
| | | * @param isHyaline 默认false |
| | | * @return |
| | | */ |
| | | public static File createLimitedQrCode(String at, String page, int width, boolean autoColor, |
| | | JSONObject lineColor, boolean isHyaline) { |
| | | String imgUrl = null; |
| | | InputStream in = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | //创建临时文件 |
| | | File file = null; |
| | | try { |
| | | file = File.createTempFile("temp", ".jpg"); |
| | | //生成发送数据 |
| | | JSONObject obj = new JSONObject(); |
| | | obj.put("width", width); |
| | | obj.put("path", page); |
| | | obj.put("auto_color", autoColor); |
| | | obj.put("line_color", lineColor); |
| | | obj.put("is_hyaline", isHyaline); |
| | | |
| | | // 创建url资源 |
| | | URL url = new URL(StringUtils.format(MAKE_TWOCODE_ROUND_URL, at)); |
| | | // 建立http连接 |
| | | conn = (HttpURLConnection) url.openConnection(); |
| | | // 设置允许输出 |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | // 设置不用缓存 |
| | | conn.setUseCaches(false); |
| | | // 设置传递方式 |
| | | conn.setRequestMethod("POST"); |
| | | // 设置维持长连接 |
| | | conn.setRequestProperty("Connection", "Keep-Alive"); |
| | | // 设置文件字符集: |
| | | conn.setRequestProperty("Charset", "UTF-8"); |
| | | // 设置文件类型: |
| | | conn.setRequestProperty("contentType", "application/json"); |
| | | // 开始连接请求 |
| | | conn.connect(); |
| | | OutputStream out = conn.getOutputStream(); |
| | | // 写入请求的字符串 |
| | | out.write((obj.toString()).getBytes()); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | // 请求返回的状态 |
| | | if (conn.getResponseCode() == 200) { |
| | | // 请求返回的数据 |
| | | in = conn.getInputStream(); |
| | | //输入到临时文件 |
| | | /*OutputStream os = new FileOutputStream(file); |
| | | int bytesRead = 0; |
| | | byte[] buffer = new byte[8192]; |
| | | while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | }*/ |
| | | FileUtils.copyInputStreamToFile(in, file); |
| | | |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | |
| | | if (in != null) { |
| | | in.close(); |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (in != null) { |
| | | try { |
| | | in.close(); |
| | | }catch (Exception ep) { |
| | | ep.printStackTrace(); |
| | | } |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }finally { |
| | | if(file != null){ |
| | | file.deleteOnExit(); |
| | | } |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | /**生成无限二维码,返回临时文 |
| | | * @param at 微信token |
| | | * @param scene 参数,只能32位,最好不要中文 |
| | | * @param page 跳转链接 |
| | | * @param width 宽度 |
| | | * @param autoColor 默认false |
| | | * @param lineColor 默认null |
| | | * @param isHyaline 默认false |
| | | * @return |
| | | */ |
| | | public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
| | | JSONObject lineColor, boolean isHyaline) throws IOException { |
| | | String imgUrl = null; |
| | | InputStream in = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | //创建临时文件 |
| | | File file = File.createTempFile("temp", ".jpg"); |
| | | |
| | | try { |
| | | //生成发送数据 |
| | | JSONObject obj = new JSONObject(); |
| | | obj.put("scene", scene); |
| | | obj.put("width", width); |
| | | obj.put("page", page); |
| | | obj.put("auto_color", autoColor); |
| | | obj.put("line_color", lineColor); |
| | | obj.put("is_hyaline", isHyaline); |
| | | |
| | | // 创建url资源 |
| | | URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
| | | // 建立http连接 |
| | | conn = (HttpURLConnection) url.openConnection(); |
| | | // 设置允许输出 |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | // 设置不用缓存 |
| | | conn.setUseCaches(false); |
| | | // 设置传递方式 |
| | | conn.setRequestMethod("POST"); |
| | | // 设置维持长连接 |
| | | conn.setRequestProperty("Connection", "Keep-Alive"); |
| | | // 设置文件字符集: |
| | | conn.setRequestProperty("Charset", "UTF-8"); |
| | | // 设置文件类型: |
| | | conn.setRequestProperty("contentType", "application/json"); |
| | | // 开始连接请求 |
| | | conn.connect(); |
| | | OutputStream out = conn.getOutputStream(); |
| | | // 写入请求的字符串 |
| | | out.write((obj.toString()).getBytes()); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | // 请求返回的状态 |
| | | if (conn.getResponseCode() == 200) { |
| | | // 请求返回的数据 |
| | | in = conn.getInputStream(); |
| | | //输入到临时文件 |
| | | /*OutputStream os = new FileOutputStream(file); |
| | | int bytesRead = 0; |
| | | byte[] buffer = new byte[8192]; |
| | | while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | }*/ |
| | | FileUtils.copyInputStreamToFile(in, file); |
| | | |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | |
| | | if (in != null) { |
| | | in.close(); |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (in != null) { |
| | | try { |
| | | in.close(); |
| | | }catch (Exception ep) { |
| | | ep.printStackTrace(); |
| | | } |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }finally { |
| | | file.deleteOnExit(); |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | |
| | | /**生成小程序二维码工具(方形) |
| | | * path 二维码跳转链接 |
| | |
| | | return obj; |
| | | } |
| | | |
| | | |
| | | |
| | | /**生成无限二维码,返回临时文 |
| | | * @param at 微信token |
| | | * @param scene 参数,只能32位,最好不要中文 |
| | | * @param page 跳转链接 |
| | | * @param width 宽度 |
| | | * @param autoColor 默认false |
| | | * @param lineColor 默认null |
| | | * @param isHyaline 默认false |
| | | * @return |
| | | */ |
| | | public static File createUnlimitQrCode(String at, String scene, String page, int width, boolean autoColor, |
| | | JSONObject lineColor, boolean isHyaline, String env_version) throws IOException { |
| | | String imgUrl = null; |
| | | InputStream in = null; |
| | | HttpURLConnection conn = null; |
| | | |
| | | //创建临时文件 |
| | | File file = File.createTempFile("temp", ".jpg"); |
| | | |
| | | try { |
| | | //生成发送数据 |
| | | JSONObject obj = new JSONObject(); |
| | | obj.put("scene", scene); |
| | | obj.put("width", width); |
| | | obj.put("page", page); |
| | | obj.put("auto_color", autoColor); |
| | | obj.put("line_color", lineColor); |
| | | obj.put("is_hyaline", isHyaline); |
| | | obj.put("env_version", env_version); |
| | | // 创建url资源 |
| | | URL url = new URL(StringUtils.format(URL_UNLIMIT_SQUARE, at)); |
| | | // 建立http连接 |
| | | conn = (HttpURLConnection) url.openConnection(); |
| | | // 设置允许输出 |
| | | conn.setDoOutput(true); |
| | | conn.setDoInput(true); |
| | | // 设置不用缓存 |
| | | conn.setUseCaches(false); |
| | | // 设置传递方式 |
| | | conn.setRequestMethod("POST"); |
| | | // 设置维持长连接 |
| | | conn.setRequestProperty("Connection", "Keep-Alive"); |
| | | // 设置文件字符集: |
| | | conn.setRequestProperty("Charset", "UTF-8"); |
| | | // 设置文件类型: |
| | | conn.setRequestProperty("contentType", "application/json"); |
| | | // 开始连接请求 |
| | | conn.connect(); |
| | | OutputStream out = conn.getOutputStream(); |
| | | // 写入请求的字符串 |
| | | out.write((obj.toString()).getBytes()); |
| | | out.flush(); |
| | | out.close(); |
| | | |
| | | // 请求返回的状态 |
| | | if (conn.getResponseCode() == 200) { |
| | | // 请求返回的数据 |
| | | in = conn.getInputStream(); |
| | | //输入到临时文件 |
| | | /*OutputStream os = new FileOutputStream(file); |
| | | int bytesRead = 0; |
| | | byte[] buffer = new byte[8192]; |
| | | while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { |
| | | os.write(buffer, 0, bytesRead); |
| | | }*/ |
| | | FileUtils.copyInputStreamToFile(in, file); |
| | | |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | |
| | | if (in != null) { |
| | | in.close(); |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }catch (Exception e) { |
| | | e.printStackTrace(); |
| | | if (in != null) { |
| | | try { |
| | | in.close(); |
| | | }catch (Exception ep) { |
| | | ep.printStackTrace(); |
| | | } |
| | | in = null; |
| | | } |
| | | |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | conn = null; |
| | | } |
| | | }finally { |
| | | file.deleteOnExit(); |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | } |