Solved

Downloading a file

Posted on 1998-11-15
6
199 Views
Last Modified: 2010-03-30
Hi,

I'm writing a Java applet (class) that need to download a file from the web and store it on the user's system.
What class/method can I use to download a file?
I looked at URL.getContent() but it seems to download
the content of the file rather than the file itself.

Thanks,
Ralph
0
Comment
Question by:rakhras
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 20 total points
ID: 1227533
you probably need:

' public final InputStream openStream() throws IOException
    Opens a connection to this URL and returns an InputStream for reading from that connection. This method is a shorthand for: openConnection().getInputStream() '

you'll receive the file as a InputStream (so you can only read from it).

Note: ordinary applets are not allowed to access local file system (security restrictions). You should either 'sign it' or use an application.

hope this helps
  heyhey
0
 

Author Comment

by:rakhras
ID: 1227534
so, i did:

URL url = new URL(text);
URLConnection urlConnection = url.openConnection();      
InputStream inputStream = urlConnection.getInputStream();

How do I save this information to a local file?
The file I'm downloading is indeed signed.

Thanks,
Ralph

0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227535
>> The file I'm downloading is indeed signed.
you mean that your Applet is signed ??

so saving into a file (sorry haven't tested it - working from memory )

URL url = new URL(text);
      URLConnection urlConnection = url.openConnection();
      InputStream is = urlConnection.getInputStream();
FileOutputStream file = new FileOutputStream("c:/myfile.dat");

int len;
byte[] buf = new byte[200];
do {
  len = is.read(buf); // fill the buffer from input stream (at most 200 bytes)
  file.write(buf, len);  // write the bufer to file
} while (len==200);

file.close();

i think you got the idea. if you still have problems I can send you the whole (working) example
  heyhey
(again - i don't have neighter JDK, nor any Java documenation - test it yourself :)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:rakhras
ID: 1227536
heyhey,

If you have an example, that'd be great!
I tried what you suggested  and now I get the following error:

JavaScript Error:
uncaught Java exception netscape/security/AppletSecurityException
("security.Couldn't resolve IP for host www.metaquest.com or for
")

This is the code:

URL url = new URL(text);
URLConnection urlConnection = url.openConnection();      
int len = urlConnection.getContentLength();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("c:/myfile.dat");

byte[] buf = new byte[len];
int nBytesRead;
// fill the buffer from input stream
nBytesRead = inputStream.read(buf, 0, len);       
outputStream.write(buf);  // write the bufer to file
outputStream.close();


I signed my applet and it made no difference.
Any idea?
Thanks,
Ralph
0
 

Author Comment

by:rakhras
ID: 1227537
heyhey,

If you have an example, that'd be great!
I tried what you suggested  and now I get the following error:

JavaScript Error:
uncaught Java exception netscape/security/AppletSecurityException
("security.Couldn't resolve IP for host www.metaquest.com or for
")

This is the code:

URL url = new URL(text);
URLConnection urlConnection = url.openConnection();      
int len = urlConnection.getContentLength();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream outputStream = new FileOutputStream("c:/myfile.dat");

byte[] buf = new byte[len];
int nBytesRead;
// fill the buffer from input stream
nBytesRead = inputStream.read(buf, 0, len);       
outputStream.write(buf);  // write the bufer to file
outputStream.close();


I signed my applet and it made no difference.
Any idea?
Thanks,
Ralph
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227538
first check the URL
try "ping www.metaquest.com" at the DOS prompt. it seems that www.metaquest.comis unreacheable ...

(is this the real URL - i can try it myself ...)
  heyhey
 
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question