Send Embedded Object as File attachment to 3rd party Web Service


	public String sendFileAttachment(EmbeddedObject eo) {
		// General Variables
		String result = "";
		String fileName = "";

		// Web Service Variables
		URL url = null;
		HttpURLConnection httpConn = null;
		InputStream inputStream = null;
		OutputStream outputStream = null;
		BufferedReader br = null;
		byte[] buffer = null;
		int bytesRead = 0;

		// TODO: Modify the Endpoint URL and Request Type as needed
		String endpointURL = "http://localhost/uploadFile";
		String requestType = "POST";

		try {
			fileName = eo.getName();

			// Setup Web Service
			url = new URL(endpointURL);
			httpConn = (HttpURLConnection) url.openConnection();
			httpConn.setRequestMethod(requestType);
			httpConn.setDoOutput(true);
			httpConn.setDoInput(true);

			// This example is specific to Agilit-e.com. This is an example of a 3rd party service you would connect to
			// TODO: Set the Header Properties based on the 3rd Party Web Service requirements
			httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			httpConn.setRequestProperty("Authorization", "agilite_token_key");
			httpConn.setRequestProperty("fileName", fileName);

			// Prep File for Sending
			outputStream = httpConn.getOutputStream();
			inputStream = eo.getInputStream();
			buffer = new byte[4096];
			bytesRead = -1;
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, bytesRead);
			}
			outputStream.flush();

			// Check response of Web Service Request
			System.out.println("Response Code = " + httpConn.getResponseCode());
			System.out.println("Response Message = " + httpConn.getResponseMessage());

			if (httpConn.getResponseCode() == 200) {
				// Success: Read Result from Web Service
				br = new BufferedReader(new InputStreamReader((httpConn.getInputStream())));
				result = br.readLine();
			} else {
				// Web Service Failed. Return Code and Message
				result = httpConn.getResponseCode() + " - " + httpConn.getResponseMessage();
				System.out.println("Request Failed");
			}
		} catch (Exception e) {
			// Code Failed
			result = "Your Agent Failed";
			e.printStackTrace();
		}

		//Returns the result from the 3rd Party Web Service as a String
		return result;
	}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
No comments yetLogin first to comment...