Get file name from URL

前端 未结 27 1685
[愿得一人]
[愿得一人] 2020-11-28 02:24

In Java, given a java.net.URL or a String in the form of http://www.example.com/some/path/to/a/file.xml , what is the easiest way to g

27条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 03:07

    import java.io.*;

    import java.net.*;
    
    public class ConvertURLToFileName{
    
    
       public static void main(String[] args)throws IOException{
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Please enter the URL : ");
    
       String str = in.readLine();
    
    
       try{
    
         URL url = new URL(str);
    
         System.out.println("File : "+ url.getFile());
         System.out.println("Converting process Successfully");
    
       }  
       catch (MalformedURLException me){
    
          System.out.println("Converting process error");
    
     }
    

    I hope this will help you.

提交回复
热议问题