ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

小程序码传前端

2020-07-07 13:03:53  阅读:189  来源: 互联网

标签:码传 file String paramJson 前端 程序 File put new


事件起因:

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

事件分析:

  常用的上传下载是通过一次请求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();

 

标签:码传,file,String,paramJson,前端,程序,File,put,new
来源: https://www.cnblogs.com/x-jingxin/p/13260150.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有