How can I check if a string is a URL in Java?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If you program in Android, you could use
android.webkit.URLUtil
to test.Hope it would be helpful.
For Android just add this line:
Here you go:
For Spring Framework users. There is:
org.springframework.util.ResourceUtils#isUrl
E.g
You can use
UrlValidator
from commons-validator. It will save you from writing code where the logic flow is guided by caching an exception, which is generally considered a bad practice. In this case, however, I think it's fine to do as others suggested, if you move this functionality to an utility method calledisValidUrl(..)
Complementing Bozho answer, to go more practical:
Test it with this sample code (reference):
Run/Debug