Get image Name from Image URL - Java java.net

Java examples for java.net:URL Encode

Description

Get image Name from Image URL

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String imageUrl = "http://java2s.com/a.png";
        System.out.println(imageUrl_2_ImageName(imageUrl));
    }//from  w w w. j  a  va  2 s .c  om

    public static String imageUrl_2_ImageName(String imageUrl) {
        if (imageUrl == null || imageUrl.isEmpty()
                || !imageUrl.contains("http://")) {
            return null;
        }

        int start = imageUrl.lastIndexOf('/');
        if (start == -1) {
            return null;
        }

        int end = imageUrl.lastIndexOf('.');
        if (end == -1) {
            return null;
        }

        if (end <= start + 1) {
            return null;
        }

        return imageUrl.substring(start + 1, end);
    }
}

Related Tutorials