⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendfilethread.java

📁 聊天工具
💻 JAVA
字号:
package client.thread;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.ProgressMonitorInputStream;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

import utils.StaticUtils;
import client.chat.chatpane.ChatFrame;
/**
 * 发送文件线程
 * @author 洪景泉
 *
 */
public class sendFileThread extends Thread{
	private InputStream is=null;
	private OutputStream os=null;
	private ChatFrame chatFrame=null;
	private String IP=null;
	private int port=0;
	
	public sendFileThread(ChatFrame chatFrame,String IP,String port) {
		this.chatFrame=chatFrame;
		this.IP=IP;
		this.port=Integer.parseInt(port);
	}
	@Override
	public void run() {
		
		Socket sock=null;
		
			try {
				sock = new Socket(IP,port);
				is=sock.getInputStream();
				os=sock.getOutputStream();
				
			} catch (UnknownHostException e1) {
				System.out.println("1");
				//服务器IP地址错误或者端口号错误
				JOptionPane.showMessageDialog(null, "请查看服务器IP跟端口号有没有设置正确", "错误提示",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			} catch (IOException e1) {
				System.out.println("2");
				//服务器没有开启
				JOptionPane.showMessageDialog(null, "连接服务器超时", "错误提示",JOptionPane.ERROR_MESSAGE);
				e1.printStackTrace();
				return;
			}
			try {
				 File selectFile=chatFrame.getLPane().getCe().getSelectFile();
				 byte[] buf = new byte[1024]; 
				 String fileName=selectFile.getName();
				 ProgressMonitorInputStream pmis=new  ProgressMonitorInputStream(chatFrame,fileName+"发送进度",new FileInputStream(selectFile));
				
				 DataInputStream fis = new DataInputStream(new BufferedInputStream(pmis));
				 DataOutputStream ps = new DataOutputStream(os); 
				 while (true) {
					int read = 0; 
					if (fis != null) {
						read = fis.read(buf); 
					}
					if (read == -1) {
						break; 
					}
					ps.write(buf, 0, read); 
				}
				ps.flush(); 
				fis.close(); 
				sock.close(); 
				System.out.println("文件传输完成");
				
				JTextPane showText = chatFrame.getLPane().getShowText();
				StyledDocument showDoc = showText.getStyledDocument();
				showText.setEditable(true);
				try {
					SimpleDateFormat sdf = new SimpleDateFormat ("HH:mm:ss");
					sdf = new SimpleDateFormat ("HH:mm:ss");
					String currentuser=(String)StaticUtils.currentUser.get("userName");
					showDoc.insertString(showDoc.getLength(),currentuser+" "+sdf.format(new Date())+"\n",null);
					showDoc.insertString(showDoc.getLength(), "文件传输完成\n", null);
					
				} catch (BadLocationException e) {
					// TODO 自动生成 catch 块
					e.printStackTrace();
				}
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -