我有以下代码将图像从Android上传到网络服务器。
除了收到的图像是内容类型八位组流之外,它工作得很好,我需要它是内容类型图像。知道如何设置内容类型吗?
public void sendImage(final Bitmap mBitmap, final String caption, final WebListener webListener) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
AQuery aq = new AQuery(smApplication);
Map<String, Object> params = new HashMap<String, Object>();
params.put("file_name", "name" + (int) (Math.random() * 1000));
params.put("caption", caption);
ByteArrayOutputStream blob = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, blob);
byte[] bitmapdata = blob.toByteArray();
params.put("photo", bitmapdata);
AjaxCallback<JSONObject> ac = new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject object, AjaxStatus status) {
if (status.getCode() == 200) {
try {
String success = object.getString("success");
if (success.equals("postCreated")) {
webListener.onCompleted(true);
}
} catch (JSONException e) {
webListener.onServiceError(e.getMessage());
}
} else {
error(status.getError(), webListener);
}
}
};
ac.header("Authorization", "Token token=" + Cache.getInstance().getDeviceKey());
aq.ajax(HOST + API + "posts", params, JSONObject.class, ac, "photoCb");
}
});
thread.start();
}
答案 0 :(得分:0)
也添加此行:
ac.header("Content-Type", "image/png" charset=utf-8");
在服务器端,您应该将mime-mapping定义为:
<mime-mapping>
<extension>png</extension>
<mime-type>image/png</mime-type>
</mime-mapping>