0

I have this file URL: http://xxx.xxx.xx.xx/resources/upload/2014/09/02/new sample.pdf which will be converted to http://xxx.xxx.xx.xx/resources/upload/2014/09/02/new%20sample.pdf later.

Now I can get the last path by:

public static String getLastPathFromUrl(String url) {
    return url.replaceFirst(".*/([^/?]+).*", "$1");
}

which will give me new sample.pdf

but how do I get the remaining of the URL: http://xxx.xxx.xx.xx/resources/upload/2014/09/02/ ?

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62
  • How about [`indexOf()`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)) and [`substring`](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int))? – PM 77-1 Sep 03 '14 at 03:59

4 Answers4

4

Easier way to get last path from URL would be to use String.split function, like this:-

String url = "http://xxx.xxx.xx.xx/resources/upload/2014/09/02/new sample.pdf";
String[] urlArray = url.split("/");
String lastPath = urlArray[urlArray.length-1];

This converts your url into an Array which can then be used in many ways. There are various ways to get url-lastPath, one way could be to join the above generated Array using this answer. Or use lastIndexOf() and substring like this:-

String restOfUrl = url.substring(0,url.lastIndexOf("/"));

PS:- Although you can learn something by doing this but I think your best solution would be to replace space by %20 in the complete url String, that would be the fastest and make more sense.

Community
  • 1
  • 1
Sourabh86
  • 744
  • 10
  • 18
  • I already have successfully replaced every white space to %20 and also getting the last path of the URL ONLY. What I want to get is the remaining part of the URL, not the last path. – Compaq LE2202x Sep 03 '14 at 06:22
0

I am not sure if I understood it correctly but when you say

I have this file URL: URL/new sample.pdf which will be converted to URL/new%20sample.pdf later.

It looks like you are trying to replace "space" with %20 in URL or said in simple words trying to take care of unwanted characters in URL. If that is what you need use pre-built

URLEncoder.encode(String url,String enc), You can us ÃœTF-8 as encoding.

http://docs.oracle.com/javase/7/docs/api/java/net/URLEncoder.html

If you really need to split it, assuming that you interested in URL after http://, remove http:// and take store remaining URL in string variable called say remainingURL. then use

List myList = new ArrayList(Arrays.asList(remainingURL.split("/")));

You can iterate on myList to get rest of URL fragments.

jbhardwaj
  • 379
  • 3
  • 8
  • I only took successfully the last path of the URL, what I want to have is the remaining portion of the whole URL, not the last path. – Compaq LE2202x Sep 03 '14 at 06:24
0

I've found it:

    File file=new File("http://xxx.xxx.xx.xx/resources/upload/2014/09/02/new sample.pdf");
    System.out.println(file.getPath().replaceAll(file.getName(),""));

Output: http://xxx.xxx.xx.xx/resources/upload/2014/09/02/

Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62
0

Spring solution:

List<String> pathSegments = UriComponentsBuilder.fromUriString(url).build().getPathSegments();
String lastPath = pathSegments.get(pathSegments.size()-1);
Benjamin Caure
  • 2,090
  • 20
  • 27