【发布时间】:2013-03-10 22:00:48
【问题描述】:
我正在开发小型 android 应用程序,我想在其中将图像从我的 android 设备上传到我的服务器。我为此使用HttpURLConnection。
我这样做是通过以下方式:
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow_down_float);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "image/jpeg");
connection.setRequestMethod(method.toString());
ByteArrayOutputStream bout = new ByteArrayOutputStream();
bout.write(data);
bout.close();
我正在使用ByteArrayOutputStream,但我不知道如何使用我的 httpurlconnection 传递该数据。这是传递原始图像数据的正确方法吗?我只是想发送包含图像数据的字节数组。没有转换或没有多部分发送。
我的代码工作正常,没有任何错误,但我的服务器给了我回复
{"error":"Mimetype not supported: inode\/x-empty"}
我使用setEntity 使用httpclient 完成了此操作,并且可以正常工作。但我想使用 urlconnection。
我做错了吗?这个怎么做? 谢谢。
【问题讨论】:
-
我想按原样发送此字节数组,而不将其转换为字符串或多部分任何解决方案。就像在 httpclient 中我做了这个
client.setEntity(new ByteArrayEntity(data));有没有办法在 urlconnection 中做类似的事情。需要帮忙。谢谢。
标签: android mime-types content-type httpurlconnection