httpclient笔记(二)

不多说了,直接上示例:

 

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");
  }
 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值