關於HttpClient上傳中文亂碼的解決辦法

2021-09-07 19:21:23 字數 1584 閱讀 1474

使用過httpclient的人都知道可以通過addtextbody方法來新增要上傳的文字資訊,但是,如果要上傳中文的話,或還有中文名稱的檔案會出現亂碼的問題,解決辦法其實很簡單:

第一步:設定multipartentitybuilder的編碼方式為utf-8。

設定請求的編碼格式

第二步:建立contenttype物件,指定utf-8編碼。

stringbody stringbody=new stringbody("中文亂碼",contenttype);

builder.addpart("test",stringbody);

附上完整**:

開啟乙個客戶端 http 請求

建立 http post 請求

//設定請求的編碼格式

設定瀏覽器相容模式

int count=0;

for(file file:files)

builder.addtextbody("method", params.get("method"));//

設定請求引數

builder.addtextbody("filetypes", params.get("filetypes"));//

設定請求引數

stringbody stringbody=new stringbody("中文亂碼",contenttype);

生成 http post 實體

post.setentity(entity);//

設定請求引數

發起請求 並返回請求的響應

if (response.getstatusline().getstatuscode()==200)

return

false;

httpclient 上傳檔案

3.0版本 用multipartrequestentity方式,怎麼搞都不行。最後還是用了multipartpostmethod,才算搞定 總之,不好使啊.具體使用方式如下 2,post.addrequestheader content type multipart form data charse...

HttpClient上傳檔案

httpclient post請求 上傳多 檔案 param url 請求位址 param params 引數列表 return 響應字串 throws unsupportedencodingexception author jie date 2015 2 12 public static stri...

CodeIgniter檔案上傳類上傳中文名檔案出錯

由於upload.php中的寫檔案是通過copy和move uploaded file來實現的,因此當中文名作為引數傳遞到copy函式中時,需要將gb2312編碼的中文轉換為utf 8編碼形式 config file name iconv utf 8 gb2312 files userfile na...