关于文件传输的问题~
刚学java不久,对于类的理解不够深入,同学发我个文件传输的程序要偶讲讲其中类的功能介绍,以及类中主要方法,请各位大侠帮帮忙~
(服务器端)Receive.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class Receive implements
ActionListener {
JFileChooser jc;
JFrame f;
ServerSocket ss;
Socket socket;
JButton b1,b2;
TextArea txt;
DataInputStream din;
DataOutputStream dout;
public Receive() {
//界面布局
jc = new JFileChooser();
f = new JFrame("文件接收");
f.setLocation(100,300);
f.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
b1 = new JButton("另存
为",new ImageIcon("1.gif"));
b2 = new JButton("取消",new
ImageIcon("2.gif"));
txt=new TextArea(10,40);
JScrollPane scrollpane = new
JScrollPane(txt);
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
Container c =
f.getContentPane();
c.setLayout(new
BorderLayout());
c.add
(txt,BorderLayout.CENTER);
c.add(p,BorderLayout.NORTH);
f.setSize(400,150);
f.setVisible(true);
//注册事件
b1.addActionListener(this);
b2.addActionListener(this);
try { ss = new ServerSocket(4321);
while(!ss.isClosed()){
socket = ss.accept();
din = new DataInputStream
(socket.getInputStream());
txt.append("开始接受文
件:"+din.readUTF()+"\n");
}
} catch (IOException e) {
if(ss.isClosed()){
txt.append("End");
}else{
e.printStackTrace();
}
}
}
public void actionPerformed
(ActionEvent e)
{
//如果同意接收同,则启动
线程接收文件
if(e.getSource()==b1)
{ jc.showSaveDialog(f);
FileReceive r= new
FileReceive(jc.getSelectedFile
(),socket);
r.start();
}else if(e.getSource()==b2){
System.out.println("用
户取消接收!"+"\n");
System.exit(0);
}
}
class FileReceive extends Thread
{ File rfile;
Socket socket;
FileReceive(File rfile, Socket
socket) {
this.rfile = rfile;
this.socket = socket;
}
public void run() {
//判断用户是否保存文件
if(rfile == null){ txt.append("没
有保存!"+"\n"); return;
}
else{
//保存文件后,则向发送方
发送同意(true)
try {
dout = new
DataOutputStream
(socket.getOutputStream());
dout.writeBoolean(true);
} catch (IOException e) {
e.printStackTrace();
}
}txt.append("开始接
收..."+"\n");
try {
FileOutputStream fout =
new FileOutputStream(rfile);
BufferedOutputStream
bout = new BufferedOutputStream
(fout);
DataInputStream bin =
new DataInputStream
(socket.getInputStream());
byte[] buf = new byte
[2048];
int num = bin.read(buf);
while(num != -1){
bout.write
(buf,0,num);
bout.flush();
num = bin.read(buf);
}
bout.close();
bin.close();
txt.append("接收完成:"+
rfile.toString()+"\n");
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
socket.close();
} catch (IOException
e) {
e.printStackTrace();
}
}
}
}
public static void main(String[]
args)
{
new Receive();
}
}
(客户端)Send.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.Socket;
public class Send implements
ActionListener{
JFileChooser jc;
JFrame f;
Socket socket;
DataInputStream din;
DataOutputStream dout;
JButton b1;
JButton b2;
TextArea txt;
public Send() {
//界面布局
jc = new JFileChooser();
f = new JFrame("文件发送");
f.setLocation(100,100);
f.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
b1 = new JButton("发送",new
ImageIcon("go.gif"));
b2 = new JButton("取消",new
ImageIcon("2.gif"));
txt=new TextArea(10,40);
JScrollPane scrollpane = new
JScrollPane(txt);
JPanel jp = new JPanel();
jp.add(b1); jp.add(b2);
Container c =
f.getContentPane();
c.add
(jp,BorderLayout.NORTH);
c.add(scrollpane,
BorderLayout.CENTER);
f.setSize(400,150);
f.setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
}
public void actionPerformed
(ActionEvent e)
{ if(e.getSource() == b1)
{
jc.showOpenDialog(f);//弹出文件
选择对话框
SendFile send = new SendFile
(jc.getSelectedFile());
send.start();//启动新的线程传递
文件
}else if(e.getSource()==b2){
System.out.println("用
户取消发送!"+"\n");
System.exit(0);
}
}
class SendFile extends Thread{
File file;//用户选择的文件
public SendFile(File file)
{
this.file = file;
//初始化socket及其相关的输入输出
流
try {
socket = new Socket
("localhost",4321);
din = new DataInputStream
(socket.getInputStream());
dout = new DataOutputStream
(socket.getOutputStream());
} catch (IOException e)
{e.printStackTrace();}
}
public void run() {
try {
dout.writeUTF(file.getName());
//把文件名发送到接收方
txt.append("开始发送文
件:"+file.getName()+"\n");
boolean isAccepted =
din.readBoolean(); //判断接收方是否
同意接收
if(isAccepted){ //如果同意接收
则开始发送文件
txt.append("开始发送..."+"\n");
BufferedInputStream fin =
new BufferedInputStream(new
FileInputStream(file));
byte[] buf = new byte[2048];
int num = fin.read(buf);
while(num != -1){
dout.write(buf,0,num);
dout.flush();
num = fin.read(buf);
}
fin.close();
txt.append("发送完成:" +
file.toString()+"\n");
}
} catch (IOException e)
{e.printStackTrace();}
finally{
try {
din.close();
dout.close();
socket.close();
} catch (IOException
e) { e.printStackTrace(); }
}
}
}
public static void main(String[]
args)
{new Send();}
}
[解决办法]
帮顶
太长了没时间看
友情up一下
[解决办法]
- Java code
package lib;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;public class Receive implements ActionListener { JFileChooser jc; JFrame f; ServerSocket ss; Socket socket; JButton b1, b2; TextArea txt; DataInputStream din; DataOutputStream dout; public Receive() { // 界面布局 jc = new JFileChooser(); // 文件选择器初始化 f = new JFrame("文件接收 "); f.setLocation(100, 300); // 设置窗口位置 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点窗口叉叉,关闭窗口 b1 = new JButton("另存为 ", new ImageIcon("1.gif "));// 两个小按钮 b2 = new JButton("取消 ", new ImageIcon("2.gif ")); txt = new TextArea(10, 40); JScrollPane scrollpane = new JScrollPane(txt); // 可以下拉的面板 JPanel p = new JPanel(); p.add(b1); p.add(b2); Container c = f.getContentPane(); // 获取当前容器 // 布局 c.setLayout(new BorderLayout()); // 边缘布局(中东南西北) c.add (txt, BorderLayout.CENTER); // txt放中间 c.add(p, BorderLayout.NORTH); f.setSize(400, 150); // 设置窗体大小 f.setVisible(true); // 窗体设为可见 // 注册事件 b1.addActionListener(this); b2.addActionListener(this); try { ss = new ServerSocket(4321); // 服务端套接字初始化 // 不关闭,此循环不结束 while (!ss.isClosed()) { // 一直在4321端口监听着 socket = ss.accept(); // 套接字输入流(简单来说,就是你接收的信息都在这个流里面,它是源头) din = new DataInputStream(socket.getInputStream()); // txt将流里面的信息显示出来 txt.append("开始接受文件: " + din.readUTF() + "\n "); } } catch (IOException e) { // 如果服务端套接字已被关闭,在txt上加上"End" if (ss.isClosed()) { txt.append("End "); } else { e.printStackTrace(); } } } /** * 监听到事件后的实现 */ public void actionPerformed(ActionEvent e) { // 如果同意接收同,则启动线程接收文件(原注释) if (e.getSource() == b1) { // 如果是JButton b1上发生的事件,你将b1定义为“另存为” jc.showSaveDialog(f); // 系统保存对话框 // 这个是下面定义的类,下面解释 FileReceive r = new FileReceive(jc.getSelectedFile(), socket); r.start(); } else if (e.getSource() == b2) { // 不多说了 System.out.println("用户取消接收! " + "\n "); System.exit(0); // 用户不接收,就退出,这下子窗口也关闭了吧!设计得挺奇怪的 // 难道不接收,我就不能干其他事情?! } } /** * 内部类,是线程的子类 * @author bbb 我的计算机名真龊(^_^) 呵呵,这里与你无关 */ class FileReceive extends Thread { File rfile; Socket socket; // 带两个参数的构造方法,传文件和套接字进来 // 上面调用时,将你选择的文件和接收的信息分别传进来,前者用于保存后者的信息 FileReceive(File rfile, Socket socket) { this.rfile = rfile; this.socket = socket; } public void run() { // 判断用户是否保存文件(原注释) // 没选择文件,自然为null,为null就给你一点提示,小样,别忽悠我! if (rfile == null) { txt.append("没有保存! " + "\n "); return; } else { // 保存文件后,则向发送方 发送同意(true) try { // 输出流 dout = new DataOutputStream(socket.getOutputStream()); dout.writeBoolean(true); } catch (IOException e) { e.printStackTrace(); } } txt.append("开始接收... " + "\n "); try { // 文件输出流 ,用来写文件的 FileOutputStream fout = new FileOutputStream(rfile); // 包装一下 BufferedOutputStream bout = new BufferedOutputStream (fout); // 获取套接字流内容 DataInputStream bin = new DataInputStream (socket.getInputStream()); byte[] buf = new byte[2048]; int num = bin.read(buf); // 将内容读取到一个byte数组中 while (num != -1) { bout.write(buf, 0, num); // 使劲写 bout.flush(); // 清缓存,没有这句话,可能会死锁 num = bin.read(buf); } // 用的比较凌乱,搞这么多东东干嘛fout根本就没用到 // 讲卫生,比如说,饭后,你会找朋友要张餐巾纸吧! bout.close(); bin.close(); txt.append("接收完成: " + rfile.toString() + "\n "); } catch (Exception e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 猪程序 */ public static void main(String[] args) { new Receive(); }}
[解决办法]
- Java code
package lib;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.Socket;public class Send implements ActionListener { JFileChooser jc; JFrame f; Socket socket; DataInputStream din; DataOutputStream dout; JButton b1; JButton b2; TextArea txt; public Send() { // 界面布局 // 这些都不用解释了吧 jc = new JFileChooser(); f = new JFrame("文件发送 "); f.setLocation(100, 100); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); b1 = new JButton("发送 ", new ImageIcon("go.gif ")); b2 = new JButton("取消 ", new ImageIcon("2.gif ")); txt = new TextArea(10, 40); JScrollPane scrollpane = new JScrollPane(txt); JPanel jp = new JPanel(); jp.add(b1); jp.add(b2); Container c = f.getContentPane(); c.add(jp, BorderLayout.NORTH); c.add(scrollpane,BorderLayout.CENTER); f.setSize(400, 150); f.setVisible(true); b1.addActionListener(this); b2.addActionListener(this); } public void actionPerformed (ActionEvent e) { if (e.getSource() == b1) { jc.showOpenDialog(f);// 弹出文件选择对话框 SendFile send = new SendFile(jc.getSelectedFile()); send.start();// 启动新的线程传递文件 } else if (e.getSource() == b2) { System.out.println("用户取消发送! " + "\n "); System.exit(0); } } class SendFile extends Thread { File file;// 用户选择的文件 public SendFile(File file) { this.file = file; // 初始化socket及其相关的输入输出流 try { // "localhost"是你发送消息的目的地址,一般填写IP,而localhost表示本机 // 信息传递至本机4321端口 socket = new Socket ("localhost ", 4321); din = new DataInputStream (socket.getInputStream()); dout = new DataOutputStream (socket.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } } public void run() { try { // 一个比喻:很大的一个游泳馆,里面若干个池和一个铺设好的全自动化控制的水流管道系统。 // 流其实很形象,IP表示游泳池,端口(port)表示接收的孔,这边你只要设定好水要 // 流到哪个游泳池,从哪个口里进去,然后你将水灌进去就行了(就形成了一个水流 // sockec.getOutputStream)。然后,“那个自动化的水管调配系统”,会将水送到目的地。 // 目的地要从水流面获取水,就从源头(即水流socket.getInputStream)里面拿水, // byte[] buf = new byte[2048]; 它们总是一个水分子一个水分子地拿! // 而过滤流BufferedReader的目的是,我一次性将所有的水分子都拿出来,暂时放到一个盆 // 子里面(这样你可以方便的进行编码或者其他格式化动作,你也可以逐行读取)。 // 这里解释一下死锁: // 文件名可比喻成一小口水,如果你不调用dout.flush()方法,那么系统懒得帮你送水,因为 // 它太少了;而这时候,发送的这边就想,水我已经是送出去了,就等对方反应了;而接收方一直 // 接收不到水,就纳闷了,水怎么还不送来呢?等.... // dout.flush()的作用是强制刷新流,将水流推过去(你告诉系统,"兄弟,这水可不便宜,是* // **性感AV女优的口水,N多人等着呢!赶紧送吧!") // 灌入文件名 dout.writeUTF(file.getName()); // 把文件名发送到接收方 txt.append("开始发送文件: " + file.getName() + "\n "); boolean isAccepted = din.readBoolean(); // 判断接收方是否同意接收 if (isAccepted) { // 如果同意接收则开始发送文件 txt.append("开始发送... " + "\n "); BufferedInputStream fin = new BufferedInputStream(new // 下面都差不多 FileInputStream(file)); byte[] buf = new byte[2048]; int num = fin.read(buf); while (num != -1) { dout.write(buf, 0, num); dout.flush(); num = fin.read(buf); } fin.close(); txt.append("发送完成: " + file.toString() + "\n "); } } catch (IOException e) { e.printStackTrace(); } finally { try { din.close(); dout.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new Send(); }}