我想把一个bitmap对象通过socket方式传到服务器端,
发送端代码(这里尝试了两种方式,注释里面是直接压入outputstream,和转成byte数组方式)ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Bitmap b;
byte []buffer = null;
buffer = Bitmap2Bytes(b);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(buffer);
// b.compress(Bitmap.CompressFormat.JPEG, 50, bos);
oos.write(bos.toByteArray());接收端ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
Object got=ois.readObject();
while(got!=null){if (got instanceof byte[]){
if(((byte[])got).length!=0){
String fileName = "D:\\copy.jpg";
System.out.println("图片,存往文件:" + fileName )
FileOutputStream fos = new FileOutputStream( fileName );
fos.write( (byte[])got );
fos.flush();
fos.close();
}
got = ois.readObject();
}用这种方式传输可以吗? 主要是bitmap那一段,但是会出现java.io.OptionalDataException异常,为什么,应该如何解决?
发送端代码(这里尝试了两种方式,注释里面是直接压入outputstream,和转成byte数组方式)ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
Bitmap b;
byte []buffer = null;
buffer = Bitmap2Bytes(b);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(buffer);
// b.compress(Bitmap.CompressFormat.JPEG, 50, bos);
oos.write(bos.toByteArray());接收端ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
Object got=ois.readObject();
while(got!=null){if (got instanceof byte[]){
if(((byte[])got).length!=0){
String fileName = "D:\\copy.jpg";
System.out.println("图片,存往文件:" + fileName )
FileOutputStream fos = new FileOutputStream( fileName );
fos.write( (byte[])got );
fos.flush();
fos.close();
}
got = ois.readObject();
}用这种方式传输可以吗? 主要是bitmap那一段,但是会出现java.io.OptionalDataException异常,为什么,应该如何解决?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货