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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈妙俊 中级黑马   /  2014-5-6 13:39  /  1653 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
import java.net.*;
class PtcClient {
        public static void main(String[] args) throws Exception{
                //1.建立服务
                Socket socket=new Socket("192.168.1.8",10006);
                FileInputStream filein=new FileInputStream("E:\\360downloads\\wpcache\\abc.jpg");
                //读取客户端已有图片
                OutputStream output=socket.getOutputStream();
                byte[] by1=new byte[1024];
                int len=0;
                while((len=filein.read(by1))!=-1){
                        output.write(by1,0,len);       
                }
                //告诉服务端数据已经写完
                socket.shutdownOutput();
                //读取服务端信息
                InputStream input=socket.getInputStream();
                byte[] by=new byte[1024];
                int line=input.read(by);
                System.out.println(new String(by,0,line));

                socket.close();
                filein.close();
        }
}
class PtcThread implements Runnable{
        private Socket serversocket;
        PtcThread(Socket serversocket){
                this.serversocket=serversocket;
        }
        public void  run(){
                int count=1;
                String ip=serversocket.getInetAddress().getHostAddress();
                try{       

                        System.out.println(ip+"connect");
                       
                        InputStream input=serversocket.getInputStream();
               
                        File file=new File(ip+"("+(count)+")"+".jpg");
                       
                        while(file.exists()){
                                file=new File(ip+"("+(count++)+")"+".jpg");
                        }

                        FileOutputStream output=new FileOutputStream("F:\\"+file);       
                        byte[] by=new byte[1024];
                        int len=0;
                        while((len=input.read(by))!=-1){
                                output.write(by,0,len);
                        }
                        OutputStream out=serversocket.getOutputStream();
                        out.write("你好,上传成功".getBytes());
                        serversocket.close();
                        output.close();

                }
                catch(Exception e){
                        throw new RuntimeException(ip+"上传失败");
                }
        }
}

class serversocket{
        public static void main(String[] args)throws Exception{
                ServerSocket server=new ServerSocket(10006);
                while(true){
                        Socket serversocket=server.accept();
                        new Thread(new PtcThread(serversocket)).start();


                }

        }

}
代码运行编译都没有错,可是我想并发上传多张图片,但是只能上传一张,求大神帮忙看看哪里的问题

6 个回复

倒序浏览
执行了你的代码,发现无论客户端执行多少次,也只复制一张图片
部分代码需要更改
run()方法中的代码改为
                        File

file=new File("F:\\"+ip+"("+

(count)+")"+".jpg");
                        
                        

while(file.exists()){
                              

  file=new File

("F:\\"+ip+"("+(count++)

+")"+".jpg");
                        }

                        

FileOutputStream output=new

FileOutputStream(file);


这样就能上传多张了。和你原来的代码相比,就是F:\\这个盘符的改变。
在你的while(file.exists),file只比较文件名,exists方法是验证绝对路径
existspublic boolean exists()测试此抽象路径名表示的文件或目录是否存在。
回复 使用道具 举报
服务端 弄个循环试试。
回复 使用道具 举报
我也写了一个,能达到循环上传文件的目的(外加一些文件判断):

//循环上传图片
class PicTread implements Runnable
{
        private Socket s;
        PicThread(Socket s)
        {
                this.s=s;
        }

        public void run()
        {
                //图片计数
                int count=1;

                //获得客户端ip名称
                String ip=s.getInetAddress().getHostAddress();
                try
                {
                        System.out.println(ip+"...connected");
                       
                        //获得Socket输入流
                        InputStream in=s.getInputStream();

                        File file=new File(ip+"("+count+")"+".jpg");
                       
                        //判断文件是否存在,存在就不覆盖
                        while(file.exists())
                                file=new File(ip+"("+count+")"+".jpg");

                        //定义输出流
                        FileOutputStream fos=new FileOutputStream("sever.bmp");

                        byte[] buf=new byte[1024];

                        int len=0;

                        //读取数据
                        while((len=in.read(buf))!=-1)
                        {
                                fos.write(buf,0,len);
                        }

                        //获得Socket输出流
                        OutputStream out=s.getOutputStream();

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

                        //关流
                        fos.close();

                        s.close();
                }
                catch (Exception e)
                {
                        throw new RuntimeException(ip+"上传失败!");
                }
        }
}

//客户端
class PicClient
{
        public static void main(String[] args) throws Exception
        {
                //不存在文件时的提示
                if(args.length!=1)
                {
                        System.out.println("请选择一个jpg格式的图片");
                        return;
                }

                File file=new File(args[0]);
                if(!(file.exists()&&file.isfile()))
                {
                        System.out.println("该文件有问题,要么不存在,要么不是文件");
                        return;
                }
               
                //图片必须为jpg格式的
                if(!file.getName().endsWith(".jpg"))
                {
                        System.out.println("图片格式错误,请重新选择");
                        return;
                }

                if(file.length()>1024*1024*5)
                {
                        System.out.println("文件过大,没安好心!");
                        return;
                }       
               
                //定义Socket信息
                Socket s=new Socket("192.168.0.2",10007);

                //读取原始图片
                FileInputStream fis=new FileInputStream("c:\\1.bmp");

                //获取Socket输出流
                OutputStream out=s.getOutputStream();

                byte[] buf=new byte[1024];

                int len=0;

                while((len=fis.read(buf))!=-1)
                {
                        out.write(buf,0,len);
                }
               
                //读取Socket输入流
                InputStream in=s.getInputStream();

                byte[] bufIn=new byte[1024];

                int num=in.read(bufIn);

                System.out.println(new String(bufIn,0,num));

                //关流
                fis.close();
                s.close();
        }
}

//服务端
class PicSever
{
        public static void main(String[] args) throws Exception
        {
                ServerSocket ss=new ServerSocket(10007);
               
                while(true)
                {
                        Socket s=ss.accept();
                       
                        //开启线程
                        new Thread(new PicThread(s)).start();
                }
        }
}

回复 使用道具 举报
来男. 发表于 2014-5-6 15:52
我也写了一个,能达到循环上传文件的目的(外加一些文件判断):

//循环上传图片

代码执行过吗,好像计数器count,在代码内没有自增过
回复 使用道具 举报
heima_xyu 发表于 2014-5-6 16:06
代码执行过吗,好像计数器count,在代码内没有自增过

自己少打了个“++”....图改成count++,,同学好细心啊!

77.jpg (19.85 KB, 下载次数: 30)

77.jpg
回复 使用道具 举报
来男. 发表于 2014-5-6 16:20
自己少打了个“++”....图改成count++,,同学好细心啊!

是count变(count++),(⊙o⊙)…
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马