Example usage for java.net URL URL

List of usage examples for java.net URL URL

Introduction

In this page you can find the example usage for java.net URL URL.

Prototype

public URL(String spec) throws MalformedURLException 

Source Link

Document

Creates a URL object from the String representation.

Usage

From source file:com.vaadin.buildhelpers.FetchReleaseNotesTickets.java

public static void main(String[] args) throws IOException {
    String version = System.getProperty("vaadin.version");
    if (version == null || version.equals("")) {
        usage();/* ww w .j  a  v a  2  s . c  o m*/
    }

    URL url = new URL(queryURL.replace("@version@", version));
    URLConnection connection = url.openConnection();
    InputStream urlStream = connection.getInputStream();

    @SuppressWarnings("unchecked")
    List<String> tickets = IOUtils.readLines(urlStream);

    for (String ticket : tickets) {
        String[] fields = ticket.split("\t");
        if ("id".equals(fields[0])) {
            // This is the header
            continue;
        }
        System.out.println(ticketTemplate.replace("@ticket@", fields[0]).replace("@description@", fields[1]));
    }
    urlStream.close();
}

From source file:StAXTest.java

public static void main(String[] args) throws Exception {
    String urlString;/*from w  ww.jav a 2s .c om*/
    if (args.length == 0) {
        urlString = "http://www.w3c.org";
        System.out.println("Using " + urlString);
    } else
        urlString = args[0];
    URL url = new URL(urlString);
    InputStream in = url.openStream();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("a")) {
                String href = parser.getAttributeValue(null, "href");
                if (href != null)
                    System.out.println(href);
            }
        }
    }
}

From source file:JarClassLoader.java

public static void main(String[] args) throws Exception {
    URL url = new URL(args[0]);
    JarClassLoader cl = new JarClassLoader(url);
    String name = null;/*from www .  j ava2s  .  c  om*/
    name = cl.getMainClassName();
    if (name == null) {
        fatal("Specified jar file does not contain a 'Main-Class'" + " manifest attribute");
    }
    String[] newArgs = new String[args.length - 1];
    System.arraycopy(args, 1, newArgs, 0, newArgs.length);
    cl.invokeClass(name, newArgs);
}

From source file:com.manning.blogapps.chapter10.examples.AuthPostJava.java

public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.out.println("USAGE: authpost <username> <password> <filepath> <url>");
        System.exit(-1);/*w w w .ja v a2s.  c  o  m*/
    }
    String credentials = args[0] + ":" + args[1];
    String filepath = args[2];

    URL url = new URL(args[3]);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    conn.setRequestProperty("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    File upload = new File(filepath);
    conn.setRequestProperty("name", upload.getName());

    String contentType = "application/atom+xml; charset=utf8";
    if (filepath.endsWith(".gif"))
        contentType = "image/gif";
    else if (filepath.endsWith(".jpg"))
        contentType = "image/jpg";
    conn.setRequestProperty("Content-type", contentType);

    BufferedInputStream filein = new BufferedInputStream(new FileInputStream(upload));
    BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
    byte buffer[] = new byte[8192];
    for (int count = 0; count != -1;) {
        count = filein.read(buffer, 0, 8192);
        if (count != -1)
            out.write(buffer, 0, count);
    }
    filein.close();
    out.close();

    String s = null;
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((s = in.readLine()) != null) {
        System.out.println(s);
    }
}

From source file:com.xx_dev.speed_test.SpeedTestClient.java

public static void main(String[] args) {
    EventLoopGroup group = new NioEventLoopGroup();
    try {//from www . j av  a2s  .  com

        URL url = new URL(args[0]);

        boolean isSSL = false;
        String host = url.getHost();
        int port = 80;
        if (StringUtils.equals(url.getProtocol(), "https")) {
            port = 443;
            isSSL = true;
        }

        if (url.getPort() > 0) {
            port = url.getPort();
        }

        String path = url.getPath();
        if (StringUtils.isNotBlank(url.getQuery())) {
            path += "?" + url.getQuery();
        }

        PrintWriter resultPrintWriter = null;
        if (StringUtils.isNotBlank(args[1])) {
            String resultFile = args[1];

            resultPrintWriter = new PrintWriter(
                    new OutputStreamWriter(new FileOutputStream(resultFile, false), "UTF-8"));
        }

        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class)
                .handler(new SpeedTestHttpClientChannelInitializer(isSSL, resultPrintWriter));

        // Make the connection attempt.
        Channel ch = b.connect(host, port).sync().channel();

        // Prepare the HTTP request.
        HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, path);
        request.headers().set(HttpHeaders.Names.HOST, host);
        request.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        //request.headers().set(HttpHeaders.Names.ACCEPT_ENCODING, HttpHeaders.Values.GZIP);

        // Send the HTTP request.
        ch.writeAndFlush(request);

        // Wait for the server to close the connection.
        ch.closeFuture().sync();

        if (resultPrintWriter != null) {
            resultPrintWriter.close();
        }

    } catch (InterruptedException e) {
        logger.error(e.getMessage(), e);
    } catch (FileNotFoundException e) {
        logger.error(e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        logger.error(e.getMessage(), e);
    } catch (MalformedURLException e) {
        logger.error(e.getMessage(), e);
    } finally {
        group.shutdownGracefully();
    }
}

From source file:GetURLInfo.java

/** Create a URL, call printinfo() to display information about it. */
public static void main(String[] args) {
    try {//from  w  ww  .j a  va 2s  . c  o m
        printinfo(new URL(args[0]));
    } catch (Exception e) {
        System.err.println(e);
        System.err.println("Usage: java GetURLInfo <url>");
    }
}

From source file:de.kaixo.mubi.lists.MubiListsScraper.java

public static void main(String ars[]) throws XMLStreamException, FactoryConfigurationError, IOException {
    for (int page = 1; page <= 10; page++) {
        System.out.println("Fetching page " + page);
        URL url = new URL(MUBI_LISTS_BASE_URL + "&page=" + page);
        List<MubiListRef> lists = MubiListsReader.getInstance().readMubiLists(url);
        for (MubiListRef list : lists) {
            System.out.println("  Fetching list " + list.getTitle());
            List<MubiFilmRef> filmList = MubiListsReader.getInstance()
                    .readMubiFilmList(new URL(MUBI_BASE_URL + list.getUrl()));
            list.addFilms(filmList);/*from  www.  j a v a  2  s . c om*/
        }

        File outfile = new File("output", "mubi-lists-page-" + String.format("%04d", page) + ".json");
        System.out.println("Writing " + outfile.getName());
        mapper.writeValue(outfile, lists);
    }
}

From source file:com.github.tsouza.promises.example.BlockingIOPromise.java

public static void main(String[] args) throws Exception {

    Promises.defer((Resolver<String> resolver) -> {
        URL url = new URL("http://api.ipify.org");

        try (InputStream stream = url.openStream()) {
            resolver.resolve(IOUtils.toString(stream));
        }//from   www  .  j  av a 2 s.c o m

    }, ThreadProfile.IO).done(System.out::println);

    Thread.sleep(5000);
}

From source file:ImageBouncer.java

public static void main(String[] args) {
    String filename = "java2sLogo.png";
    if (args.length > 0)
        filename = args[0];//from   www  . ja va 2s  . c o  m

    Image image = null;
    try {
        image = blockingLoad(new URL(filename));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    final ImageBouncer bouncer = new ImageBouncer(image);
    Frame f = new AnimationFrame(bouncer);
    f.setFont(new Font("Serif", Font.PLAIN, 12));
    Panel controls = new Panel();
    controls.add(bouncer.createCheckbox("Bilinear", ImageBouncer.BILINEAR));
    controls.add(bouncer.createCheckbox("Transform", ImageBouncer.TRANSFORM));
    final Choice typeChoice = new Choice();
    typeChoice.add("TYPE_INT_RGB");
    typeChoice.add("TYPE_INT_ARGB");
    typeChoice.add("TYPE_INT_ARGB_PRE");
    typeChoice.add("TYPE_3BYTE_BGR");
    typeChoice.add("TYPE_BYTE_GRAY");
    typeChoice.add("TYPE_USHORT_GRAY");
    typeChoice.add("TYPE_USHORT_555_RGB");
    typeChoice.add("TYPE_USHORT_565_RGB");
    controls.add(typeChoice);
    f.add(controls, BorderLayout.NORTH);

    typeChoice.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ie) {
            String type = typeChoice.getSelectedItem();
            bouncer.setImageType(type);
        }
    });
    f.setSize(200, 200);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL urlFG = new URL("http://www.java2s.com/style/download.png");
    URL urlBG = new URL("http://www.java2s.com/style/download.png");
    final BufferedImage biFG = ImageIO.read(urlFG);
    final BufferedImage biBG = ImageIO.read(urlBG);
    SwingUtilities.invokeLater(new Runnable() {
        @Override/*from   w  ww .  j ava2s.  c  o m*/
        public void run() {
            draw(biBG, biFG);
        }
    });
}