请问怎样能实现图文混排的发送,就像QQ那样,先从本机里面找出一张图片,放在聊天框里,然后输入文字,按发送,一起发给对方,对方也能收到图片和文字
请各位高手教教我,怎样将图片和文字的接受端,能同时接收并显示,实现图文混排
图片接收的服务器代码
public void run() {
ServerSocket ss=null;
Socket s=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
FileInputStream fis=null;// 这个函数的定义和参数绝对不能修改
SimpleAttributeSet textStyle = new SimpleAttributeSet();
String str = null;
AttributeSet attrSet = null;
try {
ss=new ServerSocket(8888);
while (true) {
s = ss.accept();
ois = new ObjectInputStream(s.getInputStream());
File a=(File)ois.readObject();
fos = new FileOutputStream(new File(baocunlj.getText()));
fis=new FileInputStream(a);
while(fis.available()>0){
fos.write(fis.read());
}
fos.close();
textPane.insertIcon(new ImageIcon(baocunlj.getText()));
dic.insertString(dic.getLength(), str+"\n", (javax.swing.text.AttributeSet) attrSet);
textPane.setCaretPosition(dic.getLength());
// 这两句是为了能在接收完图片之后能显示图片 acd++;
baocunlj.setText("f:\\"+acd+".jpg");
}
}
catch (Exception e) {
e.printStackTrace();
}
if(fis!=null)try{fis.close();}catch (IOException e) {}
if(s!=null)try {s.close();} catch (IOException e) {}
if(ss!=null)try {ss.close();} catch (IOException e) {}
}
文字接收端的代码如下 public void run() {
try{
byte buf[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket=new MulticastSocket(5555);
while(true){
socket.receive(packet);
String s=new String(buf,0,packet.getLength(),"GBK");
textPane.setText(s+"\n");
packet=new DatagramPacket(buf,buf.length);
}
}catch(Exception e){}
}
请各位高手教教我,怎样将图片和文字的接受端,能同时接收并显示,实现图文混排
图片接收的服务器代码
public void run() {
ServerSocket ss=null;
Socket s=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
FileInputStream fis=null;// 这个函数的定义和参数绝对不能修改
SimpleAttributeSet textStyle = new SimpleAttributeSet();
String str = null;
AttributeSet attrSet = null;
try {
ss=new ServerSocket(8888);
while (true) {
s = ss.accept();
ois = new ObjectInputStream(s.getInputStream());
File a=(File)ois.readObject();
fos = new FileOutputStream(new File(baocunlj.getText()));
fis=new FileInputStream(a);
while(fis.available()>0){
fos.write(fis.read());
}
fos.close();
textPane.insertIcon(new ImageIcon(baocunlj.getText()));
dic.insertString(dic.getLength(), str+"\n", (javax.swing.text.AttributeSet) attrSet);
textPane.setCaretPosition(dic.getLength());
// 这两句是为了能在接收完图片之后能显示图片 acd++;
baocunlj.setText("f:\\"+acd+".jpg");
}
}
catch (Exception e) {
e.printStackTrace();
}
if(fis!=null)try{fis.close();}catch (IOException e) {}
if(s!=null)try {s.close();} catch (IOException e) {}
if(ss!=null)try {ss.close();} catch (IOException e) {}
}
文字接收端的代码如下 public void run() {
try{
byte buf[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket=new MulticastSocket(5555);
while(true){
socket.receive(packet);
String s=new String(buf,0,packet.getLength(),"GBK");
textPane.setText(s+"\n");
packet=new DatagramPacket(buf,buf.length);
}
}catch(Exception e){}
}
解决方案 »
- myeclipse8.0 装find bugs
- 帮忙看看,怎么会出错呢?
- 哪位大侠能帮助我理解一下动态绑定的概念
- 关于线程中一小问题
- 类名.class??
- 小弟初学java,请教关于class文件运行时出现的问题
- 继承ApplicationWindow的类怎么让窗口到一定大下以后就不能在变小了,但可以拉大!
- 帮忙看一下这一小段代码错在哪里,谢谢
- 如何使JScrollPane出现滚动条
- 我不明白微软为什么要跟着java学习虚拟机,难道windows一定要越慢越值钱吗?
- Swing的奇怪问题
- 出来的窗口上的组件显示不出来,但是用鼠标改变一下窗口的大小后组件就出来了,我是要刷新父窗口吗,还是什么,求解
else if(接收的东西是字符串),则转换为文字的发送和接收的通道,能用这样的方法跟我解释一下吗,
本人,实力水平不高,请各位高手大侠教一教
所以正规的通讯协议,在发送正文前,都会发送“消息头”。消息头中至少会包含:正文类型、长度;如果你学习过HTTP,那么它的消息头信息量海了去了。
具体可以自行设计,比如用 byte[1]作为正文类型,然后 byte[4]作为长度。
代码
发送端
public void sendata(){
try{
String msg=textPane_1.getText();
if(msg.equals(""))
return;
textPane.setText("");
String s="127.0.0.1";
InetAddress MyIP=InetAddress.getLocalHost();
String MI=MyIP.getHostAddress();
InetAddress YourIP=InetAddress.getByName(s);
String sj=new String(""+time.get(GregorianCalendar.YEAR)+
"-"+time.get(GregorianCalendar.MONTH)+
"-"+time.get(GregorianCalendar.DATE)+
"-"+time.get(GregorianCalendar.HOUR)+
":"+time.get(GregorianCalendar.MINUTE)+
":"+time.get(GregorianCalendar.SECOND));
msg="["+MI+"]"+sj+"\n"+msg;
int len=msg.length();
//byte mess[]=new byte[len];
//msg.getBytes(0,len,mess,0);
//msg=new String(packet.getData(),0,packet.getLength(),"GBK");
DatagramPacket packet=new DatagramPacket(msg.getBytes(),msg.getBytes().length,YourIP,5555);
msg=new String(packet.getData(),0,packet.getLength(),"GBK");
socket.send(packet);
Messages message;
}
catch(Exception e1){
}
}接收
class ThreadForwenzi extends Thread {
public ThreadForwenzi() {
// 构造函数,可以用来传递你所需的某些参数,比如窗体对象,然后将其保存至成员变量中
}
public void run() {
AttributeSet attrSet1 = null;
StringBuffer sb = new StringBuffer();
try{
byte buf[]=new byte[1024];
DatagramPacket packet=new DatagramPacket(buf,buf.length);
socket=new MulticastSocket(5555);
while(true){
socket.receive(packet);
String s=new String(buf,0,packet.getLength(),"GBK");
sb.append(s+"\n");
textPane.setText(sb.toString());
packet=new DatagramPacket(buf,buf.length);
}
}catch(Exception e){}
textPane.setText(sb.toString()+"\n");
sb.append(textPane.getText().toString()+"\n");
}
}
new ThreadForwenzi().start();
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Server extends JFrame { ServerSocket ser;
Socket con;
ObjectOutputStream salida;
ObjectInputStream entrada;
public Server() { } void ejecutar(){ try{
ser = new ServerSocket( 5700,1 );
con = ser.accept();
salida = new ObjectOutputStream(con.getOutputStream());
salida.flush();
entrada = new ObjectInputStream(con.getInputStream());
procesar();
}
catch(IOException e){}
} void procesar(){ while(true){
try{ JFileChooser fc=new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int r = fc.showOpenDialog(this);
File f = fc.getSelectedFile(); salida.writeObject( new ImageIcon(""+f) );
}catch(IOException e){}
} } public static void main(String args[]) {
Server srv = new Server();
srv.ejecutar();
}}Client:import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;public class Cliente extends JFrame { JTextField tf;
Lienzo lienzo;
Socket con;
ObjectOutputStream salida;
ObjectInputStream entrada; public Cliente(){ super("Cliente");
Container c = getContentPane(); tf = new JTextField(10);
lienzo=new Lienzo(); c.add(lienzo);
pack();
setSize(100,100);
setVisible(true);
} void ejecutar(){ try{ con = new Socket("127.0.0.1",5700);
salida = new ObjectOutputStream(con.getOutputStream());
salida.flush();
entrada = new ObjectInputStream(con.getInputStream()); procesar(); }
catch(IOException e){} } void procesar() throws IOException { try{ while(true){
ImageIcon img = (ImageIcon) entrada.readObject();
escribir(img);
}
}catch(ClassNotFoundException e){} } void escribir(final ImageIcon img){ SwingUtilities.invokeLater(
new Runnable(){
public void run(){
lienzo.pinta(img);
} }
); } public static void main( String args[] ){
Cliente cl = new Cliente();
cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cl.ejecutar();
} class Lienzo extends JPanel{ ImageIcon img=null ; public Lienzo(){ } public void pinta(ImageIcon img){
this.img=img;
repaint();
} public void paintComponent(Graphics g){ super.paintComponent(g); if(img!=null)
img.paintIcon(this,g,10,10); }
}}