用户注册



邮箱:

密码:

用户登录


邮箱:

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

发表随想


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

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

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

[java]代码库

package FileTran;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

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

import MyFrame.Myframe;

public class FileClient extends Thread {

	private Socket client = null;
	private ObjectOutputStream out = null;
	private InputStream in = null;
	private Myframe mf;
	private String msg;
	private String ip;
	private String fileload ;
	private JFileChooser jfc;
	private File fi;
	public FileClient(Myframe mf) {
		this.mf = mf;
		this.msg = mf.getPane().getContendjt().getText();
		this.ip = (String) mf.getPane().getIpjt().getSelectedItem();
	}

	public void run() {
		this.jfc=new JFileChooser();
		this.jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		int reval=this.jfc.showOpenDialog(mf);
		if(reval==JFileChooser.APPROVE_OPTION){
			fi=this.jfc.getSelectedFile();
		}else if(reval==1){
			return;
			
		}else{
			JOptionPane.showMessageDialog(mf, "操作有误请稍后再试!!");
			return;
		}
		this.mf.getPane().getContendjt().setText(msg + "发送中.......\n");
		String[] file = fi.toString().split("\\\\", -1);
		byte[] b = new byte[10240];
		try {
			List li = new ArrayList();
			client = new Socket(this.ip, 9500);
			in = new FileInputStream(this.fi);
			out = new ObjectOutputStream(client.getOutputStream());
			int l = 0;
			int len = 0;
			byte[] a = null;
			while ((l = in.read(b)) != -1) {
				a = new byte[1024];
				a = b.clone();
				len = l;
				li.add(a);

			}
				
			li.add(len);
			li.add(file[file.length - 1]);
			
			out.writeObject(li);
			out.flush();
			this.mf.getPane().getContendjt().setText(msg + "文件发送完毕.....\n");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "没有连接到服务器...");
			return;
		} catch (FileNotFoundException f) {
			JOptionPane.showMessageDialog(null, "文件没有找到,请确认...");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			JOptionPane.showMessageDialog(null, "输入有误!!");
			return;
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (client != null) {
				try {
					client.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

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


网友评论    (发表评论)


发表评论:

评论须知:

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


扫码下载

加载中,请稍后...

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

加载中,请稍后...