论坛首页 入门技术论坛

HttpClient应用实例类

浏览 2277 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-06-08  
SOA

相关测试代码:

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();
}

}

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics