# try-with-resource结合http协议导致参数漏传

  • 代码复现
class Demo {
    
    public  void someFunction(){
      URL addressUrl = new URL(address);
      URLConnection conn = addressUrl.openConnection();

      try(OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));) 	{
        out.write("参数...");
        out.flush();
        //读取通信结果...略
      }
      
      //...省略...
    }
}

  • 问题说明

    http客户端打开输入流时,会触发发送请求。但是此时发送缓存区没有数据,因此发送了一个空请求!

# SimpleDateFormate调试正常,线上异常

  • 代码复现
class Test{
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    public static void Test1(){
        dateFormat.format(new Date());
    }
    
    public void Test2(){
        dateFormat.format(new Date());
    }
}
  • 问题说明
    • 报错: java.lang.NumberFormatException: multiple pints
    • SimpleDateFormat的format方法实际操作的就是Calendar**(Calendar变量也就是一个共享变量线程不安全)**,也正是因为每次在转化时间的时候foramat会先把时间set到calendar中,这样就会导致A线程读取到B线程的时间.

# spring+tomcat启动的项目,静态资源如js中文乱码,怎么设置编码都没用

  • 问题说明 jsp文件引入了多个 js,js中包含中文, 其中浏览器为utf8编码,静态资源为utf8编码,最后加载的静态资源依然乱码

  • 问题说明

    • defaultServlet默认会处理静态资源,在没有使用全局的springMvc代理之前,所有的静态资源将由defaultServlet处理,而该servlet的默认编码为iso-8859-1编码
    • 简言之,应用层面的编码配置,有其作用范围,对于defaultServlet,需要去设置tomcat本身的配置,本质上而言,这是框架问题,因为其与环境耦合了