Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Downloading a file

Posted on 1998-11-15
6
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
heyhey_ earned 40 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

618 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