在上传图片的时候会生成一个图片地址链接,把这个链接调用这个工具类进行判断,如果返回200.则图片上传成功,返回其他则图片地址有误,或者上传失败。

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

/**
 * 判断网络图片是否存在
 * 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";
    }
}