gpt4 book ai didi

java - 如何在套接字连接中下载图像并删除不需要的字节

转载 作者:行者123 更新时间:2023-12-02 02:53:14 27 4
gpt4 key购买 nike

我需要通过套接字连接下载图像。目前我有一个字节数组,其中包含 .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);
}
}

最佳答案

Which contains a .jpg file also some other unwanted bytes (ffd8) I tried to download the file without removing the bytes then I got image in corrupted format

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

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

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

关于java - 如何在套接字连接中下载图像并删除不需要的字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43492179/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com