首页>代码>java swing聊天室程序--带语音>/局域网聊天-带语音/ChatClient/src/FileSender.java
import java.net.*;
import java.io.*;
import java.awt.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
/*
 * 该类实现的是文件的传输功能
 * 默认的端口是9998
 */
public class FileSender extends Thread 
{
    String descNickname;
    String descIP;
    int port=9900;
    String fileName;
    long count=0;
    List chatContentList;
    public FileSender(String nickname,String fileName,List chatContentList) 
    {
        this.descNickname=nickname;
        this.fileName=fileName;
        this.chatContentList=chatContentList;
    }
    public void run()
    {
        try 
        {
        	Socket s=new Socket(InetAddress.getByName(this.descIP),this.port);
        	File file =new File(fileName);
        	FileInputStream fis=new FileInputStream(file);
        	BufferedInputStream bis = new BufferedInputStream(fis);
        	BufferedOutputStream bos=new BufferedOutputStream(s.getOutputStream());
        	byte[] buff = new byte[1024];
        	int i = 0;
        	ClientFrame.jLabel5.setText(file.getName());
        	ClientFrame.bar.setMinimum (0);
        	ClientFrame.bar.setMaximum((int)(file.length()/1024));
            while ((i = bis.read(buff, 0, buff.length)) != -1) 
            {
            	count+=buff.length;
            	ClientFrame.bar.setValue ((int)(count)/1024);
            	bos.write(buff,0,i);
            	bos.flush();
            }
            bis.close();
            s.close();
            JOptionPane.showMessageDialog(null, "发送文件成功!", "提示",JOptionPane.INFORMATION_MESSAGE);
            ClientFrame.bar.setVisible(false);
            ClientFrame.jLabel5.setText("");
            ClientFrame.filelength=0;
        } 
        catch (Exception ex) 
        {
            ex.printStackTrace();
        }
    }
    public void setDescIP(String ip)
    {
        this.descIP=ip;
    }
}
最近下载更多
微信网友_6444139264921600  LV6 2023年4月29日
Sopuding  LV1 2022年7月16日
tomtom113  LV2 2022年6月9日
ewan007  LV29 2022年4月21日
tdcq123  LV14 2022年3月13日
双方各何必呢  LV13 2021年12月20日
曹思辰  LV6 2021年11月21日
阳光正好  LV2 2021年10月29日
1005948011  LV7 2021年6月1日
服气辣  LV2 2021年5月24日
最近浏览更多
1112WHQ  LV7 2023年11月3日
yuchunxing  LV1 2023年9月2日
2017143155  LV12 2023年6月24日
lynn_zhou  LV1 2023年5月30日
微信网友_6444139264921600  LV6 2023年4月29日
生米的  LV10 2023年3月3日
ann000  LV2 2022年12月7日
queueandstack  LV3 2022年12月1日
微信网友_6229090122354688 2022年11月21日
暂无贡献等级
xinxingyuan8 2022年9月29日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友