I want to get the file name on the link like this:
http://localhost:7070/file.tar.gz I tried using the URI class, but I can not find the method necessary for this ...
I want to get the file name on the link like this:
http://localhost:7070/file.tar.gz I tried using the URI class, but I can not find the method necessary for this ...
It can be like this
Paths.get("http://localhost:7070/file.tar.gz").getFileName() https://gist.github.com/anonymous/d97ab4b306d67c6fa7b5c2d3e7589108
import java.nio.file.Path; import java.nio.file.Paths; public class Program { public static void main(String[] args) { Path path = Paths.get("http://localhost:7070/file.tar.gz"); System.out.println(path); System.out.println(path.getFileName()); } } The URL class has a getFile() method.
But if you look deeper inside the URL class, you can see that the internal file object in the URL set like this:
this.file = query == null ? path : path + "?" + query; That is, in fact, this is all that follows in the URL after the slash following the port. If you have the following URL: http://localhost:7070/foo/bar/file.tar.gz , getFile() will return foo/bar/file.tar.gz If you need file.tar.gz , then it remains to search for the last slash ( lastIndexOf ) and take a substring, or tie commons-io , as recommended by the link in the comment under your question.
If special classes do not fit, then you can regular:
[^\/]*$ Just need to add screening as needed.
Source: https://ru.stackoverflow.com/questions/605761/
All Articles