java实现扫描小程序码跳转相应界面

我现在的业务需求是,扫二维码实现跳转到小程序的摸个界面,应该是用到的场景会很多,希望此篇博客帮助到大家!

  1. 可以参照官方文档:
    https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html#HTTPS 调用
  2. 首先需要获取token,这个可以参照:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
  3. 设置生成二维码需要的参数:
        Map<String, Object> param = new HashMap<>();
		param.put("scene", “参数”); // 参数
		param.put("page", "页面路径"); // 位置
		param.put("width", 780);
  1. 通过微信获取图片实现方法:
	CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + token); // 接口
		httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
		String body = JSON.toJSONString(param); // 必须是json模式的 post
		StringEntity entity;
		entity = new StringEntity(body);
		entity.setContentType("image/png");
		httpPost.setEntity(entity);
		HttpResponse response;
		response = httpClient.execute(httpPost);
  1. 处理图片
 InputStream inputStream = response.getEntity().getContent();
		String name = reserveOne + ".png";
		String path = "C:/miniPic/qrcoude/";
		String picPath =path + name;
		File dir = new File(path, name);// 在目录中创建文件目录
		if (!dir.getParentFile().exists()) {
			dir.getParentFile().mkdirs();// 创建文件
		}
		saveToImgByInputStream(inputStream, path, name); // 保存图片
		
		MultipartFile file = getMulFileByPath(picPath);
		try {
			byte[] bytes = file.getBytes();
			for(byte b : bytes){
				System.out.print(b);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		//此处为上传腾讯云存储的路径
		String url = OSSFactory.build().upload(file);
        return url;
  1. 所需的一些工具类
public int saveToImgByInputStream(InputStream instreams, String imgPath, String imgName) {
		int stateInt = 1;
		if (instreams != null) {
			try {
				File files=new File(imgPath);    
				if  (!files .exists()  && !files .isDirectory())      
				{        
				    files .mkdir();    
				} 
				File file = new File(imgPath, imgName);// 可以是任何图片格式.jpg,.png等
				FileOutputStream fos = new FileOutputStream(file);
				byte[] b = new byte[1024];
				int nRead = 0;
				while ((nRead = instreams.read(b)) != -1) {
					fos.write(b, 0, nRead);
				}
				fos.flush();
				fos.close();
			} catch (Exception e) {
				stateInt = 0;
				e.printStackTrace();
			} finally {
			}
		}
		return stateInt;
	}
	private static MultipartFile getMulFileByPath(String picPath) {
		FileItem fileItem = createFileItem(picPath);
		MultipartFile mfile = new CommonsMultipartFile(fileItem);
		return mfile;
	}
	private static FileItem createFileItem(String filePath)
    {
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "textField";
        int num = filePath.lastIndexOf(".");
        String extFile = filePath.substring(num);
        FileItem item = factory.createItem(textFieldName, "text/plain", true,
            "MyFileName" + extFile);
        File newfile = new File(filePath);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        try
        {
            FileInputStream fis = new FileInputStream(newfile);
            OutputStream os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192))
                != -1)
            {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            fis.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return item;
    }

希望此篇文章可以帮助到大家哦!!!!

你可能感兴趣的:(SpringBoot)