import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class UploadManagerApplet extends JApplet implements ActionListener {
private final static String UPLOAD_URL = "
http://localhost/upload.asp"; private JButton select;
private JTextArea files;
private JButton upload;
private String dirname;
private String[] filenames;
public void init() {
getContentPane().setLayout(new BorderLayout());
select = new JButton("Select dir");
select.addActionListener(this);
getContentPane().add(select, BorderLayout.NORTH);
files = new JTextArea();
getContentPane().add(new JScrollPane(files), BorderLayout.CENTER);
upload = new JButton("Upload");
upload.addActionListener(this);
getContentPane().add(upload, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent ev) {
if(ev.getSource() == select) {
JFileChooser chooser = new JFileChooser(System.getProperty("user.home"));
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File dir = chooser.getSelectedFile();
dirname = dir.getAbsolutePath();
filenames = dir.list();
StringBuffer sb = new StringBuffer("");
for(int i = 0; i < filenames.length; i++) {
sb.append(filenames[i]);
sb.append("\r\n");
}
files.setText(sb.toString());
}
}
if(ev.getSource() == upload) {
for(int i = 0; i < filenames.length; i++) {
upload(dirname, filenames[i]);
}
}
}
private void upload(String dirname, String filename) {
try {
URL url = new URL(UPLOAD_URL + "?filename=" + filename);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
InputStream is = new FileInputStream(dirname + File.separator + filename);
OutputStream os = con.getOutputStream();
byte[] b = new byte[100000];
int n;
while((n = is.read(b)) >= 0) {
os.write(b, 0, n);
}
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
//JOptionPane.showMessageDialog(this, filename + " uploaded");
} else {
JOptionPane.showMessageDialog(this, filename + " not uploaded");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, filename + " not uploaded");
}
}
}