【问题标题】:Android File Upload set Content Type to ImageAndroid File Upload 将 Content Type 设置为 Image
【发布时间】:2014-08-22 16:02:14
【问题描述】:

我有以下代码将图像从 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();
}

【问题讨论】:

  • 您找到解决方案了吗?
  • 这是一个棘手的问题。我在为 Windows 8 应用程序开发的 javascript 中使用了它。
  • 您设置授权后是否尝试在标题中设置内容类型? ac.header("Content-Type", "application/png");
  • 找到解决方案了吗?

标签: java android file-upload aquery


【解决方案1】:

也加入这一行:

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

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

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

【讨论】:

    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-10-04
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    相关资源
    最近更新 更多