android socket 从客户端到服务器传图片的奇葩现象,求大神指导下
android客户端代码:
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mybtn = (Button) findViewById(R.id.mybtn);
mybtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Socket socket;
try {
socket = new Socket("192.168.1.102", 40000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ImageView myview = (ImageView) findViewById(R.id.myview);
myview.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//读取图片到ByteArrayOutputStream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
Log.i("Client02:", bytes.length+"");
out.write(bytes);
out.close();
socket.close();
} catch (UnknownHostException ex) {
Log.i("Client02:", "192.168.1.102 is unknow host!!");
} catch (IOException ex) {
Log.i("Client02:", "socket create failure!!!!userperssion。。。");
}
}
});
}
android服务器端代码:
public class Server02 implements Runnable {
@Override
public void run() {
try {
ServerSocket server = new ServerSocket(40000);
System.out.println("Server02 is ready to receive.....");
Socket socket = server.accept();
//通过输入流获取图片数据
DataInputStream dis = new DataInputStream(socket.getInputStream());
int len = dis.available();
System.out.println("Server02 datalength = " + len);
//得到图片的二进制数据,以二进制封装得到数据,具有通用性
byte[] data = new byte[len];
//new一个文件对象用来保存图片,默认保存当前工程根目录
File imageFile = new File("E:/firest_project_diary/test/2.jpg");
//创建输出流
FileOutputStream outStream = new FileOutputStream(imageFile);
//写入数据
outStream.write(data);
//关闭输出流
outStream.close();
System.out.println("Server02 succeed to receive picture!!" );
System.out.println("Server02 data = " + data);
dis.close();
socket.close();
server.close();
} catch (IOException ex) {
System.out.println("IOexception!!!!");
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Thread testThread=new Thread(new Server02());
testThread.start();
}
}
现象:我在android客户端的图片的大小有1.62KB,但是获取到的byte只有873字节、、然后在服务器端获取到的len总是为0,只有在那句话的前面打断点调试时,然后就能收到873字节。保存到本地的图片有大小但是打不开、、、、那位大神解释下吧,小弟在线等!!!!
[解决办法]
代码没试,看起来是这个问题
DataInputStream dis = new DataInputStream(socket.getInputStream());
socket流应该是获取不到长度的,因为只有都读完了才会知道流长度嘛。
你打了断点之后可以获取,是因为执行到断点之后,有足够的时候把流里面的内容全读取完,就可以获取流长度了,此方法不可取
[解决办法]
dis要read了以后才有数据,还没read就调用len = dis.available();,所以len总是0.
dis = new DataInputStream(socket.getInputStream());
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
byte[] inputByte = new byte[1024];
int length = 0;
while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {
bytestream.write(inputByte, 0, length);
Log.v(TAG, "received " + length + " bytes");
}
Log.v(TAG, "there is " + bytestream.size() + " bytes");
[解决办法]
干吗用原始的socket 这要自己构建协议,多扯淡,不如直接用http或者ftp