client code is below
RRAWsecurity.dataOutputStream = new
DataOutputStream(RRAWsecurity.socket.getOutputStream());
int k=0;
RRAWsecurity.dataOutputStream.writeUTF(Integer.toString(data.length));
RRAWsecurity.dataOutputStream.flush();
int len = 400;
byte[] temp = new byte[len];
Toast.makeText(getApplicationContext(), Integer.toString(data.length),
Toast.LENGTH_LONG).show();
for(int i=0; i<=data.length/len; i++)
{
for(int j=0; j<len; j++)
{
if(i==data.length/len)
{
if(j == data.length%len)
break;
}
temp[j] = data[k++];
}
//Toast.makeText(getApplicationContext(), Integer.toString(temp.length),
Toast.LENGTH_LONG).show();
RRAWsecurity.dataOutputStream.write((temp));
}
RRAWsecurity.dataOutputStream.flush();
The Server code is here
private static DataInputStream dataInputStream;
public static void main(String[] args) throws IOException {
// create socket
int length_img = 0;
byte[] temp = new byte[2];
try {
ServerSocket servsock = new ServerSocket(27508);
Socket sock = servsock.accept();
dataInputStream = new DataInputStream(sock.getInputStream());
System.out.println("Accepted connection : " + sock);
int imgNum = 0;
// while (true) {
String lengtha = dataInputStream.readUTF();
length_img = Integer.parseInt(lengtha);
// byte[] arr = Base64.decodeBase64(base64);
byte[] data = new byte[length_img];
int data_index = 0;
int size = 0;
System.out.print("length_img : " + length_img + "\n");
/*
* while (size < (length_img + 1) && length_img != 0) {
* System.out.print("Here \n"); temp =
* dataInputStream.readUTF().getBytes(); // temp =
* Base64.decodeBase64(arr); // .getBytes();
*
* for (int z = 0; z < temp.length; z++) { data[data_index++] =
* temp[z]; size++; System.out.print("size = " + size);
* System.out.print(" data_index= " + data_index + "\n");
*
* } temp = null; } System.out.print(data + "\n");
*/
dataInputStream.read(data, 0, length_img);
System.out.print(data + "\n");
FileOutputStream imageOutFile = new FileOutputStream("E:\\image"
+ imgNum + ".jpeg");
imgNum++;
imageOutFile.write(data);
imageOutFile.close();
System.out.print("bublo");
// }
} catch (Exception e) {
System.out.print(e);
}
}
问题是我可以在服务器端接收图像,但图像已损坏...当我将此数据写为文本文件时,我看到异常字符(当然会出现)但是当我看不到图像时将它存储为jpeg一些腐败或问题。