java - 发布图像无效的 MIME 类型

标签 java android

我正在尝试从我的 Android 设备将图片从我的画廊发布到服务器。 他们在后台使用 Python。

这就是后台开发人员所说的: - Django 无法读取 Android 应用程序在 request.FILES 中发布的文件。 iOS 正确地做到了这一点。 - 似乎 Multipart POST 没有正确设置正确读取请求所需的键:值。

我收到这个错误:

{"errorMessage":"","message":"Invalid mime type","errorCode":0,"success":false}

知道为什么吗?

这是我的代码:

public static final String IMAGE_JPEG = "image/jpeg";

private HttpEntity getImageEntity() throws Exception {

            File imageFile;
            Uri originalUri = Uri.parse(this.mFileName);
            String originalPath = originalUri.getPath();
            boolean isEncrypted = originalPath.contains(FileNames.CACHE_DIR.getPath()); 
            // check if file encrypted or not
            if (isEncrypted && ImageLoader.IMAGE_CODING_ENABLED) {
                File originalImageFile = new File(originalPath);
                String decodedPath = CipherUtils.decryptFile(SmartPagerApplication.getInstance(), originalImageFile);
                imageFile = new File(decodedPath);
            } else {
                imageFile = new File(originalPath);
            }

            InputStream fis = imageFile.toURI().toURL().openStream();

            int rotation = PhotoFileUtils.getOrientation(this.mFileName);
            if (rotation > 0) {
                byte[] data;
                Bitmap rotateBitmap = PhotoFileUtils.checkOrientation(BitmapFactory.decodeStream(fis), rotation);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                rotateBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                data = stream.toByteArray();
                stream.close();
                rotateBitmap.recycle();
                fis.close();
                fis = new ByteArrayInputStream(data);
            } else {
                // data = IOUtils.toByteArray(fis);
            }
            return getMultipartEntity(originalUri, fis);
}

private MultipartEntity getMultipartEntity(Uri originalPath, InputStream fis) {

            InputStreamBody isb = new InputStreamBody(fis, mMimeType, originalPath.getLastPathSegment());

            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,
                    "----WebKitFormBoundaryzGJGBFWyGteE24tw", Charset.forName("ISO-8859-1"));
            multipartEntity.addPart("binaryFile", isb);
            return multipartEntity;
}


private String executePost(String url, HttpEntity params) throws ClientProtocolException, IOException {
            Log.e("executePost url =" + url);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-Type", "multipart/form-data; boundary="+"----WebKitFormBoundaryzGJGBFWyGteE24tw");
            httpPost.addHeader("Cache-Control", "no-cache");
            httpPost.setEntity(params);

            String response =  SmartPagerHTTPClient.getHttpClient().execute(httpPost, new BasicResponseHandler());

            return response;
 }

最佳答案

我没有足够的声誉来发表评论,所以我必须把这个作为答案。在您的方法 getMultipartEntity() 中,第一行:

InputStreamBody isb = new InputStreamBody(fis, mMimeType,  originalPath.getLastPathSegment());

mMimeType 的值是多少?请确保它是正确的 MIME 类型。

关于java - 发布图像无效的 MIME 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383782/

相关文章:

java - Java 文件读取有什么问题?

java - RMI 中的 UnsupportedOperationException

android - 左侧为 Listview,右侧为 tabhost,带有 20 :80 screen sharing ratio

android - PopBackStack 但在 android 中保留第一个 fragment

java - Android打印管理器-返回保存的pdf文件路径

javascript - 在没有 token 的情况下保护 API 调用

java - 第一个图像在 GridView 中的 ImageView 单击时更改

android - 如何在抽屉导航 fragment 中放置两个回收 View

java - 删除计算器应用程序中最后一位数字的最佳方法是什么

java - 对象引用 - 什么需要更多空间?