不多说了,直接上示例:
A服务器:
msURL为:http://192.168.7.203:8080
/**
* 发送文件到另一台服务器B
*
* @param File file 附件
* @param serviceType服务类型
* @param spId id
* @return
*/
public String sendApply(File file, String serviceType, String spId) {
String fromAgentResult = "";
HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod(msUrl+"?serviceType="+serviceType+"&spId="+spId+"&type=menu");
// MultipartPostMethod filePost = new MultipartPostMethod(msUrl);
// 若上传的文件比较大 , 可在此设置最大的连接超时时间
client.getHttpConnectionManager(). getParams().setConnectionTimeout(8000);
try {
FilePart fp = new FilePart(file.getName(), file);
MultipartRequestEntity mrp= new MultipartRequestEntity(new Part[]{fp}, filePost.getParams());
filePost.setRequestEntity(mrp);
//使用系统提供的默认的恢复策略
filePost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
int httpStat = client.executeMethod(filePost);
System.out.println("httpStat----"+httpStat);
if (!(httpStat == HttpStatus.SC_OK)) {
fromAgentResult = "connected fail:" + httpStat;
} else if (httpStat == HttpStatus.SC_OK) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder
.parse(filePost.getResponseBodyAsStream());
doc.normalize();
// NodeList optypeNL= doc.getElementsByTagName("optype");
NodeList resultNL = doc.getElementsByTagName("result");
// fromAgentOptype=
// optypeNL.item(0).getFirstChild().getNodeValue();
fromAgentResult = resultNL.item(0).getFirstChild()
.getNodeValue();
System.out.println("发送请求完毕,接收状态:"+fromAgentResult);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
filePost.releaseConnection();
return fromAgentResult;
}
B服务器接收:
boolean bl = false;
System.out.println("接收文件开始--------------");
String serviceType = request.getParameter("serviceType");
String spId = request.getParameter("spId");
String type = request.getParameter("type");
if (type.equals("menu")) {
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(1000000);
List fileItems;
try {
fileItems = fu.parseRequest(request);
Iterator itr = fileItems.iterator();
while (itr.hasNext()) {
FileItem fi = (FileItem) itr.next();
if (!fi.isFormField()) {
System.out.println("\nNAME: " + fi.getName());
System.out.println("SIZE: " + fi.getSize());
try {
String newFileName = msu.updateName(Integer
.parseInt(serviceType), spId);
System.out.println("newFileName---------------"
+ newFileName);
File fNew = new File(applyFilePath, newFileName);
fi.write(fNew);
bl = msu.acceptApply(Integer.parseInt(serviceType),
spId, newFileName);
System.out.println(fNew.getAbsolutePath());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("Field =" + fi.getFieldName());
}
}
} catch (FileUploadException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//下面是返回执行结果
msu.responseToAS(response, bl);
System.out.println("接收申请处理完毕,状态:" + bl);
return null;
//msu.responseToAS
public void responseToAS(HttpServletResponse response, boolean synSuccess) {
try {
PrintWriter out = response.getWriter();
if (synSuccess)
out
.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><result>ok</result></root>");
else
out
.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><result>fail</result></root>");
} catch (IOException e) {
e.printStackTrace();
System.out.println("responseToAS--error");
}
}