A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© liu100chao 中级黑马   /  2015-6-12 19:32  /  430 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
需求:多客户并发上传图片。
*/
import java.io.*;
import java.net.*;

class picture1
{
        public static void main(String[] args) throws Exception
        {
                Socket ss = new Socket("192.168.1.104",10043);

                FileInputStream read = new FileInputStream("C:\\Users\\liuchao\\Desktop\\1.PNG");

                OutputStream write = ss.getOutputStream();

                byte[] data = new byte[1024];

                int len = 0;
               
                while((len=read.read(data))!=-1)
                {
                        write.write(data,0,len);
                }

                ss.shutdownOutput();

                InputStream wri = ss.getInputStream();
               
                byte[] bufin = new byte[1024];
                int shuzi = wri.read(bufin);

                System.out.println(new String(bufin,0,shuzi));

                read.close();
                ss.close();

        }
}

class pic implements Runnable
{
        private Socket s ;
       
        pic(Socket s)
        {
                this.s=s;
        }

        public void run()
        {

                String ip =s.getInetAddress().getHostAddress();
                int count=1;

                try
                {

                        System.out.println(ip);

                        InputStream read = s.getInputStream();

                        File dir = new File("C:\\Users\\liuchao\\Desktop\\1_copy.PNG");

                        File file = new File(dir,ip+"("+count+")"+".PNG");

                        if(file.exists())
                                file = new File(dir,ip+"("+(count++)+")"+".PNG");

                        FileOutputStream write = new FileOutputStream(file);

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

                        OutputStream out=s.getOutputStream();

                        out.write("上传成功".getBytes());

                        write.close();
                        s.close();

                }
                catch (Exception e)
                {
                        System.out.println("上传失败");
                }
        }
}


class picture2
{
        public static void main(String[] args) throws Exception
        {
                ServerSocket ss= new ServerSocket(10043);
               
                while(true)
                {
                        Socket s = ss.accept();

                        new Thread(new pic(s)).start();

                }
        }
}

今天瞧这个代码不知为什么,只要去掉多线程的代码就没问题,已加入多线程,每次运行都显示“上传失败”,请问各位大神们这是怎麽回事??会不会是我电脑的问题???

1 个回复

倒序浏览
只加入线程,运行就不通过!弄了半天,都没弄明白!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马