httpclient应用

[b][size=medium]

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
import org.apache.commons.httpclient.methods.multipart.StringPart;

public class FileUploadCmd {
public static void main(String[] args) throws HttpException, IOException{

String fileurl ="/webadmin/module/zipupload/fileUploadFinish.jsp";
String LOGON_STR = ZipUploadConfig.getProperty("LOGON_STR");
int LOGON_PORT = new Integer(ZipUploadConfig.getProperty("LOGON_PORT")).intValue();
/**
* @function:login in
* @param username,password,url
*/
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost(LOGON_STR, LOGON_PORT);
PostMethod post = new PostMethod("/login_***.jsp");
NameValuePair username = new NameValuePair("username",ZipUploadConfig.getProperty("USERNAME"));
NameValuePair password = new NameValuePair("password",ZipUploadConfig.getProperty("PASSWORD"));
NameValuePair url = new NameValuePair("url",parameter);
post.setRequestBody(new NameValuePair[]{username, password,url});//传递参数
try{
client.executeMethod(post);
}catch(Exception e){
System.out.println("Login server error");
return;
}
finally{
post.releaseConnection();
}

//cookie
/*CookieSpec cookiespec = CookiePolicy.getDefaultSpec();
Cookie[] cookies = cookiespec.match(LOGON_STR, LOGON_PORT, "/", false, client.getState().getCookies());

if(cookies.length == 0) {
System.out.println("None");
}else{
for(int i = 0; i < cookies.length; i++) {
System.out.println(cookiespec.formatCookie(cookies[i]));
System.out.println(cookies[i].getExpiryDate());
}
}*/


//登录成功后,上传文件
/**
* @function:upload file
*/
PostMethod mPost = new PostMethod (fileurl);
File f = new File(paras[0]);
StringPart spcg = new StringPart("contentgroup",paras[1]);
StringPart spof = new StringPart("overload_flag",paras[2]);
StringPart spss = new StringPart("stylesheet",paras[3]);
StringPart spt = new StringPart("template",paras[4]);
StringPart spct = new StringPart("contenttype",paras[5]);
String result = "";
StringBuffer resultBuffer = new StringBuffer();
try{
FilePart fp = new FilePart(f.getName(),f);
MultipartRequestEntity mrp = new MultipartRequestEntity(new Part[]{fp,spcg,spof,spss,spt,spct},mPost.getParams());
mPost.setRequestEntity(mrp);
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
client.executeMethod(mPost);
//LogContent = mPost.getResponseBodyAsStream();
BufferedReader in = new BufferedReader(new InputStreamReader(mPost.getResponseBodyAsStream(), mPost.getResponseCharSet()));
String inputLine = null;
while ((inputLine = in.readLine())!= null){
resultBuffer.append(inputLine);
resultBuffer.append("\n");
}
in.close();
result = resultBuffer.toString();
if(result.equals(""))
System.out.println("login failed");
//else
//System.out.println(result);
// LogFlag=true;
}catch(Exception e){
System.out.println("file can not found");
}
finally{
mPost.releaseConnection();
}
}
}

[/size][/b]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值