HttpClient应用实例类

相关测试代码:

public class fileuploadclient {

private static String url = "http://localhost:8080/jsp/upload/upload.jsp";

public static void main(String[] args) throws IOException {
String fileNames = "我爱北京new;我爱奥运new;我爱中国new";
String filePaths = "C:\\temp\\upload\\我爱北京.txt;C:\\temp\\upload\\我爱奥运.jpg;C:\\temp\\upload\\我爱中国.docx";
//防呆(个数一致、不重复)
HttpClient client = new HttpClient();
MultipartPostMethod mPost = new MultipartPostMethod(url);
Vector fileName_Vec = new Vector();
String[] fileName_Arr = fileNames.split(";");
String[] filePath_Arr = filePaths.split(";");
int FN_Length = fileName_Arr.length;
NameValuePair[] NV_Pairs = new NameValuePair[FN_Length];
for (int i = 0; i < FN_Length; i++) {
String fileName = fileName_Arr[i];
String filePath = filePath_Arr[i];
//增静态参数
NV_Pairs[i]= new NameValuePair("text_" + i, fileName);
//增TestField
File file = new File(filePath);
mPost.addParameter("file_" + i, file);
}

mPost.setQueryString(NV_Pairs);

client.setConnectionTimeout(8000);
//如需验证须设置用户名和密码
// client.getState().setCredentials(new
// AuthScope("www.baidu.com", 80),
// new UsernamePasswordCredentials("username", "password"));
// Send any XML file as the body of the POST request

// Header header;
// header.setValue("Content-type", "text/xml; charset=gb2312");
// mPost.setRequestHeader(header);
System.out.println("queryString>>>" + mPost.getQueryString());

int statuSCOde1 = client.executeMethod(mPost);

System.out.println("statusLine>>>" + mPost.getStatusLine());
System.out.println(mPost.getResponseBodyAsString());

mPost.releaseConnection();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值