如何在套接字连接中下载图像并删除不需要的字节
How to download an image in socket connection and remove the unwanted bytes
我需要通过套接字连接下载图像。目前我有一个字节数组,其中包含一个 .jpg 文件以及一些其他不需要的字节 (ffd8)。我需要从我的字节数组中删除这些不需要的字节。我试图在不删除字节的情况下下载文件,然后我得到格式损坏的图像。所以我想知道如何从我的 bytearray 中删除不需要的字节。
在我的代码中:
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
导致您最终损坏并且您仍然尝试这样做,需要发生什么才能让您不再认为 "unwanted" 字节实际上是 "very wanted"? (双关语)
事实上 FF D8 FF
是 JPEG 的 magic number 并且是 JPEG 文件的一部分。
您的代码充满错误。
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()
会填满缓冲区。这就是为什么它 returns 一个值。
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;
}
您不需要整个方法。您应该在收到数据时将数据写入文件 。 而不是浪费内存和增加延迟。
不要这样写代码。
我需要通过套接字连接下载图像。目前我有一个字节数组,其中包含一个 .jpg 文件以及一些其他不需要的字节 (ffd8)。我需要从我的字节数组中删除这些不需要的字节。我试图在不删除字节的情况下下载文件,然后我得到格式损坏的图像。所以我想知道如何从我的 bytearray 中删除不需要的字节。
在我的代码中:
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
导致您最终损坏并且您仍然尝试这样做,需要发生什么才能让您不再认为 "unwanted" 字节实际上是 "very wanted"? (双关语)
事实上 FF D8 FF
是 JPEG 的 magic number 并且是 JPEG 文件的一部分。
您的代码充满错误。
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()
会填满缓冲区。这就是为什么它 returns 一个值。
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;
}
您不需要整个方法。您应该在收到数据时将数据写入文件 。 而不是浪费内存和增加延迟。
不要这样写代码。