import java.sql.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
class FileCopyTarget {
JPanel panel;
JFrame jf;
JLabel label1,label2;
JButton Browse,Target,Copy;
JTextField textfield1,textfield2,textfield3;
JPasswordField passwordfield;
String inputFile;
String filename,location;
public FileCopyTarget() {
initComponents();
handlingEvents();
}
public void initComponents() {
jf=new javax.swing.JFrame();
jf.setTitle("File target");
jf.setLayout(null);
jf.setSize(800,500);
jf.show();
jf.setVisible(true);
JScrollPane scrollBar=new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
jf.add(scrollBar);
label1=new javax.swing.JLabel("Target Location");
label1.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
label1.setBounds(50,130,200,40);
jf.add(label1);
textfield2=new javax.swing.JTextField();
textfield2.setFont(new Font("Monotype Corsiva", Font.BOLD, 18));
textfield2.setBounds(250,130,200,30);
jf.add(textfield2);
Target=new javax.swing.JButton("Browse");
Target.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Target.setBounds(480,130,140,30);
jf.add(Target);
Copy=new javax.swing.JButton("Copy");
Copy.setFont(new Font("Monotype Corsiva", Font.BOLD, 24));
Copy.setBounds(300,180,140,30);
jf.add(Copy);
}
public void handlingEvents() {
Target.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fc.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
location=fc.getSelectedFile().getPath();
textfield2.setText(location);
}
}
});
Copy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
receive();
}
});
}
public void receive() {
try {
Vector vec=new Vector();
Socket s = new Socket("",2000);
InputStream is = s.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
vec = (Vector)ois.readObject();
filename = (String)vec.elementAt(0);
ois.close();
s.close();
}
catch(Exception e) {
System.out.println(e);
}
try {
int filesize = 6022386;
long start = System.currentTimeMillis();
int bytesRead;
int current = 0;
//local host for testing
Socket sock = new Socket("",2345);
System.out.println("Connecting…");
//Receive file:
byte[] mybytearray = new byte[filesize];
InputStream is = sock.getInputStream();
FileOutputStream fos = new FileOutputStream(location+"/"+filename);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bytesRead = is.read(mybytearray,0,mybytearray.length);
System.out.println("bytesRead="+bytesRead);
current = bytesRead;
do {
bytesRead = is.read(mybytearray,current,(mybytearray.length-current));
if(bytesRead >= 0)
current+=bytesRead;
}
while(bytesRead >-1);
System.out.println("current="+current);
bos.write(mybytearray,0,current);
bos.flush();
long end = System.currentTimeMillis();
System.out.println(end-start);
bos.close();
sock.close();
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
FileCopyTarget log = new FileCopyTarget();
}
}
catch(Exception e) {
System.out.println(e);
}
}
public static void main(String args[]) {
FileCopy log = new FileCopy();
}
}