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
Solved

Downloading a file

Posted on 1998-11-15
6
196 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 72
Java string replace 11 54
ejb example issues 3 26
printf performancy 11 70
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

860 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