java - 通过 Java 套接字发送多个文件

标签 java android file sockets

我知道为什么会出现问题,但不知道如何解决。我通过发送以下内容将多个文件从发送者发送到接收者:

  1. 文件数量
  2. 文件名
  3. 文件大小
  4. 文件数据(以字节为单位)

我正在使用 readInt()readUTF()readLong() 发送列表中的前 3 个。 对于文件数据,我通过实现以下代码来使用缓冲区:

float bytesRead = 0;
int count;
byte[] buffer = new byte[8192];
while ((count = in.read(buffer)) > 0) {
      out.write(buffer, 0, count);
      bytesRead += count;
      Float[] progressData = {(bytesRead / 1000), (float) (size / 1000), (float) i, (float) totalFileCount};
      publishProgress(progressData);
}

我正在使用 for 循环来发送文件的所有详细信息。

这里的问题是,在读取第一个文件的数据后,接收器读取下一个文件的名称也作为第一个文件的数据。我需要以某种方式让它在达到第一个文件的大小后停止读取。但我无法实现这一点。任何帮助表示赞赏。

这是接收和发送数据的代码:

正在发送

try {
        Socket client = new Socket();
        client.bind(null);
        client.connect(new InetSocketAddress(groupOwnerAddress, 8888));

        if(client.isConnected())
        {
            ((FileTransferActivity) context).setStatus("Connected to Device");
            DataOutputStream out = new DataOutputStream(client.getOutputStream());

            out.writeInt(encryptedFiles.size()); // send number of files

            for(int i = 0; i < encryptedFiles.size(); i++)
            {

                long length = encryptedFiles.get(i).length();
                if (length > Integer.MAX_VALUE) {
                    System.out.println("File is too large.");
                } else {
                    out.writeUTF(encryptedFiles.get(i).getName()); //send file name
                    out.writeLong(length); // send file length

                    DataInputStream in = new DataInputStream(new FileInputStream(encryptedFiles.get(i)));

                    float bytesRead = 0;
                    int count;
                    byte[] buffer = new byte[8192];
                    while ((count = in.read(buffer)) > 0) {
                        out.write(buffer, 0, count);
                        bytesRead += count;
                        Float[] progressData = {(bytesRead / 1000), (float) (length / 1000), (float) i, (float) encryptedFiles.size()};
                        publishProgress(progressData);

                    }
                }
            }
        }

        client.close();

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

接收

File directory = new File(directoryPath);
        if(!directory.exists())
            directory.mkdirs();

        String data = null;

            try {
                server = new ServerSocket(8888);
                client = server.accept();
                DataInputStream in = new DataInputStream(client.getInputStream());

                int totalFileCount = in.readInt();

                for(int i = 0; i < totalFileCount; i++)
                {
                    String fileName = in.readUTF();
                    long size = in.readLong();

                    FileOutputStream out = new FileOutputStream(new File(directoryPath + File.separator + fileName));

                    float bytesRead = 0;
                    int count;
                    byte[] buffer = new byte[8192]; // or 4096, or more
                    while ((count = in.read(buffer)) > 0) {
                        out.write(buffer, 0, count);
                        bytesRead += count;
                        Float[] progressData = {(bytesRead / 1000), (float) (size / 1000), (float) i, (float) totalFileCount};
                        publishProgress(progressData);
                    }

                }

            }

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

我在这里省略了 asynctask 的其他函数,因为我认为它们不相关。无论如何,如果您希望我也包含这一点,只需在评论中提及,我将与他们一起更新我的答案

最佳答案

我没有通过 javac 运行它,但它应该可以工作:

int count;
byte[] buffer = new byte[8192];

while (size > 0 && (count = in.read(buffer, 0, Math.min((int) size, buffer.length))) > 0) {
    size -= count;
    out.write(buffer, 0, count);
    // Progress reporting omitted.
}

关于java - 通过 Java 套接字发送多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49599929/

相关文章:

android - 制作一个半透明按钮,这样按钮颜色应该是可见的,背景应该是可见的

android - 通过模拟器发送电话号码

java - 分布式java应用不能用spring吗?

java - CountDownTimer 值永远不会为零

java - 非法访问错误: tried to access class while building Android with ANT

objective-c - 如何使用 objective-c 使文件在 Finder 中不可见

C 编程 - 编写可自行编译的文本文件

C++逐行从文件(包含空格)中读取数据

java - OOP分解和单元测试难题

java - 克隆 JavaFX 节点?