The Artima Developer Community
Sponsored Link

Java Answers Forum
Download web Page from java application

2 replies on 1 page. Most recent reply: Jul 1, 2002 9:52 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Abhay Srivastava

Posts: 1
Nickname: abhay
Registered: Jul, 2002

Download web Page from java application Posted: Jun 30, 2002 10:25 PM
Reply to this message Reply
Advertisement
Hello,
I am trying to download web page through a java application. I tried using URLConnection but it is not working. Is there some other way out.
Abhay


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Download web Page from java application Posted: Jun 30, 2002 10:45 PM
Reply to this message Reply
What do you mean by "download a page?" Do you want to save the content of a web page to disk? Do you just want to open it in a browser?

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Download web Page from java application Posted: Jul 1, 2002 9:52 AM
Reply to this message Reply

/* RemoteFileCopy.java
* @author: Charles Bell
* @version: May 24, 2002
* Java program which copies a file from a remote location
* on the Internet to a users local hard drive.
*/

import java.awt.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

/** This Java program copies a file from a remote location
* on the Internet to a users local hard drive. The File to copy
* is designated as an argument either to the run option or the default
* Constructor which takes a String argument of rthe URL to copy from.
* The interface brings up a SaveAs FileDialog to prompt the user for a
* file nmae to save it to with the file name defaulted to the filename
* associated with the URL. This provdes the user with the flexibility to
* change the filename as desired.
*/
public class RemoteFileCopy extends JFrame{

private String urlstring;

/** Constructor needs a String to construct a URL from as a minimum.
*/
RemoteFileCopy(String urlstring){
super("RemoteFileCopy");
this.urlstring = urlstring;
copy(urlstring);
}

/** Instantiates an instance of this class and class the copy method.
* Checks to ensure that one String argument is present.
*/
public static void main(String[] args){
if (args.length != 1){
System.out.println("Useage: java RemoteFileCopy urlstring");
}else if (args.length == 1){
RemoteFileCopy remoteCopier = new RemoteFileCopy(args[0]);
System.exit(0);
}
}

/** Copies the file associated with the urlstring argument to the local
* hard drive with the filename selected by the user as a result of a
* SaveAs FileDialog action.
*/
public void copy(String urlstring){

FileOutputStream fos = null;
DataInputStream dis = null;

try{
URL url = new URL(urlstring);
//get the filename from the URL
String urlfilename = url.getFile();
if (urlfilename.indexOf("/") >= 0){
urlfilename = urlfilename.substring(urlfilename.lastIndexOf("/")+1);
}
URLConnection connection = url.openConnection();
dis = new DataInputStream(connection.getInputStream());
//bring up a SaveAs FileDialog
File currentDirectory = new File(".");
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setCurrentDirectory(currentDirectory);
chooser.setSelectedFile(new File(urlfilename));
int returnvalue = chooser.showSaveDialog(new JFrame());
if (returnvalue == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
fos = new FileOutputStream(selectedFile);
while (true){
fos.write(dis.readByte());
}
}
}catch(NullPointerException npe){
System.out.println("You must enter a filename to save your file to. ");
}catch(EOFException eofe){
try{
//This ensures the streams are closed properly
dis.close();
fos.close();
}catch(IOException ioe){
System.out.println("IOException: " + ioe.getMessage());
}
}catch(MalformedURLException murle){
System.err.println("MalformedURLException: " + murle.getMessage());
}catch(IOException ioe){
System.err.println("IOException: " + ioe.getMessage());
}
}
}

Flat View: This topic has 2 replies on 1 page
Topic: dll and java Previous Topic   Next Topic Topic: Tomcat Installation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use