Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Java url downloader

  1. #1
    tnd_blazer is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Blog Entries
    1
    Rep Power
    0

    Exclamation Java url downloader

    Im working on a program that i can get to do WAY more than just url downloading. Im trying to get to download a url, Got that to work. But now i want the user to be able to type
    in the console "
    This is a multiple class program. There is 3 files for now.

    A.java the main class
    Java Code:
     package main;
     public class A {
     public static void main(String args[]) throws Exception{
     aa aamain = new aa();
     ab abmain = new ab();
     abmain.ask1();
    
    
     }
    
     }
    aa.java the downloader
    Java Code:
     package main;
     import java.util.Scanner; 
     import java.net.*;
     import java.io.*;
     import java.io.BufferedWriter;
     import java.io.File;
     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.io.OutputStreamWriter;
     import java.io.Writer;
     public class aa {
     private static Scanner user_input2;
     public static void callUrl() throws Exception {
     aa aamain = new aa();
     System.out.println("Called URL CALLER please enter the url IE: http://google.com or:http://facebook.com/index.html");
     user_input2 = new Scanner( System.in );
     String urlname;
     urlname = user_input2.next( );
     URL yahoo = new URL("http://" + urlname);
     URLConnection yc = yahoo.openConnection();
     BufferedReader in = new BufferedReader(
     new InputStreamReader(
     yc.getInputStream()));
     String inputLine;
     while ((inputLine = in.readLine()) != null)
     try {
     //What ever the file path is.
     File statText = new File(urlname + ".file");
     FileOutputStream is = new FileOutputStream(statText);
     OutputStreamWriter osw = new OutputStreamWriter(is); 
     Writer w = new BufferedWriter(osw);
     w.write(inputLine);
     w.close();
     } catch (IOException e) {
     System.err.println("File FAILED!");
     }
     System.out.println(inputLine);
     in.close();
     }
     }
    
     //the class "aa.java" is what i updated please help me fix it only writing on one line. i want it to write the whole file on it.
    ab.java the console class
    Java Code:
     package main;
     import java.util.Scanner;
     public class ab {
     private static Scanner user_input;
    
     public static void ask1() throws Exception{
     aa aamain = new aa();
     System.out.println("Main console.");
     user_input = new Scanner( System.in );
     String first_name;
     first_name = user_input.next( );
     System.out.println(first_name);
    
    
     if(first_name.equals("callurl")){
    
     aamain.callUrl();
    
     }
     }
     }
    What i want to be able to do is have the user type "callurl" then without hitting enter, "http://google.com" or other websites. but i can only get it to download what i put in the one line of aa.java
    on line:
    Java Code:
     URL yahoo = new URL("http://www.yahoo.com/");
    NVM i got the way to get the users input on the url thay want. Now i need a way to write what it gets to a file.... in the same folder that the program is in.
    instead of putting it in the console.
    Any ideas?
    Last edited by JosAH; 06-17-2013 at 08:26 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Java url downloader

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tnd_blazer is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Blog Entries
    1
    Rep Power
    0

    Default Re: Java url downloader

    Thanks that helps what i was trying to do befor, i got that now. And now i want to know how i can write that to a file instead of printing it on the console.
    Any ideas?
    i tryed:
    Java Code:
     package main;
     import java.util.Scanner; 
     import java.net.*;
     import java.io.*;
     import java.io.BufferedWriter;
     import java.io.File;
     import java.io.FileOutputStream;
     import java.io.IOException;
     import java.io.OutputStreamWriter;
     import java.io.Writer;
     public class aa {
     private static Scanner user_input2;
     public static void callUrl() throws Exception {
     aa aamain = new aa();
     System.out.println("Called URL CALLER please enter the url IE: http://google.com or:http://facebook.com/index.html");
     user_input2 = new Scanner( System.in );
     String urlname;
     urlname = user_input2.next( );
     URL yahoo = new URL(urlname);
     URLConnection yc = yahoo.openConnection();
     BufferedReader in = new BufferedReader(
     new InputStreamReader(
     yc.getInputStream()));
     String inputLine;
     while ((inputLine = in.readLine()) != null)
     try {
     //What ever the file path is.
     File statText = new File(urlname);
     FileOutputStream is = new FileOutputStream(statText);
     OutputStreamWriter osw = new OutputStreamWriter(is); 
     Writer w = new BufferedWriter(osw);
     w.write(inputLine);
     w.close();
     } catch (IOException e) {
     System.err.println("File FAILED!");
     }
     System.out.println(inputLine);
     in.close();
     }
     }
    That is the aa.java class. I tryed to write that to a file but i get this:
    Welcome to the ask program, Today i wll ask you everything. Lets begin
    callurl
    callurl
    Called URL CALLER please enter the url IE: http://google.com or:http://facebook.com/index.html
    http://google.com
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    File FAILED!
    null
    It fails to write the file due to the "http://" the // are not alowed. so i did this
    Java Code:
     URL yahoo = new URL("http://" + urlname); .
    Putting "http://" there befor the string. So that i can use the "urlname" to write the file name. now i get no errors. but it says "null"
    why?
    UPDATE:
    no i have it writeing to a file.... but it only writes one line and nothing els.... why?
    ill re-upload the code on the main part
    Last edited by JosAH; 06-17-2013 at 08:27 PM.

  4. #4
    tnd_blazer is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Blog Entries
    1
    Rep Power
    0

    Default Re: Java url downloader

    anyone help please?
    im trying to get it to save to a file. i got that now.
    I just need to to save the hole thing. And its only saveing oe line of the file? why is this...
    Last edited by tnd_blazer; 06-17-2013 at 07:24 PM.

  5. #5
    tnd_blazer is offline Member
    Join Date
    Jun 2013
    Posts
    12
    Blog Entries
    1
    Rep Power
    0

    Default Re: Java url downloader

    Thanks for your help, this thread is now closed

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java url downloader

    @OP: please don't remove all contents from your thread; other might stumble on this thread (through google mayhap?) only to find nothing; I reinstalled the previous content and closed this thread.

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Downloader.MultiThreading/in part downloading ....Help Needed
    By haafiz786 in forum Threads and Synchronization
    Replies: 3
    Last Post: 03-15-2012, 09:26 AM
  2. JFileDownload 2.5 - Java file downloader
    By jfileupload in forum Java Software
    Replies: 1
    Last Post: 11-12-2009, 07:55 AM
  3. Help needed with downloader in java
    By falcommoney in forum New To Java
    Replies: 3
    Last Post: 01-28-2009, 01:31 PM
  4. Creating a Downloader using JAVA
    By shinojkk in forum New To Java
    Replies: 0
    Last Post: 01-08-2008, 05:08 PM
  5. Torrent Episode Downloader 0.90
    By levent in forum Java Software
    Replies: 0
    Last Post: 08-02-2007, 05:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •