Advertisement
Jak0b

Untitled

Jan 17th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.00 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import java.net.MalformedURLException;
  6. import java.net.URI;
  7. import java.net.URISyntaxException;
  8. import java.net.URL;
  9. import java.nio.charset.MalformedInputException;
  10. import java.util.Scanner;
  11.  
  12. import javax.swing.JOptionPane;
  13.  
  14. public class Test
  15. {
  16.     public static void main( String[] args ) throws InterruptedException, IOException, URISyntaxException
  17.     {
  18.         saveUrl("https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java");
  19.        
  20.         /*Process p = Runtime.getRuntime().exec("netsh wlan show profiles name=WlanEngl key=clear");
  21.        
  22.         Scanner sc = new Scanner(p.getInputStream());
  23.        
  24.         while (sc.hasNextLine())
  25.         {
  26.             if(sc.nextLine().contains("Sicherheitsschl"))
  27.             {
  28.                 String password = sc.nextLine();
  29.                
  30.                 password = password.substring(password.lastIndexOf(": ") + 2);
  31.                
  32.                 System.out.println("Passwort: " + password);
  33.             }
  34.         }*/
  35.     }
  36.    
  37.     public static void saveUrl(final String urlString) throws MalformedInputException, IOException, URISyntaxException
  38.     {
  39.         BufferedInputStream in = null;
  40.         FileOutputStream fout = null;
  41.  
  42.         in = new BufferedInputStream(new URL(urlString).openStream());
  43.         fout = new FileOutputStream(getDomainName(urlString));
  44.  
  45.         final byte data[] = new byte[1024];
  46.         int count;
  47.         while ((count = in.read(data, 0, 1024)) != -1)
  48.         {
  49.             fout.write(data, 0, count);
  50.         }
  51.        
  52.        in.close();
  53.        fout.close();
  54.     }
  55.    
  56.     public static String getDomainName(String url) throws URISyntaxException
  57.     {
  58.         URI uri = new URI(url);
  59.         String domain = uri.getHost();
  60.         return cutUrl(domain.startsWith("www.") ? domain.substring(4) : domain);
  61.     }
  62.    
  63.     public static String cutUrl(String url)
  64.     {
  65.         int spaceIndex = url.indexOf(".");
  66.        
  67.         if (spaceIndex != -1)
  68.         {
  69.             url = url.substring(0, spaceIndex);
  70.         }
  71.        
  72.         return url + ".html";
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement