1. 打印 InputStream 的内容

问题背景:
在代码中获取 HTTP 响应内容时,想打印 InputStream 内容,但直接打印流对象只输出其地址信息。

InputStream content = response.getEntity().getContent();
System.out.println(content); // 输出:java.io.BufferedInputStream@xxxx

解决方案:

  1. 通过 BufferedReader 读取内容:

    InputStream content = response.getEntity().getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    StringBuilder result = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        result.append(line);
    }
    System.out.println(result.toString());
  2. 使用 Apache Commons IO 工具类:

    • 依赖:org.apache.commons:commons-io

    • 示例代码:

      String result = IOUtils.toString(content, StandardCharsets.UTF_8);
      System.out.println(result);

2. 微信小程序 button 点击没有弹窗

问题背景:
点击微信小程序的按钮,使用 wx.getUserProfile 方法获取用户信息时,未出现授权弹窗。

原因分析:

解决方案:


3. Nginx 代理与前端请求端口的关系

问题背景:
使用 Nginx 作为前端代理,Nginx 监听 80 端口,后端服务监听 8080 端口,前端请求时应该使用哪个端口?

解答:


4. FastJSON 的 parseparseObject 区别

区别概述:

  1. JSON.parse(String text)

    • 作用:将 JSON 字符串解析为 Object 类型。

    • 适用场景:仅需要 JSON 的基础解析,不关心具体类型。

    • 示例:

      Object obj = JSON.parse("{\"name\":\"Tom\"}");
      System.out.println(obj); // 输出 LinkedHashMap
  2. JSON.parseObject(String text, Class<T> clazz)

    • 作用:将 JSON 字符串解析为指定类型的 Java 对象。

    • 适用场景:需要将 JSON 数据映射为具体的实体类。

    • 示例:

      class User {
          private String name;
          // getter & setter
      }
      User user = JSON.parseObject("{\"name\":\"Tom\"}", User.class);
      System.out.println(user.getName()); // 输出 Tom

5. toString() 在类型转换中的作用

问题背景:
在以下代码中,toString() 是否可以省略?

Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());

分析: