📄 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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -