let's say I have a file located in:
http://example.com/123.app
now I get the file name using the following (u is an entire url string):
String fileName = u.substring( u.lastIndexOf('/')+1, u.length() );
but I want to put on the same file name also parameters, so it'll look like, this:
http://example.com/123.app?id=87983
And I want to have a String fileName开发者_运维技巧 which will contain '123.app', and also String id which will contain '87983' and possibly more parameters.
How would I go about achieving this?
Firstly, take a look at this post, which uses the URL class to make working with the different parts of the URL string a lot easier.
Could you share a link to an URL parsing implementation?
Secondly, you would need to take the Query part of the URL and the Path part of the URL and substring the returned values to get the information that you desire. It should be pretty straight forward.
Use the API of URI! That's what it's for. Forget all this substring/regex/spit stuff.
you need to use the split method on string. So for example on your fileName string
String[] mystrings = fileName.split("?");
then mystrings[0] is your filename and mystrings[1] is your parameter
A simple way is : just repeat substring :
int qidx = filename.indexOf("?");
String realFilename = filename.substring(0, qidx);
String parameters = filename.substring(qidx+1);
and so on for parsing parameters.
If you are writing a servlet try :
String fileName = request.getServletPath();
and for the parameters somthing like
String id = request.getParameter("id");
Try this regex:
String s = "http://example.com/123.app?id=87983";
String[] split = s.split(".*/|\\?id=");
String filename = split[1];
String id = (split.length == 3) ? split[2] : null;
精彩评论