错误:使用MultipartEntity实体上载文件时出现UnsupportedCharsetException

时间:2016-11-17 17:32:02

标签: java php android

我将表单发送到服务器,文本来自字段和文件Edittext。 我想我会发出错误的形状。 这是发送浏览器的一种形式:

    POST /wp-content/themes/reverie-master/contacts.php HTTP/1.1
Host: ukp.mogilev.by
Connection: keep-alive
Content-Length: 47504
Origin: http://ukp.mogilev.by
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryH00mEqZ9L5BBl7bz
Accept: */*
DNT: 1
Referer: http://ukp.mogilev.by/elektronnye-obrashcheniya-grazhdan/
Accept-Encoding: gzip, deflate
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: _ym_uid=1478194399500303049; _ym_isad=1; _ym_visorc_28369546=w
Request Payload
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="nameFF"

отправка с пк,тест
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="gorodFF"

отправка с пк,тест
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="streetFF"

отправка с пк,тест
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="homeFF"

отправка с пк,тест
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="contactFF"

q@tut.by
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="messageFF"

отправка с пк,тест
------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="fileFF[]"; filename="Instruction 3.1.png"
Content-Type: image/png


------WebKitFormBoundaryH00mEqZ9L5BBl7bz
Content-Disposition: form-data; name="fileFF[]"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryH00mEqZ9L5BBl7bz--

我使用AsyncTask帮助上传文件。

 private String uploadFile() {
        String responseString = null;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(UPLOAD_URL);
        MultipartEntity entity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        try {


            File filePathstorage = new File(getPath(filePath));//путь к файлу

            filename = filePathstorage.getName();//имя файла

            File path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);

            //File sendingFile = new File(path, filename);

            FileBody body = new FileBody(filePathstorage, "Content-Type: image/jpeg", filename);

            entity.addPart(KEY_NAME, new StringBody(name));
            entity.addPart(KEY_NASELPUNKT, new StringBody(naselpunkt));
            entity.addPart(KEY_STREET, new StringBody(street));
            entity.addPart(KEY_DOM, new StringBody(house));
            entity.addPart(KEY_EMAIL, new StringBody(e_mail));
            entity.addPart(KEY_MESAGES, new StringBody(message));


            entity.addPart(KEY_IMAGE, body);//  public static final String KEY_IMAGE = "fileFF[]";


            Log.e("RESULT_OTVET_entity", "Response from server: " + entity);
            httppost.setEntity(entity);

            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();

            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
            Log.v("Response for POst", s.toString());


            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                // Server response
                responseString = EntityUtils.toString(r_entity);
            } else {
                URLDecoder.decode(URLEncoder.encode(responseString, "iso8859-1"), "UTF-8");
                responseString = "Error occurred! Http Status Code: " + statusCode;
            }

        } catch (IOException e) {
            responseString = e.toString();
        }

        return responseString;

    }

在日志中我看到错误:

Caused by: java.nio.charset.UnsupportedCharsetException: IMG_20161115_113532.jpg

此行中的错误

 FileBody body = new FileBody(filePathstorage, "Content-Type: image/jpeg", filename);

2 个答案:

答案 0 :(得分:1)

尝试更改FileBody构造函数中的参数:GET https://i.pximg.net/img-original/img/2014/02/09/00/10/03/41485846_p0.jpg HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36 Accept: image/webp,image/*,*/*;q=0.8 Referer: http://www.pixiv.net/whitecube/user/2824699/illust/41485846 Accept-Encoding: gzip, deflate, sdch, br Accept-Language: en-US,en;q=0.8,sl;q=0.6 Host: i.pximg.net

请参阅:https://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/FileBody.html#constructor_summary

答案 1 :(得分:0)

中似乎filename
FileBody body = new FileBody(filePathstorage, "Content-Type: image/jpeg", filename);

行解释为charset。这是因为您可能使用旧版本的Apache HttpClient Mime API。

所以尝试类似的东西:

FileBody bin = new FileBody(new File(filename));