This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi Java Ranchers; is there a way to directly save an html page of a site using a servlet and save it directly on to the hard disc without opening a browser? Thanks in advance, Anand.
Anand
Sandeep Jain
Ranch Hand
Joined: Oct 25, 2000
Posts: 124
posted
0
Hi , I dont know whether ur requirement was the same or not but what I could undestand can be achieved through this . Make use of URL Class in an application , get all the data and store it in buffer and later on write it to the file . ------------------ Sandeep Jain
Try and Try Till u succeed<br /> <br />Sandeep Jain
Frank Carver
Sheriff
Joined: Jan 07, 1999
Posts: 6913
posted
0
You do it in a servlet the same way you do in any other Java class. Open a URLConnection to the desired URL and read from the provided inputStream. Then to save to disk, just write the contents of the stream out to a FileOutputStream.
I tried it and it works. Here is the code. import java.net.*; import java.io.*; public class savefile { public static void main(String args[]) { try { URL tempUrl = new URL("http://www.hotmail.com/"); InputStream inStr = tempUrl.openConnection().getInputStream(); BufferedInputStream bins = new BufferedInputStream(inStr); FileOutputStream fostreame = new FileOutputStream("out.html"); int c; while((c= bins.read())!=-1) { fostreame.write(c); } fostreame.close(); bins.close(); inStr.close();
} catch (Exception e) { e.printStackTrace(); }
} } Thanks guys, Anand.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.