Android文件上传将内容类型设置为图像

时间:2022-06-07 21:21:45

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

我有以下代码将图像从Android上传到Web服务器。

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

1 个解决方案

#1


0  

Add this line too:

添加此行:

ac.header("Content-Type", "image/png" charset=utf-8");

And in server side, you should defined mime-mapping as:

在服务器端,您应该将mime-mapping定义为:

<mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
</mime-mapping>

#1


0  

Add this line too:

添加此行:

ac.header("Content-Type", "image/png" charset=utf-8");

And in server side, you should defined mime-mapping as:

在服务器端,您应该将mime-mapping定义为:

<mime-mapping>
        <extension>png</extension>
        <mime-type>image/png</mime-type>
</mime-mapping>