Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package network;
- import java.io.*;
- import java.net.*;
- /**
- * @author B_S
- *
- */
- public class NetworkIO {
- /**
- * Downloads {@code source} file to {@code destination} location.
- *
- * @param source URL to the source file.
- * @param destination path to the destination file.
- */
- public static void getFile(String source, String destination) throws Exception {
- final String tiUser = "hereComesYourUsername";
- final char[] tiPassword = "hereComesYourPassword".toCharArray();
- Authenticator.setDefault(new Authenticator(){
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(tiUser, tiPassword);
- }
- });
- DataInputStream input = null;
- FileOutputStream fout = null;
- DataOutputStream output = null;
- try {
- URL newURL = new URL(source);
- URLConnection fileStream = newURL.openConnection();
- input = new DataInputStream(fileStream.getInputStream());
- fout = new FileOutputStream(destination);
- output = new DataOutputStream(fout);
- int data;
- while ((data = input.read()) != -1) {
- fout.write(data);
- }
- System.out.println("Download is complete." );
- } catch (Exception e){
- e.printStackTrace();
- } finally {
- try{
- input.close();
- fout.flush();
- output.close();
- } catch(Exception e){e.printStackTrace();}
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement