• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

小程序码传前端

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

事件起因:

  前端掉后台的生成小程序码返回,同时返回需要的其他业务参数。

事件分析:

  常用的上传下载是通过一次请求request返回的response,通过response接收文件流,返回前台

示例代码如下:

FileInputStream fis = null;
try {
    
    File file = new File(getFileUrl());
    response.setContentType("image/" + file.getName().substring(file.getName().lastIndexOf(".") + 1));
    OutputStream out = response.getOutputStream();
    fis = new FileInputStream(file);
    byte[] b = new byte[fis.available()];
    fis.read(b);
    out.write(b);
    out.flush();
} catch (Exception e) {
    e.printStackTrace();
} finally {
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

或者后台流转文件,返回文件url:

示例代码如下:

public static String getLimitQr(String accessToken, String path, String uploadPath) {
    String ctxPath = uploadPath;
    String fileName = "qrCode.png";
    String bizPath = "files";
    String nowday = new SimpleDateFormat("yyyyMMdd").format(new Date());
    String mkdirName = "wxQrCode";
    String ppath = ctxPath + File.separator + bizPath + File.separator + nowday;
    File file = new File(ctxPath + File.separator + bizPath + File.separator + mkdirName);
    if (!file.exists()) {
        file.mkdirs();// 创建文件根目录
    }
    String savePath = file.getPath() + File.separator + fileName;
    File tempFile = new File(savePath);
    if (tempFile.exists()) {
        tempFile.delete();//删除文件
    }
    String qrCode = bizPath + File.separator + mkdirName + File.separator + fileName;
//        if (ppath.contains("\\")) {
//            ppath = ppath.replace("\\", "/");
//        }
    if (qrCode.contains("\\")) {
        qrCode = qrCode.replace("\\", "/");
    }
//        String codeUrl=ppath+"/qrCode.png";
    System.out.print(qrCode);
    System.out.print(savePath);
    try {
        String wxCodeURL = WxCode_Limit_URL.replace("ACCESS_TOKEN", accessToken);
        URL url = new URL(wxCodeURL);
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("POST");// 提交模式
        // conn.setConnectTimeout(10000);//连接超时 单位毫秒
        // conn.setReadTimeout(2000);//读取超时 单位毫秒
        // 发送POST请求必须设置如下两行
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        // 获取URLConnection对象对应的输出流
        PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
        // 发送请求参数
        JSONObject paramJson = new JSONObject();
        paramJson.put("path", path);
        paramJson.put("width", 530);
        paramJson.put("is_hyaline", true);
        paramJson.put("auto_color", true);
        /**
         * line_color生效
         * paramJson.put("auto_color", false);
         * JSONObject lineColor = new JSONObject();
         * lineColor.put("r", 0);
         * lineColor.put("g", 0);
         * lineColor.put("b", 0);
         * paramJson.put("line_color", lineColor);
         * */

        printWriter.write(paramJson.toString());
        // flush输出流的缓冲
        printWriter.flush();
        //开始获取数据
        BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
        OutputStream os = new FileOutputStream(new File(savePath));
        int len;
        byte[] arr = new byte[1024];
        while ((len = bis.read(arr)) != -1) {
            os.write(arr, 0, len);
            os.flush();
        }
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return savePath;
}
View Code

因为这里小程序返回的就是流,如果以文件形式中转,存在存储问题,以及分布式部署访问的问题。因此这里采用处理小程序返回流,转为二进制数组,再base64编码,和其他业务参数,存在data的json串中。

示例代码如下:

StringBuilder sb = new StringBuilder();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
//转字节数组
ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*4];
int n=0;
while ((n=bis.read(buffer))!=-1){
    imageOut.write(buffer,0,n);
}
//转base64
sb.append(Base64.encode(imageOut.toByteArray()));
return sb.toString();

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序接入Echarts组件库发布时间:2022-07-18
下一篇:
JavaWeb项目,Android和微信小程序的初始页面配置发布时间:2022-07-18
热门推荐
    热门话题
    阅读排行榜

    扫描微信二维码

    查看手机版网站

    随时了解更新最新资讯

    139-2527-9053

    在线客服(服务时间 9:00~18:00)

    在线QQ客服
    地址:深圳市南山区西丽大学城创智工业园
    电邮:jeky_zhao#qq.com
    移动电话:139-2527-9053

    Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap