We can use the code below to convert the content of an InputStream into a String. At first, we use FileInputStream create to a stream to a file that going to be read. IOUtils.toString(InputStream input, String encoding) method gets the content of the InputStream and returns a string representation of it.
package org.kodejava.commons.io;
import org.apache.commons.io.IOUtils;
import java.io.InputStream;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
public class InputStreamToString {
public static void main(String[] args) throws Exception {
// Create an input stream for reading data.txt file content.
try (InputStream is = new FileInputStream("data.txt")) {
// Get the content of an input stream as a string using UTF-8
// as the character encoding.
String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
System.out.println(contents);
}
}
}
This example using the Apache Commons FileUpload library to create a simple application for uploading files. The program is divided into two parts, a form using JSP and a servlet for handling the upload process. To run the sample you need to download the Commons FileUpload and Commons IO get the latest stable version.
Commons FileUpload Demo
The first step is to create the upload form. The form contains two fields for selecting file to be uploaded and a submit button. The form should have an enctype attribute and the value is multipart/form-data. We use a post method and the submit process is handled by the upload-servlet as defined in the action attribute.
The second step is to create the servlet. The doPost method checks to see if the request contains a multipart content. After that we create a FileItemFactory, in this example we use the DiskFileItemFactory which is the default factory for FileItem. This factory creates an instance of FileItem and stored it either in memory or in a temporary file on disk depending on its content size.
The ServletFileUpload handles multiple files upload that we’ve specified in the form above sent using the multipart/form-data encoding type. The process of storing the data is determined by the FileItemFactory passed to the ServletFileUpload class.
The next steps is to parse the multipart/form-data stream by calling the ServletFileUpload.parseRequest(HttpServletRequest request) method. The parse process return a list of FileItem. After that we iterate on the list and check to see if FileItem representing an uploaded file or a simple form field. If it is represents an uploaded file we write the FileItem content to a file.
This example demonstrate how to use PropertyUtils.setMappedProperty() method to modify a Map typed property value of a bean. To set the property we need to pass bean instance, property name, map key and map value to PropertyUtils.setMappedProperty() method.
package org.kodejava.commons.beanutils;
import org.apache.commons.beanutils.PropertyUtils;
import org.kodejava.commons.beanutils.support.Track;
import org.kodejava.commons.beanutils.support.Record;
import java.util.HashMap;
import java.util.Map;
public class PropertySetMappedExample {
public static void main(String[] args) {
// Create an instance of Recording bean.
Record record = new Record();
record.setId(1L);
record.setTitle("Introduction");
// Create a map to hold record tracks.
Map<String, Track> tracks = new HashMap<>();
tracks.put("track-one", new Track());
tracks.put("track-two", new Track());
tracks.put("track-three", new Track());
record.setMapTracks(tracks);
try {
// We add another tracks to the record track using
// a PropertyUtils.setMappedProperty() method.
PropertyUtils.setMappedProperty(record,
"mapTracks", "track-four", new Track());
PropertyUtils.setMappedProperty(record,
"mapTracks", "track-five", new Track());
} catch (Exception e) {
e.printStackTrace();
}
tracks = record.getMapTracks();
System.out.println("New Track Numbers: " + tracks.size());
for (String key : tracks.keySet()) {
System.out.println(key + " = " + tracks.get(key));
}
}
}
In this example we show how to set the value of an indexed property. In the code below we modified the value of an array type. We’ll change the second colors of MyBean‘s colors property.
We do it in the same way as we are using the PropertyUtils.setSimpleProperty method. For indexed property we use the PropertyUtils.setIndexedProperty method and passes four arguments, they are the instance of bean to be manipulated, the indexed property name, the index to be changes and finally the new value.
The Commons BeanUtils component provides a class called PropertyUtils that supplies method for manipulating a bean such as our Track class below. For this demo we use a Track class that have a property called id, title and duration.
To set the value of a bean we can use the PropertyUtils.setProperty() method. This method ask for the bean instance whose property value to be set, the property name and the value.
package org.kodejava.commons.beanutils.support;
public class Track {
private Long id;
private String title;
private Integer duration;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getDuration() {
return duration;
}
public void setDuration(Integer duration) {
this.duration = duration;
}
@Override
public String toString() {
return "Track{" +
"id=" + id +
", title='" + title + '\'' +
", duration=" + duration +
'}';
}
}
Some exceptions could be thrown by this method, so we need to handle the IllegalAccessException, InvocationAccessException and NoSuchMethodException. To make the code simple we will just catch it as java.lang.Exception.
These exceptions could happen if we don’t have access to the property, or the bean’s accessor throws an exception or if the method we tried to manipulate doesn’t exist.