- 论坛徽章:
- 0
|
//CommServer.java
import com.sun.image.codec.jpeg.*;
import java.io.*;
import javax.imageio.*;
import java.awt.image.*;
import java.net.*;
import java.awt.*;
import javax.swing.*;
class MyFrame extends JFrame
{
private MyPanel panel=null;
public MyFrame()
{
panel=new MyPanel();
add(panel);
}
public void drawScr(BufferedImage bi)
{
panel.draw(bi);
}
private class MyPanel extends JPanel
{
private BufferedImage bi=null;
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(bi,0,0,null);
}
public void draw(BufferedImage bi)
{
this.bi=bi;
this.updateUI();
}
}
}
/**
* 通信机服务器端程序(多线程)
* @author lishoudi
* @version 1.0
*
*/
public class CommServer extends Thread
{
private static ServerSocket ss=null;
private static Socket s=null;
private InputStream is=null;
public MyFrame frame=null;
private static int count =0;
public CommServer()
{
}
/**
* 把字节数组保存为一个文件
* @Author Sean.guo
* @EditTime 2007-8-13 上午11:45:56
*/
public static File getFileFromBytes(byte[] b, String outputFile)
{
BufferedOutputStream stream = null;
File file = null;
try
{
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (stream != null)
{
try
{
stream.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}
}
return file;
}
public void run()
{
frame = new MyFrame();
frame.setSize(500,300);
frame.setTitle("connection "+count);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
is=s.getInputStream();
ByteArrayOutputStream output=new ByteArrayOutputStream();
transform(is,output);//将输入流转换为字节输出流
byte [] buf=output.toByteArray();
System.out.println(buf.length); //34946
System.out.println(buf[0]);
byte [] imageInfo =new byte[buf.length-88];
byte [] showInfo = new byte[buf.length-88];//显示用
System.out.println(imageInfo.length);
System.arraycopy(buf, 88, imageInfo, 0, imageInfo.length);
System.arraycopy(buf, 88, showInfo, 0, showInfo.length);
for(int i=0;i=0)
{
byte[] t = new byte[buffer.length+count];
System.arraycopy(buffer,0,t,0,buffer.length);
System.arraycopy(chunk,0,t,buffer.length,count);
buffer=t;
}
s.close();
}catch(Exception e){}
return buffer;
}
}
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/93928/showart_1967641.html |
|