我正在捕获从移动设备发送到本地服务器的数据。我没有在计算机中获得预期的数据,因此尝试使用WireShark捕获数据以查看发生了什么。 WireShark已成功接收到数据,但显示无法识别的文本。
我厌倦了以下代码以编程方式接收数据:
public class Server {
public static void main(String[] args) {
int port = 9090;
try (ServerSocket serverSocket = new ServerSocket(9090)) {
System.out.println("Server is listening on port " + port);
while (true) {
Socket socket = serverSocket.accept();
System.out.println("New client connected");
InputStream inputStreamm = socket.getInputStream();
FileOutputStream mFileOuptutStream = new FileOutputStream("output.txt");
byte[] fileBytes = new byte[100];
int fByte = inputStreamm.read(fileBytes);
int i = 0;
while (fByte > 0) {
mFileOuptutStream.write(fileBytes, 0, fByte);
fByte = inputStreamm.read(fileBytes);
i++;
}
mFileOuptutStream.close();
System.out.println("New file created");
inputStreamm.close();
socket.close();
}
} catch (IOException ex) {
System.out.println("Server exception: " + ex.getMessage());
ex.printStackTrace();
}
}
}
我已经使用以下代码示例将数据发送到服务器。
myhttp.sendtohttp(getrecorddate(upload_id),String.format(Locale.ENGLISH, url, ip, port));
public byte[] getrecorddate(int ID) {
Log.i("getrecorddate", "11111");
GlobalRecord uploadRecord = upload_db.readDbFile(sqlite_cmd.RecordTable, ID);
String jpg_filename = String.format(Locale.ENGLISH, "well%05d.jpg", ID);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int dataLen = 0;
int recordLen = 0;
byte strFormData[] = new byte[100];
for (int i = 0; i < 100; i++)
strFormData[i] = 0;
strFormData[0] = 0x68;
strFormData[1] = (byte) 0x99;
strFormData[2] = (byte) 0x88;
strFormData[3] = (byte) 0x77;
strFormData[4] = 0x78;
strFormData[5] = 0x01;
strFormData[6] = 0x09;
strFormData[7] = 0x68;
strFormData[8] = (byte) 0x84;
strFormData[11] = 0x50;
strFormData[12] = 0x00;
strFormData[21] = 0x01;
baos.write(strFormData, 0, 28);
byte[] jpgBuf = null;
int jpgLen = 0;
try {
String fileName = GlobalFinalValues.RECORD_PATH + "Num00/SubNum000/" + jpg_filename;
fileName = uploadRecord.String_Record[34];
FileInputStream fin = new FileInputStream(fileName);
jpgLen = fin.available();
jpgBuf = new byte[jpgLen];
fin.read(jpgBuf);
fin.close();
baos.write(jpgBuf, 0, jpgLen);
} catch (Exception e) {
jpgLen = 0;
e.printStackTrace();
}
if (uploadRecord != null) {
try {
// String jpg_filename="well00001.jpg";
byte[] jpg_name = jpg_filename.getBytes("UTF-8");
int jpg_filelen = jpg_name.length;
baos.write(jpg_filelen);
baos.write(jpg_name, 0, jpg_filelen);
baos.write(0xff);
recordLen = jpg_filelen + 2;
for (int i = 1; i < 15; i++) {
int m;
if (i > 7) m = i + 6;
else m = i;
if (i == 14) m = 35;
String str = uploadRecord.String_Record[m];
if (m == 35) {
str = uploadRecord.String_Record[35] + ";" + uploadRecord.String_Record[36];
}//GPS
if (str.length() > 50) str = str.substring(0, 50);
byte[] srtbyte = str.getBytes("UTF-8");
int len = srtbyte.length;
baos.write(len % 256);
if (len > 0)
baos.write(srtbyte, 0, len);
baos.write(0xff);
recordLen += len + 2;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
baos.write(recordLen / 256);
baos.write(recordLen % 256);
baos.write(0xff);
recordLen += 3;
baos.write(0);
baos.write(0x16);
byte[] data = baos.toByteArray();
dataLen = data.length;
data[9] = (byte) ((dataLen - 13) % 65536 / 256);
data[10] = (byte) ((dataLen - 13) % 65536 % 256);
data[13] = (byte) ((dataLen - 13) / 65536);
//data[14] = (byte) ((dataLen - 13) / 65536 % 256);
data[dataLen - 2] = 0x00;
for (int i = 0; i < (dataLen - 2); i++)
data[dataLen - 2] += data[i];
return data;
}
public boolean sendtohttp(byte strFormData[], String url) {
boolean back = false;
HttpURLConnection urlConnection = null;
try {
int dataLen = strFormData.length;
urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(dataLen);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
("application/xml; charset=utf-8").replaceAll("\\s", ""));
OutputStream out = urlConnection.getOutputStream();
out.write(strFormData, 0, dataLen);
out.close();
int responseCode = urlConnection.getResponseCode();
InputStream in = null;
if (responseCode == 200) {
in = new BufferedInputStream(urlConnection.getInputStream());
} else {
in = new BufferedInputStream(urlConnection.getErrorStream());
}
byte readbuf[] = new byte[100];
int readlen = in.read(readbuf, 0, 100);
String str = new String(readbuf, 0, readlen, "UTF-8");
if (str.equals("succ")) {
back = true;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
return back;
}
如果您可以指导我,我将不胜感激这项技术。
我期望数据以文本形式显示,但我得到以下输出:
POST / alky_gps_server / alky / send9_1 HTTP / 1.1 内容类型:application / xml; charset = utf-8 内容长度:25925 用户代理:Dalvik / 2.1.0(Linux; U; Android 5.1.1; Drone Build / LMY48G上的AOSP) 主机:192.168.0.75:9090 连接:保持活动 接受编码:gzip
h��wxh�e8P����JFIF��C
%#,#&')*)-0-(0%()(��C
((((((((((((((((((( ((((((����“��
���}!1AQa“q2.��#B��R��$ 3br。
%&'()* 456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������� ����������������������������
���w!1AQaq“2�B����#3R�br。
$4�%�&'() 56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz������������������������������������ ���������������������������� �@�〜T9m��g&�����#��)n�9�h<�V�l5�y4�i���)1�zhM���(��B。 ��ݹ���:���B�G8�����Ҍm���1DIhj�g= iTg。“ ~~&b。(�Fj�A�;��
“���P-�Ҝ���ccGʝ�=(sFp =���;�@��P1��N�84�3
qN ^�����_ژ���S�z�qq�z������I��ļ���������������������������������������������������������������4�����p�ۧ�@=9(^��dNP:H"7#<P�<�t����LOAXp �t�<����=:PTE���9xjS�8}i���s�4|٧�<�N��-E�{pi��<�G^��N�v�
!
c4(��: g�ȡ���ii�Ju {//NizP5.hLsN.q�As @�=r��=�������2�1B�)�ڐ�� x���BM9�)g���i@�JR8��i99�9�SH���(�8�k�^ GJi G =iFH�h��M�v s rQ�v���ܯ'�GN��+ c = {Q���!��#ph ^�zU��H�,)��F�J^〜��F。 ���$��0s�jM��
�M;�g<�r�''�&����H���hM���N^ =��Hu��n��4�oo$Ҩ�@�]g��� �F�-bM8�V�,KjKmE#&F��
��H���z〜�>���uxe����Vh���O�3�1D4f5��������6��i�U3yr� E���:���>��z���k7��?:[5�4�\��〜5�j���������Z�RFm��$。 �Y�}����l5��N�ÚY���.D`y'v2A�=kE�0��+G���{a�+��= Sϱ��}�Iq8NT o =�8��4{�������s。[�%?4�q��<)�������6O�\��B���������6 ���W�z�