免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1975 | 回复: 0
打印 上一主题 下一主题

通信机服务器端(并发接收客户端图片文件) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-06-17 18:55 |只看该作者 |倒序浏览

//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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP