HTTP_BAD_REQUEST 使用 HttpUrlConnection 发送图像文件时出错
HTTP_BAD_REQUEST errors while sending image files using HttpUrlConnection
我在使用 httpurlconnection 将图像发送到我的服务器时遇到问题。我已阅读 Android documentation and another HttpUrlconnection implementation 但我不知道我在哪里做错了,因为我收到 HTTP_BAD_REQUEST 错误代码 (400)。我在下面的代码中缺少什么重要的东西?
我的响应代码总是 return 400 但我的 link 没问题,因为我可以使用 httpclient
实现
link = "my link.com";
try {
URL url = new URL(link);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(0);
connection.setRequestProperty("Content-Type", "image/jpeg");
BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
FileInputStream stream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead =stream.read(buffer ,0 ,buffer.length)) != -1){
outputStream.write(buffer);
outputStream.flush();
}
outputStream.flush();
responseCode = connection.getResponseCode();
我认为问题在于如何将图像添加到输出流。所有连接配置步骤看起来都不错。
我最近尝试了这个方法,效果很好:
包装在 AsyncTask 中也是一个好习惯。我注意到 MultipartEntity 现在已被弃用,但您可以替换为 MultipartEntityBuilder。
更新
要监听文件上传事件并更新进度条,您可以覆盖任何 HttpEntity 实现的 writeTo
方法,并在字节写入输出流时计算字节数。
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://www.google.com/sorry");
MultipartEntity outentity = new MultipartEntity() {
@Override
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new CoutingOutputStream(outstream));
}
};
outentity.addPart("stuff", new StringBody("Stuff"));
httppost.setEntity(outentity);
HttpResponse rsp = httpclient.execute(httppost);
HttpEntity inentity = rsp.getEntity();
EntityUtils.consume(inentity);
} finally {
httpclient.getConnectionManager().shutdown();
}
static class CoutingOutputStream extends FilterOutputStream {
CoutingOutputStream(final OutputStream out) {
super(out);
}
@Override
public void write(int b) throws IOException {
out.write(b);
System.out.println("Written 1 byte");
}
@Override
public void write(byte[] b) throws IOException {
out.write(b);
System.out.println("Written " + b.length + " bytes");
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
System.out.println("Written " + len + " bytes");
}
}
更新
如果您想根据 http 进度更新进度条,这个 link 提供了一个很好的例子
Link
我在使用 httpurlconnection 将图像发送到我的服务器时遇到问题。我已阅读 Android documentation and another HttpUrlconnection implementation 但我不知道我在哪里做错了,因为我收到 HTTP_BAD_REQUEST 错误代码 (400)。我在下面的代码中缺少什么重要的东西?
我的响应代码总是 return 400 但我的 link 没问题,因为我可以使用 httpclient
实现 link = "my link.com";
try {
URL url = new URL(link);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setChunkedStreamingMode(0);
connection.setRequestProperty("Content-Type", "image/jpeg");
BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
FileInputStream stream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead =stream.read(buffer ,0 ,buffer.length)) != -1){
outputStream.write(buffer);
outputStream.flush();
}
outputStream.flush();
responseCode = connection.getResponseCode();
我认为问题在于如何将图像添加到输出流。所有连接配置步骤看起来都不错。
我最近尝试了这个方法,效果很好:
包装在 AsyncTask 中也是一个好习惯。我注意到 MultipartEntity 现在已被弃用,但您可以替换为 MultipartEntityBuilder。
更新
要监听文件上传事件并更新进度条,您可以覆盖任何 HttpEntity 实现的 writeTo
方法,并在字节写入输出流时计算字节数。
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://www.google.com/sorry");
MultipartEntity outentity = new MultipartEntity() {
@Override
public void writeTo(final OutputStream outstream) throws IOException {
super.writeTo(new CoutingOutputStream(outstream));
}
};
outentity.addPart("stuff", new StringBody("Stuff"));
httppost.setEntity(outentity);
HttpResponse rsp = httpclient.execute(httppost);
HttpEntity inentity = rsp.getEntity();
EntityUtils.consume(inentity);
} finally {
httpclient.getConnectionManager().shutdown();
}
static class CoutingOutputStream extends FilterOutputStream {
CoutingOutputStream(final OutputStream out) {
super(out);
}
@Override
public void write(int b) throws IOException {
out.write(b);
System.out.println("Written 1 byte");
}
@Override
public void write(byte[] b) throws IOException {
out.write(b);
System.out.println("Written " + b.length + " bytes");
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
System.out.println("Written " + len + " bytes");
}
}
更新 如果您想根据 http 进度更新进度条,这个 link 提供了一个很好的例子
Link