Solved

Downloading a file

Posted on 1998-11-15
6
165 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now