nr.iras.sk
nr.iras.sk

Reputation: 8498

Get name of an image in java

Is it possible to get the name of an image in java. The image source is a url.

For eg. "http://172.16.2.42/apache_pb.png"

I need the output "apache_pb.png"

Upvotes: 0

Views: 4353

Answers (3)

Denis Tulskiy
Denis Tulskiy

Reputation: 19167

You can use this helper method from the URL class:

String file = new URL("http://172.16.2.42/apache_pb.png").getPath();

Upvotes: 2

clstrfsck
clstrfsck

Reputation: 14829

Would URL#getFile() do this for you?

Upvotes: 1

Ananth
Ananth

Reputation: 4397

String s = "http://172.16.2.42/apache_pb.png";
int index = s.lastIndexOf('/');

String name = s.substring(index+1);
System.out.println(name);

Upvotes: 2

Related Questions