首页 > 广告合作 > java判断图片地址链接是否有效
2018
12-12

java判断图片地址链接是否有效

在工作中,难免遇到一些奇怪的需求,就比如前端上传了图片,一张图的时候可以回显出来,但遇到多图上传的时候,给后台传base64流,后台接收到图片流之后,只顾着一个劲儿的上传,而不知道这些传上去的是否有效。因此,写了一个小工具类,分享一下。我们项目使用到的是七牛图片上传


/**
     * 判断网络图片是否存在
     * posturl 图片地址链接
     */
    public static String isImagesTrue(String posturl) throws IOException {
        URL url = new URL(posturl);
        HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
        urlcon.setRequestMethod("POST");
        urlcon.setRequestProperty("Content-type",
                "application/x-www-form-urlencoded");
        if (urlcon.getResponseCode() == HttpURLConnection.HTTP_OK) {
            System.out.println(HttpURLConnection.HTTP_OK + posturl
                    + ":posted ok!");
            return "200";
        } else {
            System.out.println(urlcon.getResponseCode() + posturl
                    + ":Bad post...");
            return "404";
        }
    }


本文》有 0 条评论

留下一个回复