httpclient 노트 (2)

4686 단어 xmlFP
더 이상 말 하지 않 겠 습 니 다. 직접 예 를 들 어 보 겠 습 니 다.
 
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("
NAME: " + 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");
  }
 }

좋은 웹페이지 즐겨찾기