DownloadApplet.java
-------------------
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class DownloadApplet extends Applet implements ActionListener {
private Vector urls = new Vector();
private TextArea list = new TextArea();
private Button download = new Button();
public void init() {
int n = 1;
String urlstr;
while((urlstr = getParameter("url" + n)) != null) {
urls.addElement(urlstr);
n++;
}
list.setColumns(60);
list.setRows(20);
list.setText("");
for(int i = 0; i < urls.size(); i++) {
list.append((String)urls.elementAt(i) + "\r\n");
}
download.setLabel("Download");
download.addActionListener(this);
add(list, null);
add(download, null);
this.setSize(500, 400);
setVisible(true);
}
public void actionPerformed(ActionEvent ev) {
for(int i = 0; i < urls.size(); i++) {
String urlstr = (String)urls.elementAt(i);
String fnm = urlstr.substring(urlstr.lastIndexOf("/") + 1);
download(urlstr, fnm);
}
}
private void download(String urlstr, String fnm) {
try {
String dir = System.getProperty("java.io.tmpdir");
dir = dir.substring(0, dir.length() - 1);
dir = dir.substring(0, dir.lastIndexOf(File.separator));
dir = dir.substring(0, dir.lastIndexOf(File.separator) + 1);
dir = dir + "Desktop";
URL url = new URL(urlstr);
HttpURLConnection con = (HttpURLConnection)url.openConnection();
con.connect();
if(con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = con.getInputStream();
OutputStream os = new FileOutputStream(dir + File.separator + fnm);
byte[] b = new byte[100000];
int n;
while((n = is.read(b)) >= 0) {
os.write(b,0,n);
}
os.close();
is.close();
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
download.html
-------------
<applet codebase="." code="DownloadApplet.class" archive="DownloadApplet.jar" width="500" height="400">
<param name="url1" value="
http://localhost/upload.html"><param name="url2" value="
http://localhost/download.html"></applet>