0
我試圖使用多部分發布請求上傳兩個字符串參數(令牌和源)和從移動設備捕獲的圖像(返回jpeg編碼的字節[])。但它確實產生錯誤,我確信是由於請求不正確。multipart http請求上傳圖像
我仍然不確定有關創建多部分發布請求的註釋。所以任何有用的鏈接和資源都會很好。
的代碼是黑莓Java開發
//------------------------------------------------------------------------------//
StringBuffer buffer = new StringBuffer();
String boundary = "[email protected]#$--";
byte[] image = byte[] from camera.getsnapshot;
buffer.append(boundary+"\r\nContent-Disposition: form- data;name=\"token\"\r\n"+token+"\r\n");
buffer.append(boundary+"\r\nContent-Disposition: form- data;name=\"source\"\r\n"+"Blackberry"+"\r\n");
buffer.append(boundary+"\r\nContent-Disposition: form- data;name=\"file.jpg\";filename=\""+ "file.jpg"+"\""+"\n" + "Content- Type:image/jpeg"+"\n"+ "Content-Transfer-Encoding: binary" + boundary +"\r\n" +new String(image));
buffer.append("\r\n" + boundary + "\r\n");
String string = new String(buffer);
byte[] post = string.getBytes();
HttpConnection connection = (HttpConnection)Connector.open(url);
connection.setRequestMethod("POST");
connection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA+
";boundary="+boundary);
connection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,String.valueOf(post.length));
connection.setRequestProperty("User-Agent", "Profile/MIDP_2.0 Configuration/CLDC-1.0");
OutputStream postStream =connection.openOutputStream();
postStream.write(post,0,post.length);
postStream.close();
\ ------------------------------- -------------------------------------------------- - \
向我們展示錯誤。 –