欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

026.5 网络编程 上传图片

程序员文章站 2022-03-26 08:50:30
#######################################客户端 ##################################################################服务端 ......

#######################################
客户端

###uploadpicclient.java
public class uploadpicclient {
    /**
     * @param args
     * @throws ioexception 
     * @throws  
     */
    public static void main(string[] args) throws ioexception {
        system.out.println("上传图片客户端运行......");
        //1,创建socket。
        socket s = new socket("192.168.1.223", 10007);
        
        //2,读取源图片。
        file picfile = new file("tempfile\\1.jpg");
        fileinputstream fis = new fileinputstream(picfile);
        
        //3,目的是socket 输出流。
        outputstream out = s.getoutputstream();
        
        byte[] buf = new byte[1024];
        
        int len = 0;
        while((len=fis.read(buf))!=-1){
            out.write(buf,0,len);
        }
        
        //告诉服务器端图片数据发送完毕,不要等着读了。
        s.shutdownoutput();
        
        //读取上传成功字样。
        inputstream in = s.getinputstream();
        byte[] bufin = new byte[1024];
        int lenin = in.read(bufin);
        system.out.println(new string(bufin,0,lenin));
        
        //关闭。
        fis.close();
        s.close();
    }
}

 

##################################################################
服务端

###uploadpicserver.java
public class uploadpicserver {
    /**
     * @param args
     * @throws ioexception
     */
    public static void main(string[] args) throws ioexception {
        system.out.println("上传图片服务端运行......");
        // 创建server  socket 。
        serversocket ss = new serversocket(10007);

        while (true) {
            // 获取客户端。
            socket s = ss.accept();
            
            //实现多个客户端并发上传,服务器端必须启动做个线程来完成。
            new thread(new uploadpic(s)).start();
        }
    }
}

 

###uploadpic.java
public class uploadpic implements runnable {
    private socket s;
    public uploadpic(socket s) {
        this.s = s;
    }
    @override
    public void run() {
        try {
            string ip = s.getinetaddress().gethostaddress();
            system.out.println(ip + ".....connected");

            // 读取图片数据。
            inputstream in = s.getinputstream();

            // 写图片数据到文件。
            file dir = new file("e:\\uploadpic");
            if (!dir.exists()) {
                dir.mkdir();
            }
            // 为了避免覆盖,通过给重名的文件进行编号。
            int count = 1;
            file picfile = new file(dir, ip + "(" + count + ").jpg");
            while (picfile.exists()) {
                count++;
                picfile = new file(dir, ip + "(" + count + ").jpg");
            }
            fileoutputstream fos = new fileoutputstream(picfile);

            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }

            // 给客户端一个回馈信息。
            outputstream out = s.getoutputstream();
            out.write("上传成功".getbytes());

            // 关闭资源。
            fos.close();
            s.close();
        } catch (ioexception e) {
        }
    }
}