用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java在线聊天及文件传输系统8

2015-03-24 作者: java源代码大全举报

[java]代码库

package FileTran;

import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import MyFrame.Myframe;

public class FileMsgthread extends Thread {

	private Myframe mf;
	private Socket sock = null;
	private ObjectInputStream in = null;
	private byte[] b = null;
	private OutputStream out = null;
	private String msg;
	private JFileChooser jfc;
	private File fi;
	public FileMsgthread(Socket sock, Myframe mf) {
		this.sock = sock;
		this.mf = mf;
		msg = mf.getPane().getContendjt().getText();
	}

	public void run() {
		try {
			in = new ObjectInputStream(this.sock.getInputStream());
			List li = null;
			List lb = null;

			li = (List) this.in.readObject();

			lb = li.subList(0, li.size() - 2);
			int len = (Integer) li.get(li.size() - 2);
			String file = (String) li.get(li.size() - 1);
			
			int judge=JOptionPane.showConfirmDialog(mf, "正在接受用户 "
					+ this.sock.getInetAddress().getHostAddress()
					+ " 发送文件:\n"+file);
			if(judge==0){
				this.jfc=new JFileChooser();
				this.fi=new File(file);
				this.jfc.setSelectedFile(this.fi);
				this.jfc.showSaveDialog(mf);
				this.fi=this.jfc.getSelectedFile();
				this.out = new FileOutputStream(this.fi);
			}else{
				return;
			}
			this.mf.getPane().getContendjt().setText(msg + "接收中.......\n");
			int i = 0;
			while (i < lb.size()) {
				b = (byte[]) lb.get(i);
				if (i == (lb.size() - 1)) {
					out.write(b, 0, len);
					out.flush();
					break;
				}
				out.write(b);
				out.flush();
				i++;
			}

			this.mf.getPane().getContendjt().setText(msg + "文件接收完毕.....\n");

		} catch (FileNotFoundException f) {
			JOptionPane.showMessageDialog(null, "文件存储有误...");
			return;
		} catch (EOFException eof) {
			JOptionPane.showMessageDialog(null, "无法接收文件...");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			JOptionPane.showMessageDialog(null, "输入有误...");
			return;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			JOptionPane.showMessageDialog(null, "无法找到文件");
			return;
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (sock != null) {
				try {
					sock.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (this.out != null) {
				try {
					this.out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

}
//源代码片段来自云代码http://yuncode.net
			


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...