HttpClient post 上传文件 笔记
来源:互联网 发布:苹果越狱软件源 编辑:程序博客网 时间:2024/06/14 06:40
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();
}
- HttpClient post 上传文件 笔记
- HttpClient post 上传文件 笔记
- HttpClient post 上传文件 笔记
- HttpClient通过Post上传文件
- HttpClient 通过Post 上传文件。
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- HttpClient用POST上传文件
- HttpClient通过Post上传文件
- HttpClient通过Post上传文件
- httpClient中post上传文件乱码
- Android HttpClient post MultipartEntity - Android 上传文件
- iOS POST上传文件~笔记
- HttpClient通过post上传文件和提交参数
- HttpClient通过post上传文件和提交参数
- 09 - get请求、post请求、httpclient、文件上传
- Oracle入门心得
- Linux启动中setup_arch分析
- oracle cbo rbo 详解
- 一步一步学Linq to sql(四):查询句法
- 优秀的程序员,不是优秀的构架师,设计师
- HttpClient post 上传文件 笔记
- 一步一步学Linq to sql(五):存储过程
- 内核态fs寄存器指向结构KPCR,方便反汇编
- 一步一步学Linq to sql(六):探究特性
- 关于Activity的onSaveInstanceState调用时机的说明
- js中时间的比较setFullYear(, ,)
- volatile关键字
- 第十二章 1 java输入流和输出流
- plsql知识梳理-基础操作