我在我的 BlackBerry 应用程序中使用 J2ME 的 HttpConnection 类将数据发送到 Web 服务器。我需要在 HTTP 请求的正文中发送图像的内容。
这就是我所做的
获取数组中文件的字节数
打开 HTTP 连接
将内容类型标头设置为 image/jpeg
获取连接的输出流
将字节写入输出流
关闭输出流和连接
但是图像没有上传到服务器。可能是什么问题呢?
谢谢。
编辑 - 添加代码
HttpConnection conn = null;
OutputStream out = null;
try{
conn = new HttpConnection(Connector.open(myURL));
conn.setRequestProperty("Content-Type", "image/jpeg");
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Disposition", "form-data");
conn.setRequestProperty("Connection", "Keep-Alive");
out = conn.openOutputStream;
out.write(buffer, 0, buffer.length);
conn.setRequestProperty("Content-Length", buffer.length);
out.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(out != null)
out.close();
if(conn != null){
System.out.println("" + conn.getResponseCode());
conn.close();
}
}
编辑
相同的代码,当我尝试使用字符串时,可以正常工作并将字符串发送到服务器。但这仍然是图像字节的问题。