我想從Swing應用程序發送HashMap到Web Application.HashMap類型爲。它包含一個字符串,ArrayList和字節數組。我想在Web應用程序中檢索此地圖。使用URLConnection發送和檢索地圖
搖擺端代碼:
private static void sendMap()
{
System.out.println("Sending MAP");
byte[] bytes=getByteArray();
ArrayList<String> list =new ArrayList<String>();
list.add("ABC");
list.add("XYZ");
list.add("ABXY");
Map<String, Object> params=new HashMap<String, Object>();
params.put("Type", "Document");
params.put("bytes", bytes);
params.put("PartyNameList", list);
try {
URL url= new URL(iinkiturl+"/getMap?id=1234567");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setAllowUserInteraction(false);
DataOutputStream dstream = new DataOutputStream(uc.getOutputStream());
// The POST line
System.out.println(toByteArray(params));
dstream.write(params.toString().getBytes());
dstream.close();
InputStream in = uc.getInputStream();
int x;
while ((x = in.read()) != -1) {
System.out.write(x);
}
in.close();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String line;
while ((line = r.readLine()) != null) {
buf.append(line);
}
} catch(Exception exception)
{
exception.printStackTrace();
}
}
Web應用程序端:
InputStream in = request.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuffer buf = new StringBuffer();
String line;
while ((line = r.readLine())!=null) {
buf.append(line);
}
System.out.println("Buffer ======= "+buf.toString());
輸出:
緩衝======= {類型=歸檔,PartyNameList = [ABC, XYZ,ABXY],字節= [B @ 24c0f1ec}
如何從此緩衝區獲取Map對象。或者是否有任何替代解決方案來實現這一點。提前致謝。
見XML或JSON對象,而相應的API,以生產和在Java中使用它們... –
使用Arrays.toString到修復字節數組 –
通常項目在某個框架上增長。幾乎所有人都有對編碼/解碼參數的概念。 –