public static String TestParam() throws HttpException, IOException {
String url = "http://localhost:8080/IphoneTest/rece.do?method=receive";
HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod(url);
// 填入各個表單域的值
NameValuePair[] data = { new NameValuePair("username", "username"),
new NameValuePair("passwd", "123456") };
// 將表單的值放入postMethod中
postMethod.setRequestBody(data);
// 執行postMethod
int status = client.executeMethod(postMethod);
System.out.println(status + "--------狀態------------");
if (status!= HttpStatus.SC_OK) {
System.out.println("--------fail-------");
} else if (status== HttpStatus.SC_OK) {
String str=postMethod.getResponseBodyAsString();
System.out.println("---------服務器返回值---------:"+str);
}
return null;
}
//上傳帶附件的參數:
public static String ParamFile() {
String path = "D:\\KuGou\\b.txt";
File file = new File(path);
if (!file.exists()) {
return "文件不存在!";
}
String url = "http://localhost:8080/IphoneTest/rece.do?method=receive";
PostMethod filePost = new PostMethod(url);
try {
//FilePart:用來上傳文件的類
FilePart fp = new FilePart("file", file); //Part:類專門用來上傳文件,其子類 ,FilePart:用來上傳文件的類 StringPart:普通的文本參數
System.out.println("---" + fp);
fp.setContentType(MIME.getMIME(file.getName().substring(file.getName().lastIndexOf(".")+1)));
//StringPart:普通的文本參數
StringPart uname=new StringPart("username", "aa");
StringPart pass=new StringPart("password", "123456");
Part[] parts = {uname,pass,fp};
//對于MIME類型的請求,httpclient建議全用MulitPartRequestEntity進行包裝
MultipartRequestEntity mre=new MultipartRequestEntity(parts,filePost.getParams());
filePost.setRequestEntity(mre);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 設置連接時間
int status = client.executeMethod(filePost);
System.out.println(status + "--------------");
if (status != HttpStatus.SC_OK) {
System.out.println(status + "--------------fail----");
} else if (status == HttpStatus.SC_OK) {
String str = "";
str = filePost.getResponseBodyAsString();
System.out.println(filePost.getResponseBodyAsString()+ "---------服務器返回值---------");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//同時上傳幾個附近
public static String ParamFiles() {
String path = "D:\\KuGou\\b.txt";
String path2 = "D:\\KuGou\\a.txt";
File file = new File(path);
File file2=new File(path2);
if (!file.exists()) {
return "文件不存在!";
}
if (!file2.exists()) {
return "文件不存在!";
}
String url = "http://localhost:8080/IphoneTest/rece.do?method=receive";
PostMethod filePost = new PostMethod(url);
try {
//FilePart:用來上傳文件的類
FilePart fp = new FilePart("file", file); //Part:類專門用來上傳文件,其子類 ,FilePart:用來上傳文件的類 StringPart:普通的文本參數
FilePart fp2=new FilePart("file2", file2);
System.out.println("---" + fp);
fp.setContentType(MIME.getMIME(file.getName().substring(file.getName().lastIndexOf(".")+1)));
//StringPart:普通的文本參數
StringPart uname=new StringPart("username", "aa");
StringPart pass=new StringPart("password", "123456");
Part[] parts = {uname,pass,fp,fp2};
MultipartRequestEntity mre=new MultipartRequestEntity(parts,filePost.getParams());
filePost.setRequestEntity(mre);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 設置連接時間
int status = client.executeMethod(filePost);
System.out.println(status + "--------------");
if (status != HttpStatus.SC_OK) {
System.out.println(status + "--------------fail----");
} else if (status == HttpStatus.SC_OK) {
String str = "";
str = filePost.getResponseBodyAsString();
System.out.println(filePost.getResponseBodyAsString()+ "---------服務器返回值---------");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
測試:
public static void main(String[] args) throws HttpException, IOException {
// 上傳一般的參數
TestParam();
ParamFile();
ParamFiles();
}