六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 52|回复: 0

HttpClient应用实例类

[复制链接]

升级  14.67%

74

主题

74

主题

74

主题

举人

Rank: 3Rank: 3

积分
244
 楼主| 发表于 2013-1-27 05:20:55 | 显示全部楼层 |阅读模式
相关测试代码:
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();
}

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表