Tag Archives: Apache Commons IO

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 – Get the content of inputStream from HttpServletRequest

Recently, i got a support case and it is about the inconsistency of the content length declared in the http header and the actual content length of the http request.

So i modified an existing program which will get the http request and manipulate it. I add some codes such that it will get the request inputStream and count it byte by byte before the request manipulation.
Continue reading Java – Get the content of inputStream from HttpServletRequest