0

I send the form to the server, the text from the field and file Edittext. I think I will send the wrong shape. This is a form of sending the browser:

    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"

[email protected]
------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--

I upload file with help 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;

    }

In log i see error:

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

The error in this line

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

2 Answers 2

1

Try change the parameter in FileBody constructor: FileBody(filePathstorage, ContentType.MULTIPART_FORM_DATA, filename);

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

Sign up to request clarification or add additional context in comments.

3 Comments

Error: IllegalStateException: Content has been consumed in line responseString = EntityUtils.toString(r_entity);
in line responseString = EntityUtils.toString(r_entity);
That's because you are consuming the response twice. The StringBuilder 's' variable already have the result, so you can do: responseString = s.toString();
0

Seems filename in

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

line interpreted as charset. This is because You probably use old version of Apache HttpClient Mime API.

So try something like that:

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

7 Comments

FileBody bin = new FileBody(new File(filename)); E/RESULT_OTVET: Response from server: java.io.FileNotFoundException: /IMG_20161115_113532.jpg: open failed: ENOENT (No such file or directory)
You should combine filePathstorage and filename to get filename with full path to it. Like new FileBody(new File(filePathstorage + "\" +filename));. And "отправка с пк,тест" and "name="gorodFF"" - это так мило )
@ Andriy Omelchenko you propose to do so FileBody body = new FileBody(filePathstorage, ContentType.MULTIPART_FORM_DATA, filename); but I've already done that, and indicated the error below
I'm propose FileBody(new File(filePathstorage + "\" +filename)); - with 1 argument not FileBody(filePathstorage, ContentType.MULTIPART_FORM_DATA, filename); - with 3.
And You use old (probably 4.3 or lower) Apache HttpClient Mime API. Update it, or better get rid of it (see official documentation and this).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.