应用聚合数据提供的收费二维码生成器;
申请地址:https://www.juhe.cn/docs/api/id/296

在pom,xml中引入模板jar包

<dependency>       <groupId>org.springframework.boot</groupId>       <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

在service层建设一个HttpClient,用于对外网收回http申请。

import org.springframework.http.HttpMethod;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Service;import org.springframework.util.MultiValueMap;import org.springframework.web.client.RestTemplate;/** 1. @author DXG 2. @date 2018/11/7 0007. */@Servicepublic class HttpClient {    public String client(String url, HttpMethod method, MultiValueMap<String, String> parms) {        RestTemplate template = new RestTemplate();        ResponseEntity<String> response = template.getForEntity(url, String.class);        return response.getBody();    }}

建设base64与image的工具转换类

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class Base64Test {    /**     * 图片转化成base64字符串     *     * @param imgFile 图片门路     * @return     */    public static String GetImageStr(String imgFile) {//将图片文件转化为字节数组字符串,并对其进行Base64编码解决        InputStream in = null;        byte[] data = null;        //读取图片字节数组          try {            in = new FileInputStream(imgFile);            data = new byte[in.available()];            in.read(data);            in.close();        } catch (IOException e) {            e.printStackTrace();        }        //对字节数组Base64编码          BASE64Encoder encoder = new BASE64Encoder();        //返回Base64编码过的字节数组字符串        return encoder.encode(data);    }    /**     * base64字符串转化成图片     *     * @param imgStr     * @return     */    public static boolean GenerateImage(String imgStr, String imgFilePath) {   //对字节数组字符串进行Base64解码并生成图片        //图像数据为空        if (imgStr == null) {            return false;        }        BASE64Decoder decoder = new BASE64Decoder();        try {            //Base64解码              byte[] b = decoder.decodeBuffer(imgStr);            for (int i = 0; i < b.length; ++i) {                //调整异样数据                if (b[i] < 0) {                    b[i] += 256;                }            }            //生成jpeg图片            //新生成的图片            OutputStream out = new FileOutputStream(imgFilePath);            out.write(b);            out.flush();            out.close();            return true;        } catch (Exception e) {            return false;        }    }}

在Controller层建设测试连贯。

import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.ixiaxiang.rq_code.Service.HttpClient;import com.ixiaxiang.rq_code.Util.Base64Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpMethod;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;/** 1. @author DXG 2. @date 2018/11/7 0007. */@RequestMapping("/code")@RestControllerpublic class CodeController {    @Autowired    private HttpClient httpClient;    @RequestMapping("/gettest")    public String getJson(@RequestParam("id") String id) {        String AppKey = "这里填对应的KEY";        String Text= "这里填须要存入的二维码的数据";        String url = "http://apis.juhe.cn/qrcode/api?key=" + AppKey + "&text="+Text;        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();        //调用工具类 返回API返回的Json         String str = httpClient.client(url, HttpMethod.GET, params);        //应用的是alibaba的JSON工具类        JSONObject object = JSON.parseObject(str);        String base64 = object.getJSONObject("result").get("base64_image").toString();        //门路本人批改,这里图不便间接写死了        Base64Test.GenerateImage(base64, "D:\\a.jpg");        return "";    }}

————————————————
版权申明:本文为CSDN博主「0老船长0」的原创文章
原文链接:https://blog.csdn.net/weixin_...