java基础--(00)--有用的代码片段

在我们写程序的过程中,往往会经常遇到一些常见的功能。而这些功能或效果往往也是相似的,解决方案也相似。下面是我在写代码的过程中总结的一些有用的代码片段。

1、在多线程环境中操作同一个Collection,会出现线程同步的问题,甚至有时候会抛出异常

解决方案:使用Collections.synchronizeMap(),并使用如下代码访问或者删除元素

public class ConcurrentMap {
    private Map<String, String> map = Collections.synchronizedMap(new HashMap<>());

    public synchronized void add(String key, String value) {
        map.put(key, value);
    }

    public synchronized void remove(String key) {
        Set<Map.Entry<String, String>> entries = map.entrySet();
         Iterator<Map.Entry<String, String>> it = entries.iterator();
         while (it.hasNext()) {
             Map.Entry<String, String> entry = it.next();
             if (key.equals(entry.getKey())) {
                 it.remove();
             }
         }
     }

     public synchronized void remove2(String key) {
         Set<Map.Entry<String, String>> entries = map.entrySet();
         entries.removeIf(entry -> key.equals(entry.getKey()));
     }
 }



View Code

2、根据URL获取ip

解决方案,使用java.net.InetAddress工具类

    /**
     * 根据url获取对应的ip
     * @throws UnknownHostException UnknownHostException
     */
    @Test
    public void testGetIP() throws UnknownHostException {
        Pattern domainPattern = Pattern.compile("(?<=://)[a-zA-Z\\.0-9]+(?=\\/)");    //匹配域名
        String url = "http://ngcdn001.cnr.cn/live/zgzs/index.m3u8";
        Matcher matcher = domainPattern.matcher(url);
        if (matcher.find()) {
            InetAddress inetAddress = Inet4Address.getByName(matcher.group());
            String hostAddress = inetAddress.getHostAddress();
            System.out.println("hostAddress = " + hostAddress);
        }
    }



View Code

 3、正确匹配URL的正则表达式

解决方案:(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]  IP地址、前后有汉字、带参数的,都是OK的。

辅助:RegexBuddy 正则神器

 

4、servlet 返回二进制文件,弹出下载框

HttpServletResponse resp
// 设置字符集
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("Access-Control-Allow-Methods", "*");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        resp.setHeader("Access-Control-Request-Method", "*");
        resp.setHeader("Access-Control-Request-Headers", "*");
        // 解决IE跨域的问题
        resp.setHeader("P3P", "CP=NON DSP COR CURa ADMa DEVa TAIa PSAa PSDa IVAa IVDa CONa HISa TELa OTPa OUR UNRa IND UNI COM NAV INT DEM CNT PRE LOC");
        // 设置弹出对话框
        resp.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        resp.setCharacterEncoding("UTF-8");
        // 设置生成的文件名字
        resp.setHeader("Content-Disposition", "attachment; filename=xxx.xlsx");
IOUtils.write(bytes, resp.getOutputStream());

 

看这里,看这里

文章总目录:博客导航

码字不易,尊重原创,转载请注明:https://blog.csdn.net/u_ascend/article/details/80485954

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值