发送端
/*2011
*by小郭
*远程文件传输
**/
import javax.swing.*;
import https://www.doczj.com/doc/309071267.html,.*;
import java.io.*;
import java.awt.*;
import https://www.doczj.com/doc/309071267.html,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 TcpSend
class 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 try
catch (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 https://www.doczj.com/doc/309071267.html,.*;
import java.io.*;
import java.awt.*;
import https://www.doczj.com/doc/309071267.html,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 if
else{receive.start();} //System.out.println("aaaa");} //bug:按2次取消才能取消第二次取消才打印
}//end if
}//end if
if(e.getSource()==button2)
{
if(label.getText()=="暂时没收到文件请求,请等待。。。")
{
JOptionPane.showMessageDialog(this,"没收到请求哦!");
}else
{
JOptionPane.showMessageDialog(this,"正在退出!");
System.exit(0);
}
}//end if
}//end actionPerformed
public static void main(String args[])
{
new TcpReceive();
}
}//end TcpReceive
class Receive extends Thread
{
private File file;
private Socket socket;
int l;
public Receive(File file, Socket socket)
{
this.file=file;
this.socket=socket;
}
public File location()
{
return file;
}
public void run()
{
if(file == null)
{
JOptionPane.showMessageDialog(null,"没有监听到文件");
return;
}else
{
try
{
DataOutputStream Dout = new DataOutputStream(socket.getOutputStream());//数据输出流,用来写入由数据输入流读取的数据
Dout.writeBoolean(true);
} catch (IOException e)
{e.printStackTrace();}
}//end else
try
{
FileOutputStream fout = new FileOutputStream(file);//创建一个向指定file表示的文件中写入数据的文件输出流。即上面的chooser.getSelectedFile()
BufferedOutputStream Bout = new BufferedOutputStream(fout);//创建一个新的缓冲输出流,以将数据写入指定的输出流。
BufferedInputStream Bin = new BufferedInputStream(socket.getInputStream());//创建一个缓冲区数组,保存套接字socket输入流,以便使用
byte[] by = new byte[100000];
int l ;
int yes=JOptionPane.showConfirmDialog(null,"点击是,文件开始接收");
if(yes==JOptionPane.YES_OPTION)
{// JOptionPane.showMessageDialog(null,"点击确定,文件开始接收");
while((l=Bin.read(by)) != -1)//从输入流中将各字节读取到by数组中只要不是=-1 如果=-1即到达流末尾就跳出循环
{
Bout.write(by,0,l);//将by数组中从偏移地址0开始的1个字节写入输出流。
Bout.flush();
}
Bout.close();
Bin.close();
JOptionPane.showMessageDialog(null,"成功接收文件\n位置是:"+file);
}//end if
else
{
Bout.close();
Bin.close();
file.delete();
}
}//end try
catch (Exception e) {e.printStackTrace();}
finally//保证即使因为异常,try里面的代码不会被执行,但是finally里面的语句还是会执行,这样可以释放一些资源
{
try
{
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}//end finally
}//end run
}//end Receive(线程类)