当前位置:文档之家› Java远程传输文件(增加覆盖取消等功能)

Java远程传输文件(增加覆盖取消等功能)

发送端/*2011*by小郭*远程文件传输**/import javax.swing.*;import .*;import java.io.*;import java.awt.*;import ng.*;import java.awt.event.*;public class TcpSend extends JFrame implements ActionListener {private JButton button;private JFileChooser chooser;//private FileInputStream in;//private String filename;//byte[] by=new byte[100000];public TcpSend(){super("小郭文件传输发送端");this.setBounds(10,10,400,400);this.setLayout(null);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setResizable(false);this.setVisible(true);chooser=new JFileChooser();button=new JButton("发送");button.setFont(new Font("楷体",Font.PLAIN,30));button.setBounds(0,0,400,400);add(button);button.addActionListener(this);}public void actionPerformed(ActionEvent e){chooser.showOpenDialog(this);if(chooser.showOpenDialog(this)==JFileChooser.CANCEL_OPTION){button.setText("取消文件发送");//小bug 按2次才能取消return;}Send send=new Send(chooser.getSelectedFile());send.start();button.setText("文件已发送");}public static void main(String[] args){new TcpSend();}}//end TcpSendclass Send extends Thread{private File file;private Socket socket;private DataOutputStream Dout;private DataInputStream Din;BufferedInputStream buffered;Send(File file){this.file = file;try{socket= new Socket("localhost",1111);//localhost可以改成IP 如果是内网直接填内网IP 外网的话IP要映射buffered=new BufferedInputStream(socket.getInputStream());//创建一个缓冲区数组,保存套接字s输入流,以便使用Din = new DataInputStream(buffered);//数据输入流,用来读取Dout = new DataOutputStream(socket.getOutputStream());//数据输出流,用来写入由数据输入流读取的数据} catch (IOException e) {e.printStackTrace();}}public void run(){try{Dout.writeUTF(file.getName());//将文件名写入输出流JOptionPane.showMessageDialog(null,"发送的文件是:"+file.getName());boolean isAccepted = Din.readBoolean();//接收端是否读取输入字节if(isAccepted){// JOptionPane.showMessageDialog(null,"对方已经接受文件传输,点击确定开始传输!");BufferedInputStream Bin = new BufferedInputStream(new FileInputStream(file));//创建一个缓冲区数组,保存文件输入流byte[] by = new byte[100000];int l;while(( l =Bin.read(by))!= -1)//从输入流中将各字节读取到by数组中只要不是=-1 如果=-1即到达流末尾就跳出循环{Dout.write(by,0,l);//将by数组中从偏移地址0开始的1个字节写入输出流。

Dout.flush();//清空数据输出流//l = Bin.read(by);多了这一句照成接收的文件大小只有一半的容量}Bin.close();//关闭缓冲输入流// JOptionPane.showMessageDialog(null,file.toString()+"\n文件发送完毕!");}//end if}//end trycatch (IOException e){e.printStackTrace();}finally//保证即使因为异常,try里面的代码不会被执行,但是finally里面的语句还是会执行,这样可以释放一些资源{try{Din.close();//关闭数据输入流Dout.close();//关闭数据输出流socket.close();}catch (IOException e){e.printStackTrace();}}//end finally}//end run()}//end Send(线程类)接收端import javax.swing.*;import .*;import java.io.*;import java.awt.*;import ng.*;import java.awt.event.*;public class TcpReceive extends JFrame implements ActionListener {private JButton button1,button2;private JLabel label;private Socket socket;private ServerSocket ss;private String filename;public TcpReceive(){super("小郭文件传输接收端");this.setBounds(420,420,400,400);setLayout(null);setVisible(true);this.setResizable(false);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);label=new JLabel();label.setText("暂时没收到文件请求,请等待。

");button1=new JButton("Accept");button2=new JButton("Cancel");add(label);add(button1);add(button2);label.setBounds(10,10,300,300);button1.setBounds(60,310,100,30);button2.setBounds(240,310,100,30);button1.addActionListener(this);button2.addActionListener(this);try{ss=new ServerSocket(1111);//绑定到端口1111的服务器套接字while(!ss.isClosed()){socket=ss.accept();//侦听并接受到此套接字的连接DataInputStream Din = new DataInputStream(socket.getInputStream());//数据输入流,用来读取从1111端口接收到的输入流filename = Din.readUTF();//读取对方发过来的字符串(即文件名)label.setText(filename);}}catch (IOException e){if(ss.isClosed())//端口关闭就退出{JOptionPane.showMessageDialog(this,"端口已关闭,程序退出");System.exit(0);}else{ e.printStackTrace();}}}//end构造函数public void actionPerformed(ActionEvent e){if(e.getSource()==button1){JFileChooser chooser=new JFileChooser();chooser.setSelectedFile(new File(filename));//路径抽象化chooser.showSaveDialog(this);chooser.getName(chooser.getSelectedFile());Receive receive = new Receive(chooser.getSelectedFile(),socket);//创建Receive线程对象用来启动线程类if(chooser.showSaveDialog(this)==JFileChooser.APPROVE_OPTION)//bug:按2次保存才保存下来{//System.out.println("bbb");if(chooser.getSelectedFile().exists())//测试此抽象路径名表示的文件或目录是否存在{int over=JOptionPane.showConfirmDialog(this,"文件"+chooser.getSelectedFile().getName()+"已经存在,确定覆盖吗?","覆盖与否", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);if(over==JOptionPane.YES_OPTION)//bug:按2次确定才能执行{//else{System.out.println("aaa");}receive.start();}//end if}//end ifelse{receive.start();} //System.out.println("aaaa");} //bug:按2次取消才能取消第二次取消才打印}//end if}//end ifif(e.getSource()==button2){if(label.getText()=="暂时没收到文件请求,请等待。

相关主题