生成小程序码,通过 HTTPS 调用方式,生成的码图片是 0KB 急急急,怎么处理?
发布于 6 年前 作者 xiachao 6094 次浏览 来自 官方Issues
下面是贴出来的请求代码。 麻烦大佬给看看嗯?   我有 15 台集群,每台都这么调用的。 是不是我需要在is = conn.getInputStream(); 这个地方判断下图片是否生成???

String basePath = "";
HttpsURLConnection conn = null;
OutputStream outputStream = null;
InputStream is = null;
OutputStream os = null;
try {
    // 创建SSLContext对象,并使用我们指定的信任管理器初始化
    TrustManager[] tm = {newMyX509TrustManager()};
    SSLContext sslContext = SSLContextgetInstance("SSL", "SunJSSE";
    sslContext.init(null, tm, new java.security.SecureRandom());
    // 从上述SSLContext对象中得到SSLSocketFactory对象
    SSLSocketFactory ssf = sslContext.getSocketFactory();
    URL url = newURL(requestUrl;
    conn = (HttpsURLConnection) url.openConnection();
    conn.setSSLSocketFactory(ssf;
    conn.setDoOutput(true;
    conn.setDoInput(true;
    conn.setUseCaches(false;
    conn.setReadTimeout(15000);
    conn.setConnectTimeout(15000);
    // 设置请求方式(GET/POST)
    conn.setRequestMethod(requestMethod;
    // 当outputStr不为null时向输出流写数据
    outputStream = conn.getOutputStream();
    // 注意编码格式
    outputStream.write(outputStr.getBytes("UTF-8");
    outputStream.close();
    // 从输入流读取返回内容
    String path2 = request.getSession().getServletContext().getRealPath("/";
    String path = path2.replace("\\", "/") + "SmallProgram";
    File file = newFile(path;
    if (!file.exists()) {
        file.mkdirs();
    }
    Date now = newDate();
    //修改日期格式
    SimpleDateFormat dateFormat = newSimpleDateFormat("yyyyMMddHHmmssSSS";
    String name = dateFormat.format(now);
    name = "/" + name + CommonUtilnumber + ".png";
    String path1 = path + name;
    basePath = request.getScheme() + "://" + request.getServerName() + request.getContextPath() + "/SmallProgram" + name;
    CommonUtilsetNumber(number++;
    is = conn.getInputStream();
    os = newFileOutputStream(path1;
    //缓冲数组
    byte[] bs = new byte1024;
    String resultInfo = null;
    int len;
    while ((len = isread(bs)) != -) {
        os.write(bs, , len);
        os.flush();
        resultInfo = newString(bs, "UTF-8";

    }
    booleanbool = false;
    if (resultInfo == null || resultInfo.contains("errcode")) {
        bool = true;
    }
    if (bool) {
        logger.error("获取小程序码异常,异常信息:" + resultInfo);
        returnnull;
    }
    os.flush();
    os.close();
    isclose();

    conn.disconnect();
} catch (ConnectException ce) {
    Systemout.println("连接超时:{}");
    basePath = "";
} catch (Exception e) {
    Systemout.println("https请求异常:{}");
    basePath = "";
} finally {
    try {
        if (outputStream != null) {
            outputStream.close();
        }
        if (is != null) {
            isclose();
        }
        if (os != null) {
            os.close();
        }
        if (conn != null) {
            conn.disconnect();
        }
    } catch (IOException e) {
        Systemout.println("https请求异常:{}");
    }
}
return basePath;
2 回复

不懂java,不好意思

求大佬帮忙看看。

is = conn.getInputStream()sh

是不是要判断下 is 是否读取到呢? 怎么判断是否读取到呢???


回到顶部