使用套接字发送多个文件

时间:2015-04-29 13:27:42

标签: java sockets netbeans

我正在尝试使用socket从客户端向服务器发送多个文件 但是当我点击上传按钮时,它只添加一个文件 然后第二次点击添加第二次。这是我的代码:

客户端

  void SendFile (String St, String Na) throws IOException{

                    Socket sock = new Socket("127.0.0.1", 1112);
                    //ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
                    //outStream.writeObject("test message #");
                    //outStream.flush();
                    //FileInputStream Fi =  null;

            try {
                // TODO add your handling code here:
                FileInputStream Fi = new FileInputStream(St);

                byte [] Buffer = new byte[Fi.available()];

                Fi.read(Buffer);
                ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());

                out.writeObject(Buffer);
                out.flush();
                out.close();
                //outStream.writeObject(Buffer);
                write.println("Send:File: " + Na);

                write.flush();





            } catch (FileNotFoundException ex) {
                Logger.getLogger(Add_Book.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Add_Book.class.getName()).log(Level.SEVERE, null, ex);


                }
                catch (Exception ex) 
                {
                    System.out.println("error connecting to server");
                }
             sock.close();
        }
           `

通话方式:

SendFile(CoverPath,CoverName);
SendFile(BookPath,BookName); 

服务器

     public class UploadServer implements Runnable 
    {
        @Override
        public void run() 
        {
            clientOutputStreams = new ArrayList();
            users = new ArrayList();  
try 
            {
                ServerSocket serverSocket = new ServerSocket(1112);

                while (true) 
                {
                Socket clientSocket = serverSocket.accept();
                ObjectOutputStream Oos = new ObjectOutputStream(clientSocket.getOutputStream());

                                is = clientSocket.getInputStream();

                Thread listener = new Thread(new UploadHandler(clientSocket));
                listener.start();
                TextArea.append("Request upload. \n");
                                ss = clientSocket;
                }
            }
            catch (Exception ex)
            {
                TextArea.append("Error making a connection. \n");
            }

        }
}


 public class UploadHandler implements Runnable 
   {
          ObjectInputStream inputS;
          ObjectInputStream input;
       BufferedReader reader;
       ObjectInputStream Ois;
       Socket sock;


       byte[] aByte = new byte[1];
        int bytesRead;

      public UploadHandler(Socket clientSocket) 
       {

            try 
            {
                sock = clientSocket;
                inputS = new ObjectInputStream(sock.getInputStream());
                //input = new ObjectInputStream(sock.getInputStream());

            }
            catch (Exception ex) 
            {
                TextArea.append("Unexpected error... \n");
            }

       }

       @Override
       public void run() 
       {

            try {

                  byte [] Buffer = (byte[]) inputS.readObject();
                  FileOutputStream Fi = new FileOutputStream("Uploads\\"+Name);
                  Fi.write(Buffer);
                  System.out.println("Done");

              } catch (IOException ex) {
                  Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
              } catch (ClassNotFoundException ex) {
                  Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
              }




       }


      }

0 个答案:

没有答案