🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #1260

Re: How to download via HTTP a file in java ?

#1260
From: John
Date: Sun, 17 Mar 2002 14:53
96 lines
4032 bytes
Take a look at this chunk of code.  I have a vector of URL objects that contain
filenames on some webserver.

I get the name of the url file, and transform it into a windows valid filename.
Then open a stream to the webserver, and just start reading bytes.  There are
more efficient ways of reading the file.  I wrote this when I first started
learning Java.  Now I would read a huge buffer, but you can figure this out.

Should do you well.  I can't believe that I commented it.  :)

John 


  protected void downloadImages()
  {
     StringBuffer fileName;
     int info;
     
     // ImgList is a vector of URL objects that contain jpeg files names.


     for( int x=0; x<ImgList.size(); x++ )
     {
        // create the file name to save this to.
        fileName = new StringBuffer( ((URL)ImgList.elementAt(x)).getHost() +
((URL)ImgList.elementAt(x)).getFile() );

        try
        {
           // create a proper file name
           for( int y=0; y<fileName.length(); y++)
              if( (fileName.charAt(y) == '/') || (fileName.charAt(y) == ':'))
                 fileName.setCharAt(y,'_');


           // create the buffered output file stream
           BufferedOutputStream webSaveFile = new BufferedOutputStream(
                                                new
FileOutputStream(filePath+fileName.toString() ) );

           // create the buffered input stream
           BufferedInputStream webStream = new BufferedInputStream(
((URL)ImgList.elementAt(x)).openStream(), 65536 );

           System.out.println(getName() + " Downloading(" + x + ")  " +
ImgList.elementAt(x) + "   to   " + fileName);

           // move throught the input stream one byte at a time
           // and put the byte into the output stream.
           while(  (info = webStream.read()) != -1 )
              webSaveFile.write( (byte)info );

           // count the number of images downloaded
           imagesDL++;

           webSaveFile.close();

//           yield();
        }
        catch( IOException e )
        {
           System.out.println("Error: " + e.getMessage() );
           System.out.println("  " + getName() + " IOException,     " +
((URL)ImgList.elementAt(x)) + "      " + filePath + fileName);
        }
     }

     // clear image list
     System.out.println("Clearing Image List");
     ImgList.removeAllElements();
  }

}// end of class




On Mon, 11 Mar 2002 17:58:39 +0100, "Laurent Souriau"
<lsouriau@club-internet.fr> wrote:

>        hi,
>
>
>I would like to know if it is possible to :
>
>- check existence of a  file on a  client side
>
>- download this file on the  client PC via http
>with java instructions ?
>
>If yes, how do you do it ?
>
>Thank you
>
>Laurent
>
Technical Details
Message-ID: <s6b99ucdds6mkv86m31m71f3p5ua3vr0ai@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!newsfeed.wirehub.nl!news.stealth.net!news.stealth.net!news-east.rr.com!news-west.rr.com!cyclone.tampabay.rr.com!news-post.tampabay.rr.com!typhoon.tampabay.rr.com.POSTED!not-for-mail
References: <a6ingf$7l0$1@s1.read.news.oleane.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,cbc90b6df5988189
X-Google-Attributes: gid1089d0,public
X-Google-Thread: f74b3,cbc90b6df5988189
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2002-03-17 06:53:44 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!newsfeed.wirehub.nl!news.stealth.net!news.stealth.net!news-east.rr.com!news-west.rr.com!cyclone.tampabay.rr.com!news-post.tampabay.rr.com!typhoon.tampabay.rr.com.POSTED!not-for-mail
From: John <cyllan@pobox.com>
Newsgroups: borland.public.jbuilder.java.api,borland.public.jbuilder.java.language
Subject: Re: How to download via HTTP a file in java ?
Message-ID: <s6b99ucdds6mkv86m31m71f3p5ua3vr0ai@4ax.com>
References: <a6ingf$7l0$1@s1.read.news.oleane.net>
X-Newsreader: Forte Agent 1.9/32.560 trialware
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 97
Date: Sun, 17 Mar 2002 14:53:43 GMT
NNTP-Posting-Host: 24.92.190.198
X-Complaints-To: abuse@rr.com
X-Trace: typhoon.tampabay.rr.com 1016376823 24.92.190.198 (Sun, 17 Mar 2002 09:53:43 EST)
NNTP-Posting-Date: Sun, 17 Mar 2002 09:53:43 EST
Organization: RoadRunner - TampaBay