Solved

Downloading a file

Posted on 1998-11-15
6
188 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why cannot we forward request once the response is committed 2 42
ForLoop Example 3 47
spring jars download 1 26
eclipse buid path vs tomcat lib path 10 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 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

10 Experts available now in Live!

Get 1:1 Help Now