【问题标题】:How to download an image in socket connection and remove the unwanted bytes如何在套接字连接中下载图像并删除不需要的字节
【发布时间】:2017-04-19 09:50:28
【问题描述】:

我需要通过套接字连接下载图像。目前我有一个字节数组,其中包含一个 .jpg 文件以及一些其他不需要的字节(ffd8)。我需要从我的字节数组中删除这些不需要的字节。我尝试在不删除字节的情况下下载文件,然后我得到了格式损坏的图像。所以我想知道如何从我的字节数组中删除不需要的字节。

在我的代码中:

  while (!status) {
                        byte[] buffer = new byte[1024]; // or 4096, or more
                        int bytesRead = input.read(buffer, 0, buffer.length);
                        int availableBytes = input.available();

                        str2 = str2.append(bytesRead);
                        ImageDataArray = concat(ImageDataArray, buffer);
                        countPackets = countPackets + buffer.length;
                        if (input.available() != 0) {
                            System.out.println("String:" + str1.toString());
                        } else break;
                    }
                    System.out.println("--------------------out of while ------------------");
                    File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "/SteelmanPro/1020/images/image" +filename1);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                }
                write(ImageDataArray, Environment.getExternalStorageDirectory().getAbsoluteFile() + "/SteelmanPro/1020/images/image" +filename1);

concat() 方法:

 public  byte[] concat(byte[]...arrays)
{
    // Determine the length of the result array
    int totalLength = 0;
    for (int i = 0; i < arrays.length; i++)
    {
        totalLength += arrays[i].length;
    }

    // create the result array
    byte[] result = new byte[totalLength];

    // copy the source arrays into the result array
    int currentIndex = 0;
    for (int i = 0; i < arrays.length; i++)
    {
        System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
        currentIndex += arrays[i].length;
    }

    return result;
}

写法:

 void write(byte[] aInput, String aOutputFileName) {

    Log.e("dfd", "Writing binary file...");
    try {
        OutputStream output = null;
        try {
            output = new BufferedOutputStream(new FileOutputStream(aOutputFileName));
            output.write(aInput);
        } finally {
            if (output != null)
                output.close();
        }
    } catch (FileNotFoundException ex) {
        Log.e("dfd", "File not found.");
    } catch (IOException ex) {
        Log.e("dfd", "" + ex);
    }
}

【问题讨论】:

  • 删除了十几个或更多无意义的空行。不要浪费垂直空间,尤其是在网络上。

标签: java android sockets java-io


【解决方案1】:

其中包含一个 .jpg 文件以及一些其他不需要的字节 (ffd8) 我尝试在不删除字节的情况下下载文件,然后我得到格式损坏的图像

由于删除 FFD8 导致您最终损坏并且您仍然尝试这样做,需要发生什么才能让您不再认为“不需要的”字节实际上是“非常想要的”? (双关语)

事实上FF D8 FF 是JPEG 的magic number 并且是JPEG 文件的一部分。

https://en.wikipedia.org/wiki/JPEG

【讨论】:

  • 实际上我没有删除它。我知道它表示 JPEG 文件的开始。我需要在这个幻数之后存储字节
  • 您需要存储损坏的 JPG 文件吗?你在每个文件中“节省”了 2 个字节——我真的很好奇这里的用例。
  • 所以我不需要从我的字节数组中删除幻数?
  • 没有。如果你想存储 JPG 文件,你需要它,包括。幻数。
  • 可以用浏览器下载图片吗?如果是这样,请与您的代码下载的内容进行比较。检查大小,如果相等,检查内容。
【解决方案2】:

您的代码充满了错误。

while (!status) {
    byte[] buffer = new byte[1024]; // or 4096, or more

这个数组应该分配在循环之外。

    int bytesRead = input.read(buffer, 0, buffer.length);
    int availableBytes = input.available();

调用available() 通常毫无意义,这也不例外,尤其是因为您没有在任何地方使用availableBytes。

     str2 = str2.append(bytesRead);

如果这是一张图片,则不应将其存储在 String 中。 String 不是二进制数据的容器。

        ImageDataArray = concat(ImageDataArray, buffer);

由于您尚未发布 ImageDataArray,因此无法知道这意味着什么。

        countPackets = countPackets + buffer.length;

错了。应该是countPackets + bytesRead。您不能假设 read() 会填满缓冲区。这就是它返回值的原因。

        if (input.available() != 0) {

available() 的另一个误用。它不是对流结束或消息结束的测试。

            System.out.println("String:" + str1.toString());
        } else break;
    }
    System.out.println("--------------------out of while ------------------");
    File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "/SteelmanPro/1020/images/image" +filename1);
     if (!file.exists()) {
         file.createNewFile();
    }

当你要创建一个同名的new FileOutputStream(...)时,这个块不仅是多余的,而且是浪费的。

}
write(ImageDataArray, Environment.getExternalStorageDirectory().getAbsoluteFile() + "/SteelmanPro/1020/images/image" +filename1);

由于您尚未发布此write() 方法,因此无法对其发表评论。

concat() 方法:

public  byte[] concat(byte[]...arrays)

签名已经错了。您不知道这些数组中的任何一个都已填充。见上文。

{
    // Determine the length of the result array
    int totalLength = 0;
    for (int i = 0; i < arrays.length; i++)
    {
        totalLength += arrays[i].length;
    }

    // create the result array
    byte[] result = new byte[totalLength];

    // copy the source arrays into the result array
    int currentIndex = 0;
    for (int i = 0; i < arrays.length; i++)
    {
        System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
        currentIndex += arrays[i].length;
    }
    return result;
}

您不需要整个方法。您应该在收到数据时将数据写入文件。而不是浪费内存并增加延迟。

不要这样写代码。

【讨论】:

  • 'byte[] ImageDataArray= new byte[0];' .我已经发布了 write() 方法,请检查一下。
猜你喜欢
  • 2013-04-01
  • 2012-05-24
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-09
  • 1970-01-01
  • 2016-01-27
相关资源
最近更新 更多