*我在PC上的c:/名称共享中有一个文件夹,并且在运行客户端和服务器代码后我有4张图片,我在Android模拟器中下载了全部4张图片,但只有第一个图片正确下载,其他3张是垃圾
这是我的代码
服务器端

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();

    }
}


客户端

Socket sock = new Socket(“10.0.2.2”,5991);
System.out.println(“正在连接......”);
        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();
    }

最佳答案

您的读取循环不正确。您需要限制读取长度,以免溢出到下一个文件:

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

其他的建议:
  • 文件的长度是long,而不是ints。
  • 使用更大的缓冲区(至少8192),并在方法顶部声明一次。您不需要每个文件一个新文件。
  • 不要在循环内刷新。
  • 不要继续创建流。在 socket 的两端使用相同的 socket 。
  • 您应该写的是“bos”,而不是“dos”。实际上,您根本不需要DataOutputStream写入文件。只是BufferedOutputStream和FileOutputStream。
  • 您应该发送一个文件名,一个长度,然后一个文件,然后是下一个文件名,...这样,发件人可以随时停止。这消除了初始计数,也消除了所有“标志”废话。如果让EOFException读取下一个名称,则对等方已关闭连接。
  • 10-07 19:46
    查看更多