6

I am having below code to upload an image from Android to a web server.

It works fine except that the received image is of Content Type Octet Stream and I need it to be Content Type Image. Any idea how I can set the content type?

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();
}
4

1 回答 1

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>
于 2015-11-18T11:13:02.143 回答