我想将存储在RMS中的图像发送到服务器。为此,我将捕获的图像存储在RMS中。我可以成功地访问它,并在设备上显示它,但当我把它发送到服务器时,服务器上只有图像的名字出现,但图像没有生成。
这是我试图使用的行代码
byte[] byteArrRec = LoadImagesFromRMS.objImageRecordStore.getRecord(recID);
ByteArrayInputStream bin = new ByteArrayInputStream(byteArrRec);
DataInputStream din = new DataInputStream(bin);
int width = din.readInt();
int height = din.readInt();
int length = din.readInt();
int[] rawImg = new int[width * height];
for (int itemp = 0; itemp < length; itemp++) {
rawImg[itemp] = din.readInt();
}
Image tempImage = Image.createRGBImage(rawImg, width, height, false);
byteArr = get_Byte_Array(tempImage);
byteArr = get_Byte_Array(tempImage);
然后我使用post方法在服务器上传递了byteArray。
但是图像没有生成,有人知道吗?
- 首先需要从响应中读取所有字节并存储在字节数组的一个变量(
bytearray
)中。然后写这段代码 -
从字节数组中创建一个
ByteArrayInputStream
,然后使用ImageIO
类从该流中读取图像。InputStream in = new ByteArrayInputStream(bytearray);
BufferedImage image = ImageIO.read(in);
谢谢
您需要与远程服务器创建HttpConnection
,在创建连接后,创建与HttpConnection
变量关联的DataOutputStream
变量。现在将字节数组写入DataOutputStream
变量并将其作为"POST"
方法发送。如果字节数组的大小非常大,请尝试以块的形式发送。