jeans 发表于 2013-1-27 05:20:55

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;
for (int i = 0; i < FN_Length; i++) {
String fileName = fileName_Arr;
String filePath = filePath_Arr;
//增静态参数
NV_Pairs= 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();
}

}
页: [1]
查看完整版本: HttpClient应用实例类