package fileUpload;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
public class FileUpload_Client extends JFrame implements ActionListener, Runnable{
/**
*
*/
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField currentUpload;
private JProgressBar progressBar;
private JTextArea uploads;
private File[] selectedFiles;
private JButton upload;
private boolean selectionHasNotYetBeenMade=true;
private JLabel lblNewLabel;
private JLabel lblCurrentFilesProgress;
private Socket channel;
private ObjectInputStream ois;
private ObjectOutputStream oos;
/**
* Create the frame.
*/
public FileUpload_Client(Socket channel) {
this.channel=channel;
setResizable(false);
setTitle("Remote Administrator");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblPleaseUploadyourFiles = new JLabel("Please upload your files by clicking the button here:");
lblPleaseUploadyourFiles.setFont(new Font("Dialog", Font.BOLD, 10));
lblPleaseUploadyourFiles.setBounds(12, 12, 306, 15);
contentPane.add(lblPleaseUploadyourFiles);
upload = new JButton("Upload");
upload.addActionListener(this);
upload.setBounds(317, 9, 105, 19);
contentPane.add(upload);
JLabel lblFileCurrentlyBeing = new JLabel("File currently being uploaded:");
lblFileCurrentlyBeing.setFont(new Font("Dialog", Font.BOLD, 10));
lblFileCurrentlyBeing.setBounds(12, 32, 196, 15);
contentPane.add(lblFileCurrentlyBeing);
currentUpload = new JTextField();
currentUpload.setEnabled(false);
currentUpload.setHorizontalAlignment(SwingConstants.CENTER);
currentUpload.setOpaque(false);
currentUpload.setEditable(false);
currentUpload.setBounds(226, 32, 196, 14);
contentPane.add(currentUpload);
currentUpload.setColumns(10);
progressBar = new JProgressBar();
progressBar.setEnabled(false);
progressBar.setBounds(226, 59, 196, 15);
contentPane.add(progressBar);
uploads = new JTextArea();
uploads.setFont(new Font("Dialog", Font.PLAIN, 10));
uploads.setEnabled(false);
uploads.setEditable(false);
uploads.setBounds(12, 111, 410, 140);
contentPane.add(uploads);
lblNewLabel = new JLabel("List of uploaded files:");
lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 10));
lblNewLabel.setBounds(12, 84, 161, 15);
contentPane.add(lblNewLabel);
lblCurrentFilesProgress = new JLabel("Current file's progress bar:");
lblCurrentFilesProgress.setFont(new Font("Dialog", Font.BOLD, 10));
lblCurrentFilesProgress.setBounds(12, 59, 196, 15);
contentPane.add(lblCurrentFilesProgress);
setVisible(true);
}
@Override
public void run() {
try {
ois = new ObjectInputStream(channel.getInputStream());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
oos = new ObjectOutputStream(channel.getOutputStream());
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
System.out.println("ois and oos initialized");
while(selectionHasNotYetBeenMade){ }
for(int i=0; i<selectedFiles.length; i++){
//Sending over file's name
try {
oos.writeObject((String)selectedFiles[i].getName());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
currentUpload.setText(selectedFiles[i].getName());
try {
oos.writeLong(selectedFiles[i].length());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//Sending over its data
FileInputStream in = null;
try {
in = new FileInputStream(selectedFiles[i]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] buff = null;
try {
buff = new byte[channel.getSendBufferSize()];
} catch (SocketException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
int bytesReadNow = 0;
int bytesReadUntilNow = 0;
try {
while((bytesReadNow = in.read(buff))>0)
{
oos.write(buff,0,bytesReadNow);
oos.flush();
bytesReadUntilNow += bytesReadNow;
progressBar.setValue((int)(bytesReadUntilNow/selectedFiles[i].length()));
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
progressBar.setValue(100);
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
currentUpload.setText("");
uploads.append(selectedFiles[i].getName()+"\n");
}
try {
channel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser files_chooser = new JFileChooser();
files_chooser.setMultiSelectionEnabled(true);
files_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
if(files_chooser.showDialog(this, "Upload")== JFileChooser.APPROVE_OPTION){
selectedFiles=files_chooser.getSelectedFiles();
uploads.setEnabled(true);
currentUpload.setEnabled(true);
try {
oos.writeInt(selectedFiles.length);
oos.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
selectionHasNotYetBeenMade=false;
}
}
}