Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (289)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Downloading files via HTTP  (Read 1358 times)
0 Members and 1 Guest are viewing this topic.
Offline adamp

Senior Newbie




-x-x-x-


« Posted 2004-03-16 13:37:28 »

Hi there,

I need to download a file from an internet adress like
"http://www.someserver.com/somedoc.pdf".
Is there a short answer to solve this problem?
Have googled a lot, but don't find, what I am looking for.

What I want: a simple class that ask for a URL and saves
the document (PDF in my case) on my harddisk.

Thank you, AdamP
Offline Matzon
« League of Dukes »

JGO Knight


Medals: 13
Projects: 2


I'm gonna wring your pants!


« Reply #1 - Posted 2004-03-16 13:39:54 »

http://java.sun.com/j2se/1.4.2/docs/api/java/net/URLConnection.html ?

Offline vrm

Junior Member




where I should sign ?


« Reply #2 - Posted 2004-03-16 15:20:49 »

from www.rganon.com

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
 import java.io.*;
 import java.net.*;

 public class suckURL {
   String aFile;
   String aURL;

   public static void main(String args[]) {
     // GIF  JAVA How-to  at Real's Home
    String url =
       "http://www.rgagnon.com/images/";
     suckURL b = new suckURL(url, "jht.gif");
     b.doit();
     }

   suckURL(String u, String s){
     aURL  = u;
     aFile = s;
     }

   public void doit() {
     DataInputStream di = null;
     FileOutputStream fo = null;
     byte [] b = new byte[1];  
       
     try {
       System.out.println("Sucking " + aFile);
       System.out.println("   at " + aURL );
       // input
      URL url = new URL(aURL + aFile);
       URLConnection urlConnection = url.openConnection();
       urlConnection.connect();
       di = new DataInputStream(urlConnection.getInputStream());

       // output
      fo = new FileOutputStream(aFile);

       //  copy the actual file
      //   (it would better to use a buffer bigger than this)
      while(-1 != di.read(b,0,1))
         fo.write(b,0,1);
         di.close();  
         fo.close();                
         }
       catch (Exception ex) {
         System.out.println("Oups!!!");
         ex.printStackTrace();
         System.exit(1);
         }
       System.out.println("done.");  
     }
 }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline adamp

Senior Newbie




-x-x-x-


« Reply #3 - Posted 2004-03-16 21:12:36 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
import java.io.*;
import java.net.*;

class Get
{

      public static void main( String[] args )
      {
            if (args.length == 0) {
                  System.out.println ("Usage  : Java Get <dokument>");
                  System.out.println ("Example: Java Get http://www.server.com/doc.pdf");
                  System.exit(1);
            }
            try
            {
                  URL url = new URL(args[0]);

                  InputStreamReader in = new InputStreamReader (url.openStream());
                  String name = args[0].substring (args[0].lastIndexOf ("/")+1);
                  FileOutputStream fos = new FileOutputStream (name);

                  char[] buf = new char[1024];
                  byte[] buf2 = new byte[1024];
                  int nread = 0;

                  System.out.println ("Lade Datei: "+name);
                  while ( ( nread = in.read (buf) ) > 0 ) {
                        for (int i=0; i<nread; i++)
                        buf2[i] = (byte)buf[i];
                        fos.write (buf2, 0 ,nread);
                  }
                  in.close();
                  fos.close ();
                  System.out.println ("Ready.");
            }
                  catch ( Exception e ) {
                  System.out.println("Error");
            }
      }
}


Thanks for all your help, I use the code above now!
Greetings, AdamP
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (60 views)
2013-05-17 21:29:12

alaslipknot (69 views)
2013-05-16 21:24:48

gouessej (99 views)
2013-05-16 00:53:38

gouessej (96 views)
2013-05-16 00:17:58

theagentd (107 views)
2013-05-15 15:01:13

theagentd (98 views)
2013-05-15 15:00:54

StreetDoggy (144 views)
2013-05-14 15:56:26

kutucuk (166 views)
2013-05-12 17:10:36

kutucuk (165 views)
2013-05-12 15:36:09

UnluckyDevil (175 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.256 seconds with 21 queries.