HTML Script Downloader
--------------------------------------------------------------------------------------------------------------------------
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyException extends Exception{
String msg="No TextField Should be left UnFilled";
public String toString(){return String.format("Required :"+msg);}
}
public class htmldownloader extends JFrame implements ActionListener{
Label l1,l2,l3;
TextField tf1,tf2,tf3;
Button btn1;
Panel p1,p2,p3;
htmldownloader(){
super("Page Downloader");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
setSize(400,400);
l1 = new Label("Enter a valid url with protocol :");
l2 = new Label("Enter File Destinition (use \\) :");
l3 = new Label("Enter the File name:");
tf1 = new TextField("http://www.apexfreak.tk");
tf2 = new TextField("C:\\Users\\user\\Desktop\\");
tf3 = new TextField("demo.txt");
btn1 = new Button("Download");
btn1.setBounds(170,210,60,50);
p1 = new Panel(new FlowLayout());
p1.setBounds(20,20,260,50);
p2 = new Panel(new FlowLayout());
p2.setBounds(20,90,260,50);
p3 = new Panel(new FlowLayout());
p3.setBounds(20,160,260,50);
p1.add(l1);p1.add(tf1);
p2.add(l2);p2.add(tf2);
p3.add(l3);p3.add(tf3);
add(p1);add(p2);add(p3);
add(btn1);
btn1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae){
try{
if(tf1.getText().length()==0 || tf2.getText().length()==0 || tf3.getText().length()==0)
throw new MyException();
int c;
File f = new File(tf2.getText()+tf3.getText());
f.createNewFile();
f.setWritable(true);
URL url = new URL(tf1.getText());
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
OutputStream os = new FileOutputStream(f);
while((c=is.read())!=-1)
os.write((byte)c);
os.flush();
os.close();
is.close();
}catch(Exception e){
JOptionPane.showMessageDialog(this,e);
}
}
public static void main(String[] ag){
new htmldownloader();
}
}
Comments
Post a Comment