高手,我搞了一个通信程序,为了实现图文混排,我写出了下面的测试结构:
(接收端)
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.net.Socket;
import java.util.UUID;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
public class MyServer3 extends JFrame{
private static final long serialVersionUID = -6927612627246594189L;
private static MyJTextPane jtp1;
private MyJTextPane jtp2;
private JButton jb;
String MyFilePath = "";
static int MyLover = 0;
public static void main(String[] args) {
new MyServer3();
}
public void start(){
try {
System.out.println("s" + 1);
SocketServer ss=new SocketServer(9999);
System.out.println("s" + 2);
while(true) {
Socket s=ss.nextSocket();//把服务端页DEbug一下好的,大哥.
System.out.println("s" + 3);
String str = ss.read(s);
char[] ch = str.toCharArray();
System.out.println("ch.length===="+ch.length);
String[] buffer = str.split("丗");
String MyObjIP = buffer[0];
String MyObjNum = buffer[1];
MyFilePath = "C:\\"+str+"\\"+MyObjNum+"\\aa.jpg";
ss.write(s, GetMyUserIP.getMyIP());
final ClipboardUtil.MyImgTextContent mydata=(ClipboardUtil.MyImgTextContent)ss.readObject(s);
ClipboardUtil.MyImgTextContent mylocaldata = new ClipboardUtil.MyImgTextContent();
for(String c : mydata){
if(c.startsWith("text")) {
mylocaldata.content.add(c);
} else {
String filePath = "D:/" + UUID.randomUUID() + ".jpg";
String[] sss = c.replaceAll("img-", "").split(",");
byte[] bs = new byte[sss.length];
for(int i = 0; i < bs.length; i++) {
bs[i] = Byte.parseByte(sss[i]);
}
File f = new File(filePath);
f.createNewFile();
FileOutputStream fos = new FileOutputStream(f);
fos.write(bs);
fos.flush();
fos.close();
mylocaldata.content.add("img-" + filePath);
}
}
jtp1.setContent(mylocaldata,"");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public MyServer3(){
super("不改原创");
this.setTitle("MyServer3端点");
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
this.setLayout(new GridLayout(3, 1));
jtp1 = new MyJTextPane(false);
jtp2 = new MyJTextPane(true);
jtp2.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 0)));
jb = new JButton("send");
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jtp1.setContent(jtp2.getContent(),"");
jtp2.setText("");
jtp2.grabFocus();
}
});
this.add(new JScrollPane(jtp1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER));
this.add(new JScrollPane(jtp2, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane. HORIZONTAL_SCROLLBAR_NEVER));
//----------------------------------
JPanel pan = new JPanel();
JButton btn = new JButton("Get Text");
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String content = jtp1.getText();
System.out.println(content);
}
});
pan.add(jb);
pan.add(btn);
this.add(pan);
//----------------------------------
this.setSize(400, 600);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
start();
}
}
(发送端)
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectOutputStream;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
public class MyClient3 extends JFrame{
/**
* 欢迎大家使用这个源码 如有疑问请加qq群:151648295
*
*/
/**
* @param args
*/
private static final long serialVersionUID = -6927612627246594189L;
private MyJTextPane jtp1;
private MyJTextPane jtp2;
private JButton jb;