An SO User
An SO User

Reputation: 24998

Extract a path to file from a URL which has parameters in it

So, I am trying to extract a path to a mp4 file from a URL. The problem is that they have parameters appended to it after a ?. Eg: www.example.com/video.mp4?w=1280&h=720
Now, what I do is extract substring as follows:

String fileNameWithParam = f.getVideoURL().substring(f.getVideoURL().lastIndexOf("/")+1);
String fileNameWithoutParam = fileNameWithParam.substring(0, fileNameWithParam.indexOf("?"));
fileName = fileNameWithoutParam;  

Which works when the video URL has parameters appended and doesn't work when it does not have parameters appended to it.

Can someone please suggest a more general way to extract path to files from URL ? (without a specific file extension)

Upvotes: 0

Views: 71

Answers (1)

user1596371
user1596371

Reputation:

Try:

final String path = new URL(f.getVideoURL()).getPath();

Upvotes: 1

Related Questions