[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Know the error code in downloading a file

Hi!
I use the following code to download a url from the web:
-------------------------------------

import java.net.*;
import java.io.*;

class GeneralDownloader {
   
    static final int MAX_SIZE=50000;
    static final int BUF_SIZE=512;

    public GeneralDownloader(){
    }
   
    public void run(String pSourceURL, String pDestinationFile){

        InputStream in;
        try {
            URL idURL=new URL(pSourceURL);
            try {

                System.out.println( "file="+idURL.getFile().substring(1));
                if (pDestinationFile==null) {
                    pDestinationFile=idURL.getFile().substring(1);
                }
                //Connect with remoteHost
                System.out.println( "Trying to connect with remote host.." );
                DataInputStream remoteIn = new DataInputStream(idURL.openStream());
                //Reading data from remote host
                System.out.println( "Reading data from remote host.." );

                byte urlData[]=new byte[MAX_SIZE];
                //read whole resource
                int count_all=0;
                int count_1=BUF_SIZE;
                byte buf[]=new byte[BUF_SIZE];

                while(count_1>0) {
                    count_1=remoteIn.read(buf);
                    if (count_1>0) {
                        System.arraycopy(buf, 0, urlData, count_all, count_1);
                        count_all+=count_1;
                    }
                }
                remoteIn.close();

                //Write  data to file
                FileOutputStream out=new FileOutputStream(pDestinationFile);
                System.out.println( "Write  data to file " +pDestinationFile+". Data len="+count_all+" bytes.");
                out.write(urlData, 0, count_all);
                out.flush();
                out.close();

            }catch (IOException io){io.printStackTrace();}

        } catch (MalformedURLException m) {
            m.printStackTrace();
        }
    }


    public static void main(String a[]) {
        GeneralDownloader mGeneralDownloader = new GeneralDownloader();
        mGeneralDownloader.run("http://www.marzevit.co.il/index.html", "index.html");
    }

}



-------------------------------------
If there is an error (like the file doesn't exist or i have no authorization for this) i want to get a notification that will contain the error number and message like in Netscape.
Can you give me a code example of this?
Thank you.
0
rzvika3
Asked:
rzvika3
1 Solution
 
sen_kumCommented:
You need to take care any thing, you will get the error number and message automatically, to check that try to get the file in.html from the url you specified. You will get a html with title "404 Not found" and
error message "The requested URL /in.html was not found on this server".

0
 
rzvika3Author Commented:
Do you mean that i will get automatically an html error page?
If so, how can i know that there was an error? according to exception?
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now