2016-11-17 31 views
0

我將表單發送到服務器,字段和文件Edittext中的文本。 我想我會發送錯誤的形狀。 這是向瀏覽器發送的形式:錯誤:使用MultipartEntity實體上載文件時出現UnsupportedCharsetException

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-- 

我上傳的文件有幫助的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; 

    } 

在日誌中我看到的錯誤:在

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

行解釋爲字符集

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

錯誤在這條線

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

回答

1

嘗試改變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

+0

'錯誤: IllegalStateException異常:內容已在線路 'responseString = EntityUtils.toString(r_entity)被consumed' ; ' –

+0

這個錯誤是在FileBody行嗎? – fsnasser

+0

在行 'responseString = EntityUtils。toString(r_entity);' –

0

似乎filename。這是因爲您可能使用舊版本的Apache HttpClient Mime API。

因此,嘗試這樣的事情:

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

FileBody斌=新FileBody(新文件(文件名)); E/RESULT_OTVET:來自服務器的響應:java.io.FileNotFoundException:/IMG_20161115_113532.jpg:打開失敗:ENOENT(沒有這樣的文件或目錄) –

+0

您應該結合'filePathstorage'和'filename'來獲取具有完整路徑的文件名。像'新的FileBody(新文件(filePathstorage +「\」+文件名));'。和「отправкаспк,тест」和「name =」gorodFF「」 - этотакмило) –

+0

@ Andriy Omelchenko 您建議這麼做 'FileBody body = new FileBody(filePathstorage,ContentType.MULTIPART_FORM_DATA,filename);'但是我已經做到了,並指出了錯誤 –

相關問題