package datatransfer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Client extends JPanel{
Client(){
this.setSize(300,300);
this.setLayout(null);
JButton Button_Choose = new JButton("選擇");
JButton Button_Send = new JButton("傳送");
JTextField DataSite = new JTextField();
JTextField IP = new JTextField();
Button_Choose.setSize(60,25);
Button_Send.setSize(60,25);
DataSite.setSize(200,25);
IP.setSize(200,25);
Button_Choose.setLocation(215, 100);
Button_Send.setLocation(215, 135);
DataSite.setLocation(10, 100);
IP.setLocation(10, 135);
DataSite.setText("請選擇檔案");
IP.setText("請輸入欲傳送之IP位置");
this.add(Button_Choose);
this.add(Button_Send);
this.add(DataSite);
this.add(IP);
Button_Choose.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try
{
JFileChooser FileChooser =new JFileChooser();
int ret = FileChooser.showOpenDialog(null);
if( ret == JFileChooser.APPROVE_OPTION)
{
DataSite.setText(FileChooser.getCurrentDirectory()+"\\"+FileChooser.getSelectedFile().getName());
}
} catch (Exception e2){
JOptionPane.showMessageDialog(null,"Error","Error",JOptionPane.ERROR_MESSAGE);
}
}
});
Button_Send.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
try {
File file = new File(DataSite.getText());
Socket Socket_Server = new Socket(IP.getText(), 9999);
JOptionPane.showMessageDialog(null,"開始傳送檔案!");
PrintStream printStream =
new PrintStream(Socket_Server.getOutputStream());
printStream.println(file.getName());
BufferedInputStream inputStream =
new BufferedInputStream(
new FileInputStream(file));
int readin;
while((readin = inputStream.read()) != -1) {
printStream.write(readin);
Thread.yield();
}
printStream.flush();
printStream.close();
inputStream.close();
Socket_Server.close();
JOptionPane.showMessageDialog(null,"傳送完成!");
}
catch(Exception e2) {
;
}
}
});
}
}