在 HTTP 请求正文中发送数据失败
我在 BlackBerry 应用程序中使用 J2ME 的 HttpConnection 类将数据发送到 Web 服务器。 我需要在 HTTP 请求正文中发送图像的内容。
这就是我所做的
获取数组中文件的字节
打开HTTP连接
设置内容类型标头as 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();
}
}
编辑
相同的代码,当我尝试使用字符串时,工作正常并将字符串发送到服务器。 但仍然是图像字节的问题。
I am using the HttpConnection class of J2ME in my BlackBerry app to send data to a web server. I need to send the contents of an image in the body of the HTTP request.
This is what I do
Get the bytes of the file in an array
Open HTTP connection
Set content type header as image/jpeg
Get output stream of the connection
Write the bytes to the output stream
Close the output stream and connection
But the image is not uploaded to the server. What could be the problem?
Thanks.
EDIT - Adding code
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();
}
}
EDIT
The same code, when I try it with a string, works fine and sends the string to the server. But it is still a problem with the image bytes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的列表中可能缺少一些内容:
A few things that may be missing from your list:
这条线是错误的。 Connection 是一个工厂类,它通过查看适当的协议实现来创建新的 Connection 对象。
其余代码看起来没问题。 当您发布时,您至少需要定义内容类型和内容长度。
This line is wrong. Connection is a factory class that creates new Connection objects by looking it's appropriate protocol implementation.
The rest of the code seems ok. When you're POSTing, at minimun you need to define the content-type and content-length.
您绝对需要在发送 POST 数据之前设置所有标头,包括
Content-Length
标头。另外,请确保您发送的标头对请求有效,而不是仅响应的标头。
You most definitely need to set all headers before sending the POST data, including the
Content-Length
header.Also, make sure you are sending headers valid for requests, and not response-only headers.
您需要对字节进行编码(最好是 Base-64)并发送该字符串。 原始字节不太可能是 http 安全的。
然后在服务器上,您需要将它们解码回字节数组并执行您要使用它执行的任何操作(另存为文件、填充到数据库中等)
You'll need to encode the bytes (preferably Base-64) and send that string. The raw bytes aren't likely to be http safe.
Then on the server, you'll need to decode them back into a byte array and do whatever you were going to do with it (save as file, stuff into database, etc.)