日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

httpclient上傳文件使用

系統(tǒng) 2380 0

org.apache.commons.httpclient.HttpClient hc = new
????? org.apache.commons.httpclient.HttpClient();
? hc.getHostConfiguration().setHost("localhost", 80);
? org.apache.commons.httpclient.methods.MultipartPostMethod mpm = new
????? org.apache.commons.httpclient.methods.MultipartPostMethod("/upload.asp");
? mpm.addParameter("username", "username");
? mpm.addParameter("file1", "文件名.txt", new java.io.File("c://boot.ini"));
? mpm.addParameter("file2", "再來一個文件.txt", new java.io.File("c://boot.ini"));
? hc.executeMethod(mpm);
? System.out.println(mpm.getResponseBodyAsString());
org.apache.commons.httpclient.HttpClient hc = new
????? org.apache.commons.httpclient.HttpClient();
? hc.getHostConfiguration().setHost("localhost", 80);
? org.apache.commons.httpclient.methods.MultipartPostMethod mpm = new
????? org.apache.commons.httpclient.methods.MultipartPostMethod("/upload.asp");
? mpm.addParameter("username", "username");
? mpm.addParameter("file1", "文件名.txt", new java.io.File("c://boot.ini"));
? mpm.addParameter("file2", "再來一個文件.txt", new java.io.File("c://boot.ini"));
? hc.executeMethod(mpm);
? System.out.println(mpm.getResponseBodyAsString());

package ?com.test;

import ?org.apache.commons.httpclient.Cookie;
import ?org.apache.commons.httpclient.HttpClient;
import ?org.apache.commons.httpclient.NameValuePair;
import ?org.apache.commons.httpclient.cookie.CookiePolicy;
import ?org.apache.commons.httpclient.cookie.CookieSpec;
import ?org.apache.commons.httpclient.methods.GetMethod;
import ?org.apache.commons.httpclient.methods.PostMethod;

public ? class ?FormLoginDemo? {
?
static ? final ?String?LOGON_STR? = ? " localhost " ;
?
static ? final ? int ?LOGON_PORT? = ? 8080 ;
?
?
public ? static ? void ?main(String[]?args)? throws ?Exception {
??HttpClient?client?
= ? new ?HttpClient();
??
??client.getHostConfiguration().setHost(LOGON_STR,?LOGON_PORT);
??
// ???模擬登錄頁面login.jsp->main.jsp
??PostMethod?post? = ? new ?PostMethod( " /a.jsp " );
??NameValuePair?name?
= ? new ?NameValuePair( " name " ,? " Id " );
??NameValuePair?pass?
= ? new ?NameValuePair( " password " ?,? " Id " );
??
??post.setRequestBody(
new ?NameValuePair[] {name,?pass} );
??
??
int ?status? = ?client.executeMethod(post);
??
??System.out.println(post.getResponseBodyAsString());
??post.releaseConnection();
??
??
// 查看cookie?信息
??CookieSpec?cookiespec? = ?CookiePolicy.getDefaultSpec();
??Cookie[]?cookies?
= ?cookiespec.match(LOGON_STR,?LOGON_PORT,? " / " ,? false ,?client.getState().getCookies());
??
??
if ?(cookies.length? == ? 0 ) {
???System.out.println(
" none " );
??}
else {
???
for ?( int ?i? = ? 0 ;?i < ?cookies.length;?i ++ ) {
????System.out.println(cookies[i].toString());
???}

??}

??
??
// 訪問所需的頁面main2.jsp
??GetMethod?get? = ? new ?GetMethod( " /b.jsp " );
??client.executeMethod(get);
??System.out.println(get.getResponseBodyAsString());
??get.releaseConnection();
??
?}

}

?

針對采用HttpClient 文件上傳 的其實和一般上傳一樣,將文件以流的形式作為請求的信息RequestBody 體即可

必須注意中文問題:

關(guān)于httpclient上傳文件是中文名稱的處理

在一個項目中,上傳文件采用httpclient來post文件,在測試中發(fā)現(xiàn)如果文件是中文名稱,上傳的文件是亂碼
經(jīng)過跟蹤發(fā)現(xiàn),原來在httpclient中進(jìn)行了編碼,為ASCII,所以為亂碼
org\apache\commons\httpclient\util包下EncodingUtil.Java

/**
*ConvertsthespecifiedstringtobytearrayofASCIIcharacters.
*
*@paramdatathestringtobeencoded
*@returnThestringasabytearray.
*
*@since3.0
*/
publicstaticbyte[]getAsciiBytes(finalStringdata){

if(data==null){
thrownewIllegalArgumentException("Parametermaynotbenull");
}

http://www.mscto.com

?

try{
returndata.getBytes("US-ASCII");
}catch(UnsupportedEncodingExceptione){
thrownewHttpClientError("HttpClientrequiresASCIIsupport");
}
}

解決方法:
1在接收端處理ascii數(shù)據(jù)
2重新編譯httpclient包,即更改上面的方法,改為iso8859-1或utf-8
這樣可以解決中文的問題

采用httpclient是一個很好的方法,可以給你其他的系統(tǒng)post數(shù)據(jù),好像Delphi中的indy控件,

httpclient上傳文件使用


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 曲阜市| 五指山市| 鄯善县| 藁城市| 嘉兴市| 屏东县| 浦城县| 麻栗坡县| 民县| 佛山市| 如皋市| 丰城市| 绥化市| 大足县| 和林格尔县| 西乌| 武邑县| 仙游县| 福贡县| 巴塘县| 墨玉县| 大悟县| 武平县| 哈巴河县| 建水县| 美姑县| 山丹县| 霞浦县| 巫溪县| 玉田县| 成武县| 东山县| 和林格尔县| 隆昌县| 白山市| 丹棱县| 宜都市| 日土县| 吉林省| 宣城市| 清新县|