📄 multithread.java
字号:
package PC;
import java.io.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.*;
public class MultiThread extends Thread
{
private Socket skt=null;
private OutputStream netOut;
private OutputStream doc;
private InputStream netIn;
private InputStream in;
private File file;
private RandomAccessFile raf;
private OutputStreamWriter osw;
private byte[] buf=new byte[2048];
private int num;
private String ty;
private static String fname="";
private JTextArea content;
private final static String NEW_LINE = System.getProperty("line.separator");
public MultiThread(Socket s,String type,OutputStreamWriter osw,JTextArea content)
{
ty=type;
skt=s;
this.osw=osw;
this.content=content;
}
public void run()
{
try
{
netOut=skt.getOutputStream();
doc=new DataOutputStream(new BufferedOutputStream(netOut));
netIn=skt.getInputStream();
in=new DataInputStream(new BufferedInputStream(netIn));
}
catch(Exception e)
{
}
if(ty.equals("send"))
{
try{
JFileChooser jfc;
jfc=new JFileChooser("c:/");
jfc.showOpenDialog(null);
file=jfc.getSelectedFile();
fname=file.getName();
String s1="Other size sent you a file: "+fname;
osw.write(s1+ '\n',0,s1.length()+1);
osw.flush();
FileInputStream fos=new FileInputStream(file);
buf=new byte[2048];
num=fos.read(buf);
while(num!=(-1))
{
doc.write(buf,0,num);
doc.flush();
num=fos.read(buf);
}
fos.close();
doc.close();
}
catch(Exception ex)
{
}
}
else
{
try{
JFileChooser jfc;
jfc=new JFileChooser("c:/");
jfc.showOpenDialog(null);
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fname=jfc.getSelectedFile().getName();
file=new File(fname);
file.createNewFile();
raf=new RandomAccessFile(file,"rw");
buf=new byte[2048];
num=in.read(buf);
while(num!=(-1))
{
raf.write(buf,0,num);
raf.skipBytes(num);
num=in.read(buf);
}
content.append(NEW_LINE+"SYSTEM: File have recived successfully.");
String s0="File sent successfully.";
osw.write(s0+ '\n',0,s0.length()+1);
osw.flush();
in.close();
raf.close();
}
catch(Exception ex)
{
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -