1.通过HttpClient发送请求登录系统
public static boolean login(HttpClient client,String httpUrl,String userId) throws HttpException, IOException{
boolean result = false;
// HttpClient client = new HttpClient();
client.setTimeout(5000);//5秒超时
client.setConnectionTimeout(5000);
PostMethod postMethod = new PostMethod(httpUrl);
NameValuePair userIdPair = new NameValuePair("userId", userId);
postMethod.setRequestBody(new NameValuePair[] { userIdPair });
int httpStat = client.executeMethod(postMethod);
if (httpStat == HttpStatus.SC_OK) {
result = true;
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
}
//String loginhtml = postMethod.getResponseBodyAsString();
//System.out.println("login:" + loginhtml);
postMethod.releaseConnection();
return result;
}
2.使用HttpClient发送HTTP请求发送文件
client.setTimeout(300000);//5分钟超时
client.setConnectionTimeout(300000);
PostMethod postMethod = new PostMethod(httpUrl);
// 设置最大连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
postMethod.setParameter("Connection", "Keep-Alive");
postMethod.getParams().setContentCharset("UTF-8");
postMethod.getParams().setUriCharset("UTF-8");
List<Part> parts = new ArrayList<Part>();//封装Form表单
Iterator<String> iter = strParts.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = (String) strParts.get(key);
StringPart part = new StringPart(key, value, "UTF-8");
parts.add(part);
}
File file = new File(fileName);
FilePart fp = new FilePart("uploadFile", file.getName(), file, null, "UTF-8");
parts.add(fp);
MultipartRequestEntity requestEntity = new MultipartRequestEntity(
parts.toArray(new Part[parts.size()]), postMethod.getParams());
postMethod.setRequestEntity(requestEntity);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
int httpStat = client.executeMethod(postMethod);
String responseCode = postMethod.getResponseBodyAsString();
//System.out.println(responseCode);
if (httpStat == HttpStatus.SC_OK) {
result = responseCode;
}else{
result = "1";
}
postMethod.releaseConnection();
3.解析HttpClient的请求
request.setCharacterEncoding("UTF-8");
String responseCode ="0";//远程调用时返回的响应码,0成功 1失败 2上传文件超出限制
String userId = "";
String syncType = request.getParameter("syncType");
String exportCode = "";
String fileName = "";
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(8 * 1024);// 设置缓冲区大小为4kb
String tempFile = request.getRealPath("/") + "data" + File.separator + "temp" + File.separator;
File tempDir = LoaderUtil.createDirectory(tempFile);
factory.setRepository(tempDir);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(Constants.UPLOAD_FILE_MAXSIZE);// 设置最大文件大小
List<FileItem> fileItems = upload.parseRequest(request);
Iterator<FileItem> iterator = fileItems.iterator();
while (iterator.hasNext()) {
FileItem fileItem = iterator.next();
String itemName = fileItem.getFieldName();
if ("syncType".equals(itemName)) {
syncType = fileItem.getString();
} else if ("userId".equals(itemName)) {
userId = fileItem.getString();
} else if ("uploadFile".equals(itemName)) {
if(fileItem.getSize()<=0){
responseCode = "1";//上传失败
}else if(fileItem.getSize()>Constants.UPLOAD_FILE_MAXSIZE){
responseCode = "2";//数据压缩包超出50M
}else{
File fullFile = new File(fileItem.getName());
fileName = fullFile.getName();
File saveFile = new File(baseDir, fileName);
fileItem.write(saveFile);
}
}
}
public static boolean login(HttpClient client,String httpUrl,String userId) throws HttpException, IOException{
boolean result = false;
// HttpClient client = new HttpClient();
client.setTimeout(5000);//5秒超时
client.setConnectionTimeout(5000);
PostMethod postMethod = new PostMethod(httpUrl);
NameValuePair userIdPair = new NameValuePair("userId", userId);
postMethod.setRequestBody(new NameValuePair[] { userIdPair });
int httpStat = client.executeMethod(postMethod);
if (httpStat == HttpStatus.SC_OK) {
result = true;
Cookie[] cookies = client.getState().getCookies();
client.getState().addCookies(cookies);
}
//String loginhtml = postMethod.getResponseBodyAsString();
//System.out.println("login:" + loginhtml);
postMethod.releaseConnection();
return result;
}
2.使用HttpClient发送HTTP请求发送文件
client.setTimeout(300000);//5分钟超时
client.setConnectionTimeout(300000);
PostMethod postMethod = new PostMethod(httpUrl);
// 设置最大连接超时时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
postMethod.setParameter("Connection", "Keep-Alive");
postMethod.getParams().setContentCharset("UTF-8");
postMethod.getParams().setUriCharset("UTF-8");
List<Part> parts = new ArrayList<Part>();//封装Form表单
Iterator<String> iter = strParts.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = (String) strParts.get(key);
StringPart part = new StringPart(key, value, "UTF-8");
parts.add(part);
}
File file = new File(fileName);
FilePart fp = new FilePart("uploadFile", file.getName(), file, null, "UTF-8");
parts.add(fp);
MultipartRequestEntity requestEntity = new MultipartRequestEntity(
parts.toArray(new Part[parts.size()]), postMethod.getParams());
postMethod.setRequestEntity(requestEntity);
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
int httpStat = client.executeMethod(postMethod);
String responseCode = postMethod.getResponseBodyAsString();
//System.out.println(responseCode);
if (httpStat == HttpStatus.SC_OK) {
result = responseCode;
}else{
result = "1";
}
postMethod.releaseConnection();
3.解析HttpClient的请求
request.setCharacterEncoding("UTF-8");
String responseCode ="0";//远程调用时返回的响应码,0成功 1失败 2上传文件超出限制
String userId = "";
String syncType = request.getParameter("syncType");
String exportCode = "";
String fileName = "";
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(8 * 1024);// 设置缓冲区大小为4kb
String tempFile = request.getRealPath("/") + "data" + File.separator + "temp" + File.separator;
File tempDir = LoaderUtil.createDirectory(tempFile);
factory.setRepository(tempDir);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(Constants.UPLOAD_FILE_MAXSIZE);// 设置最大文件大小
List<FileItem> fileItems = upload.parseRequest(request);
Iterator<FileItem> iterator = fileItems.iterator();
while (iterator.hasNext()) {
FileItem fileItem = iterator.next();
String itemName = fileItem.getFieldName();
if ("syncType".equals(itemName)) {
syncType = fileItem.getString();
} else if ("userId".equals(itemName)) {
userId = fileItem.getString();
} else if ("uploadFile".equals(itemName)) {
if(fileItem.getSize()<=0){
responseCode = "1";//上传失败
}else if(fileItem.getSize()>Constants.UPLOAD_FILE_MAXSIZE){
responseCode = "2";//数据压缩包超出50M
}else{
File fullFile = new File(fileItem.getName());
fileName = fullFile.getName();
File saveFile = new File(baseDir, fileName);
fileItem.write(saveFile);
}
}
}