简体   繁体   中英

I want to download all files from PC's folder from my android for this m doing java sockets but achieved 1st file correctly others are garbage

*i have have a folder in my pc in c:/ name share and in that i have 4 pictures after running my client and server code i got all 4 picture downloaded in my android emulator but only first image is correctly download other 3 are garbage here is my code SERVER SIDE

public class Multiplefilessending 
{  
    public static void main(String[] args) throws IOException,EOFException
    {  
        FileOutputStream fos;  
        BufferedOutputStream bos;  
        OutputStream output;    
        int len;  
        int smblen;   
        InputStream in;  
        boolean flag=true;  
        DataInputStream clientData;  
        BufferedInputStream clientBuff;  
        System.out.println("Waiting for Connection");
        ServerSocket serverSocket = new ServerSocket(5991);  
        Socket clientSocket = null;  
        clientSocket = serverSocket.accept();

        ////////////////////////
        File myFile = new File("C:/share");  
        File[] Files = myFile.listFiles();              

        OutputStream os = clientSocket.getOutputStream();    
        DataOutputStream dos = new DataOutputStream(os);   

        dos.writeInt(Files.length);  

        for (int count=0;count<Files.length;count ++)
        {  
              dos.writeUTF(Files[count].getName());  

        }  

        for (int count=0;count<Files.length;count ++)
        {  

              int filesize = (int) Files[count].length();  
              dos.writeInt(filesize);  
        }  

        for (int count=0;count<Files.length;count ++)
        {    
        int filesize = (int) Files[count].length();  
        byte [] buffer = new byte [filesize];  

        //FileInputStream fis = new FileInputStream(myFile);    
        FileInputStream fis = new FileInputStream(Files[count].toString());    
        BufferedInputStream bis = new BufferedInputStream(fis);    

        //Sending file name and file size to the server    
        bis.read(buffer, 0, buffer.length); //This line is important  

        dos.write(buffer, 0, buffer.length);     
        dos.flush();   
        //dos.close();  
        }    

        if (flag==false){  
            clientSocket = serverSocket.accept();  
            flag = true;  
         }
        //Closing socket    
        //dos.close();  
        clientSocket.close();

    }   
}

And CLIENT SIDE

Socket sock = new Socket("10.0.2.2", 5991);
System.out.println("Connecting.........");

        FileOutputStream fos;  
        BufferedOutputStream bos;  
        OutputStream output;
        DataOutputStream dos;
        int len;  
        int smblen;   
        InputStream in;  
        boolean flag=true;  
        DataInputStream clientData;  
        BufferedInputStream clientBuff;

        while (true)
        {  
            //while(true && flag==true){  
            while(flag==true)
            {    
                in = sock.getInputStream(); //used    
                clientData = new DataInputStream(in); //use   
                clientBuff = new BufferedInputStream(in); //use    
                int fileSize = clientData.read();  
                ArrayList<File>files=new ArrayList<File>(fileSize);                         ArrayList<Integer>sizes = new ArrayList<Integer>(fileSize); //store file size from client  
                //Start to accept those filename from server  
                for (int count=0;count < fileSize;count ++){  
                    File ff=new File(clientData.readUTF());  
                    files.add(ff);  
                }  
                for (int count=0;count < fileSize;count ++){  
                    sizes.add(clientData.readInt());  
                }  
                for (int count =0;count < fileSize ;count ++)
                {    
                    if (fileSize - count == 1)
                    {  
                        flag =false;  
                    }  
                    len=sizes.get(count);  
                    //System.out.println("File Size ="+len);  
                    output = new FileOutputStream("/mnt/sdcard/" + files.get(count));  
                    dos=new DataOutputStream(output);  
                    bos=new BufferedOutputStream(output);  
                    byte[] buffer = new byte[1024];    
                    bos.write(buffer, 0, buffer.length); //This line is important  
                    while (len > 0 && (smblen = clientData.read(buffer)) > 0)
                    {   
                        dos.write(buffer, 0, smblen);   
                        len = len - smblen;  
                        dos.flush();  
                    }    
                    dos.close();  //It should close to avoid continue deploy by resource under view  
                }     
            }  

            if (flag==false)
             {  
                sock = new Socket("10.0.2.2", 5991);  
                 flag = true;  
              }
        }           } 

    catch (UnknownHostException e) 
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }

Your read loop is incorrect. You need to constrain the read length so you don't over-run into the next file:

while (len > 0 && (smblen = clientData,read(buffer, 0, len > buffer.length ? buffer.length : (int)len)) > 0)
{
    bos.write(buffer, 0, smblen);
    len -= smblen;
}

Other comments:

  • File lengths are longs, not ints.
  • Use a bigger buffer, at least 8192, and declare it once at the top of the method. You don't need a new one per file.
  • Don't flush inside the loop.
  • Don't keep recreating the streams. Use the same ones for the life of the socket, at both ends.
  • You should be writing to 'bos', not 'dos'. In fact you don't need the DataOutputStream to write to the file at all. Just the BufferedOutputStream and the FileOutputStream.
  • You should send one filename, one length, then one file, then the next filename, ... That way the sender can stop any time. That gets rid of the initial count, and it also gets rid of all that 'flag' nonsense. If you get EOFException reading the next name, the peer has closed the connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM