用于上传图像的多部分http请求

时间:2011-08-22 11:08:57

标签: blackberry post java-me upload multipart

我尝试使用multipart post请求上传两个字符串参数(令牌和源)以及从移动设备捕获的图像(返回jpeg编码的byte [])。但它产生了错误,我确信这是由于不正确的请求。

我仍然不确定有关创建多部分帖子请求的注释。所以任何有用的链接和资源都会很好。

该代码适用于Blackberry Java开发

//------------------------------------------------------------------------------//

StringBuffer buffer = new StringBuffer();
String boundary = "--@#$--";
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();

\ ---------------------------------------------- ------------------------------------ \

1 个答案:

答案 0 :(得分:3)

也许这个诺基亚社区维基页面可以帮助您:HTTP Post multipart file upload in Java ME