zoukankan      html  css  js  c++  java
  • java下socket传图片

    package cn.stat.p4.ipdemo;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    public class imageserver {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            ServerSocket ss=new ServerSocket(10000);
            
            Socket s=ss.accept();
            
            InputStream ins=s.getInputStream();
            
            byte[]  buf=new byte[1024];
            
            int len=0;
            
            File file=new File("2.gif");
            if(!file.exists())
                file.createNewFile();
            
            FileOutputStream fout=new FileOutputStream(file);
            
            while((len=ins.read(buf))!=-1)
            {
                fout.write(buf,0,len);
            }
            fout.flush();
            
            OutputStream outs=s.getOutputStream();
            outs.write("完成".getBytes());
            fout.close();
            outs.close();
            ins.close();
            s.close();
            ss.close();
    
        }
    
    }
    package cn.stat.p4.ipdemo;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class imageclent {
    
        /**
         * @param args
         * @throws IOException 
         * @throws UnknownHostException 
         */
        public static void main(String[] args) throws UnknownHostException, IOException {
            Socket s=new Socket("192.168.1.103",10000);
            
            FileInputStream files=new FileInputStream("1.gif");
            
            OutputStream outs=s.getOutputStream();
            
            byte[] buf=new byte[1024];
            
            int len=0;
            
            while((len=files.read(buf))!=-1)
            {
                outs.write(buf,0,len);
            }
            
            s.shutdownOutput();
            
            InputStream ins=s.getInputStream();
            
            byte[] ibuf=new byte[1024];
            
            int ilen=ins.read(ibuf);
            
            String text=new String(ibuf,0,ilen);
            System.out.println(text);
            
            ins.close();
            outs.close();
            files.close();
            s.close();
    
        }
    
    }
  • 相关阅读:
    变量
    匿名函数与内建函数
    Datanode denied communication with namenode because hostname cannot be resol
    0003.搭建Hadoop的环境
    0001.大数据课程概述与大数据背景知识
    享学首创年薪阿里60W+/对标P7岗移动 架构师成长路线V3.0 ---- Android移动互联网架构开发
    FFmpeg和WebRTC的区别
    查看每个文件下内存占用大小
    linux 删除中文乱码
    FFmpeg 命令大全
  • 原文地址:https://www.cnblogs.com/zywf/p/4792921.html
Copyright © 2011-2022 走看看