List of usage examples for java.net URL URL
public URL(String spec) throws MalformedURLException
From source file:MainClass.java
public static void main(String args[]) throws Exception { URL u = new URL("http://www.java2s.com/binary.dat"); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int contentLength = uc.getContentLength(); if (contentType.startsWith("text/") || contentLength == -1) { throw new IOException("This is not a binary file."); }//from w ww . j a v a2 s.co m InputStream raw = uc.getInputStream(); InputStream in = new BufferedInputStream(raw); byte[] data = new byte[contentLength]; int bytesRead = 0; int offset = 0; while (offset < contentLength) { bytesRead = in.read(data, offset, data.length - offset); if (bytesRead == -1) break; offset += bytesRead; } in.close(); if (offset != contentLength) { throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes"); } String filename = u.getFile().substring(filename.lastIndexOf('/') + 1); FileOutputStream out = new FileOutputStream(filename); out.write(data); out.flush(); out.close(); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { String query = "name=yourname&email=youremail@yourserver.com"; URLConnection uc = new URL("http:// your form ").openConnection(); uc.setDoOutput(true);/*from ww w . jav a2 s .c o m*/ uc.setDoInput(true); uc.setAllowUserInteraction(false); DataOutputStream dos = new DataOutputStream(uc.getOutputStream()); // The POST line, the Accept line, and // the content-type headers are sent by the URLConnection. // We just need to send the data dos.writeBytes(query); dos.close(); // Read the response DataInputStream dis = new DataInputStream(uc.getInputStream()); String nextline; while ((nextline = dis.readLine()) != null) { System.out.println(nextline); } dis.close(); }
From source file:MainClass.java
public static void main(String[] args) { try {/*from w w w . ja v a 2 s . co m*/ MyHttpHandler handler = new MyHttpHandler(); URLConnection uc = handler.openConnection(new URL("http://www.ora.com")); if (!uc.getAllowUserInteraction()) { uc.setAllowUserInteraction(true); } } catch (MalformedURLException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } }
From source file:Main.java
public static void main(String avg[]) throws Exception { BufferedImage img = ImageIO.read(new URL("http://www.java2s.com/style/download.png")); ImageIcon icon = new ImageIcon(img); JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setSize(200, 300);//from w ww. j av a 2 s . co m JLabel lbl = new JLabel(); lbl.setIcon(icon); frame.add(lbl); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String encoding = "ISO-8859-1"; URL u = new URL("http://www.java2s.com"); URLConnection uc = u.openConnection(); String contentType = uc.getContentType(); int encodingStart = contentType.indexOf("charset="); if (encodingStart != -1) { encoding = contentType.substring(encodingStart + 8); }// w w w . j a va2 s. c o m InputStream in = new BufferedInputStream(uc.getInputStream()); Reader r = new InputStreamReader(in, encoding); int c; while ((c = r.read()) != -1) { System.out.print((char) c); } }
From source file:GoogleSearch.java
public static void main(String args[]) throws Exception { URL url = new URL("http://api.google.com/GoogleSearch.wsdl"); QName serviceName = new QName("urn:GoogleSearch", "GoogleSearchService"); QName portName = new QName("urn:GoogleSearch", "GoogleSearchPort"); Service service = Service.create(url, serviceName); Dispatch<SOAPMessage> dispatch = service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE); SOAPMessage request = MessageFactory.newInstance().createMessage(null, new FileInputStream("yourGoogleKey.xml")); SOAPMessage response = dispatch.invoke(request); response.writeTo(System.out); }
From source file:MediaPlayer.java
public static void main(String[] args) throws Exception { final JFrame frame = new JFrame("MediaPlayer"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); URL url = new URL(args[0]); final Player player = Manager.createPlayer(url); player.addControllerListener(new ControllerListener() { public void controllerUpdate(ControllerEvent ce) { if (ce instanceof RealizeCompleteEvent) { Component visual = player.getVisualComponent(); Component control = player.getControlPanelComponent(); if (visual != null) frame.getContentPane().add(visual, "Center"); frame.getContentPane().add(control, "South"); frame.pack();//ww w. j a va 2s .c o m frame.setVisible(true); player.start(); } } }); player.realize(); }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.java2s.com/style/download.png"); BufferedImage origImg = ImageIO.read(url); JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(origImg))); File newFile = new File("new.png"); ImageIO.write(origImg, "png", newFile); BufferedImage newImg = ImageIO.read(newFile); JOptionPane.showMessageDialog(null, new JLabel("New", new ImageIcon(newImg), SwingConstants.LEFT)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { AudioFileFormat fformat = AudioSystem.getAudioFileFormat(new File("audiofile")); fformat = AudioSystem.getAudioFileFormat(new URL("http://hostname/audiofile")); if (fformat.getType() == AudioFileFormat.Type.AIFC) { } else if (fformat.getType() == AudioFileFormat.Type.AIFF) { } else if (fformat.getType() == AudioFileFormat.Type.AU) { } else if (fformat.getType() == AudioFileFormat.Type.WAVE) { }/*w w w .ja v a 2 s .c om*/ }
From source file:Main.java
public static void main(String[] args) throws Exception { URL url = new URL("http://www.java2s.com/style/download.png"); final BufferedImage bi = ImageIO.read(url); final String size = bi.getWidth() + "x" + bi.getHeight(); SwingUtilities.invokeLater(new Runnable() { public void run() { JLabel l = new JLabel(size, new ImageIcon(bi), SwingConstants.RIGHT); JOptionPane.showMessageDialog(null, l); }/*from w w w . ja v a 2 s. co m*/ }); }