错误:UnsupportedCharsetException时,上传文件与多父实体


Error: UnsupportedCharsetException when upload file with MultipartEntity entity

我将表单发送到服务器,字段中的文本和文件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);

尝试更改FileBody构造函数中的参数:FileBody(filePathstorage, ContentType.MULTIPART_FORM_DATA, filename);

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

Seems filename in

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

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

那么试试这样写:

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