小程序码传前端
事件起因:
前端掉后台的生成小程序码返回,同时返回需要的其他业务参数。
事件分析:
常用的上传下载是通过一次请求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; }
因为这里小程序返回的就是流,如果以文件形式中转,存在存储问题,以及分布式部署访问的问题。因此这里采用处理小程序返回流,转为二进制数组,再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();