usmanazizgroupdocs
4/15/2016 - 10:36 AM

SaveAsFile.java

// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-Java
/**
 * This method writes input stream to output file
 * 
 * @param fileName
 *            name of the output file
 * @param inputStream
 *            input stream to be written in output file
 */
public static void saveAsFile(String fileName, InputStream inputStream) {

	try {

		// Create stream for output file
		OutputStream outputStream = new FileOutputStream(outputPath + fileName);
		int read = 0;
		byte[] bytes = new byte[1024];

		// Write bytes into output stream
		while ((read = inputStream.read(bytes)) != -1) {
			outputStream.write(bytes, 0, read);
		}
		outputStream.close();
		outputStream.flush();
	} catch (Exception exp) {
		System.out.println("Exception: " + exp.getMessage());
		exp.printStackTrace();
	}
}