Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.net.MalformedURLException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.nio.charset.MalformedInputException;
- import java.util.Scanner;
- import javax.swing.JOptionPane;
- public class Test
- {
- public static void main( String[] args ) throws InterruptedException, IOException, URISyntaxException
- {
- saveUrl("https://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java");
- /*Process p = Runtime.getRuntime().exec("netsh wlan show profiles name=WlanEngl key=clear");
- Scanner sc = new Scanner(p.getInputStream());
- while (sc.hasNextLine())
- {
- if(sc.nextLine().contains("Sicherheitsschl"))
- {
- String password = sc.nextLine();
- password = password.substring(password.lastIndexOf(": ") + 2);
- System.out.println("Passwort: " + password);
- }
- }*/
- }
- public static void saveUrl(final String urlString) throws MalformedInputException, IOException, URISyntaxException
- {
- BufferedInputStream in = null;
- FileOutputStream fout = null;
- in = new BufferedInputStream(new URL(urlString).openStream());
- fout = new FileOutputStream(getDomainName(urlString));
- final byte data[] = new byte[1024];
- int count;
- while ((count = in.read(data, 0, 1024)) != -1)
- {
- fout.write(data, 0, count);
- }
- in.close();
- fout.close();
- }
- public static String getDomainName(String url) throws URISyntaxException
- {
- URI uri = new URI(url);
- String domain = uri.getHost();
- return cutUrl(domain.startsWith("www.") ? domain.substring(4) : domain);
- }
- public static String cutUrl(String url)
- {
- int spaceIndex = url.indexOf(".");
- if (spaceIndex != -1)
- {
- url = url.substring(0, spaceIndex);
- }
- return url + ".html";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement