httpclient应用
**
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*));
System.out.println(cookies*.getExpiryDate());
}
}*/
//登录成功后,上传文件
/**
* @function:upload file
*/
PostMethod mPost = new PostMethod (fileurl);
File f = new File(paras);
StringPart spcg = new StringPart("contentgroup",paras);
StringPart spof = new StringPart("overload_flag",paras);
StringPart spss = new StringPart("stylesheet",paras);
StringPart spt = new StringPart("template",paras);
StringPart spct = new StringPart("contenttype",paras);
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();
}
}
}
**