rutera

Download file

Oct 17th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. //rutera.org
  2. //
  3.  
  4. import java.io.*;
  5. import java.net.*;
  6.  
  7. public class Main {
  8.  public static void main(String[] args) {
  9.  URL url; //represents the location of the file we want to dl.
  10.  URLConnection con; // represents a connection to the url we want to dl.
  11.  DataInputStream dis; // input stream that will read data from the file.
  12.  FileOutputStream fos; //used to write data from inut stream to file.
  13.  byte[] fileData; //byte aray used to hold data from downloaded file.
  14.  try {
  15.  url = new URL("http://www.slava.bg/images/content/2011/10/10/33139/0901.jpg");
  16.  con = url.openConnection(); // open the url connection.
  17.  dis = new DataInputStream(con.getInputStream()); // get a data stream from the url connection.
  18.  fileData = new byte[con.getContentLength()]; // determine how many byes the file size is and make array big enough to hold the data
  19.  for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
  20.  fileData[x] = dis.readByte();
  21.  }
  22.  dis.close(); // close the data input stream
  23.  fos = new FileOutputStream(new File("file.jpg")); //create an object representing the file we want to save
  24.  fos.write(fileData); // write out the file we want to save.
  25.  fos.close(); // close the output stream writer
  26.  }
  27.  catch(MalformedURLException m) {
  28.  System.out.println(m);
  29.  }
  30.  catch(IOException io) {
  31.  System.out.println(io);
  32.  }
  33.  }
  34. }
Add Comment
Please, Sign In to add comment