Category Archives: Java

Java – Convert String to InputStream

If you want to convert a String into InputStream, you can use the following piece of code.

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;

public class ConvertStringToInputStream {

	public static void main(String[] args) {
		try {
			// Convert the String into InputStream 
			String eurekaUrl = "https://ykyuen.wordpress.com";
			InputStream is = new ByteArrayInputStream(eurekaUrl.getBytes("UTF-8"));
			
			// Use Apache Commons IO to get the content of the InputStream
			System.out.println(IOUtils.toString(is, "UTF-8")); 
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

Continue reading Java – Convert String to InputStream

Java – Convert InputStream to String

Recently, i have created a simple Java Servlet server which could parse a request xml from the HTTP POST Request and then return the HTTP Response together with the response xml. In the server implementation, i need to convert an InputStream into a String. The following code shows you how to make it.

In this example, the program will read the data.txt file and print the content to the console. Continue reading Java – Convert InputStream to String

Java – Connect Windows Active Directory Through LDAP @ 1

The Java Naming and Directory Interface (JNDI) is the standard Java API for multiple naming and directory services such as Lightweight Directory Access Protocol (LDAP).

Active Directory is a group of network services like account authentication and it supports LDAP. Therefore, you can write a Java program using JNDI in order to obtain the account credentials in the Active Directory of a Windows Server. Here comes the example. Continue reading Java – Connect Windows Active Directory Through LDAP @ 1

Selenium – Integrate the Selenium Tests into Maven Build

In this article, i will show you how to integrate the Selenium tests into a Maven webapp project using the selenium-maven-plugin and cargo-maven2-plugin.

In this example, i used JUnit as the testing framework. There is no problem if you use TestNG to replace JUnit. This example is a simple Java webapp Maven project which only shows a simple html page (index.jsp). During the build, a Selenium Server is started with the webapp deployed in the Embedded Jetty. It then runs an Unit Test (Selenium Test) during the Maven integration-test life cycle. Here comes to the code.
Continue reading Selenium – Integrate the Selenium Tests into Maven Build