Show Image with XML

Malab29

Re: Show Image with XML

Post by Malab29 »

I have managed to get the same encoded data in Java and PHP :)

Here the test class I use to encode the image :

Code: Select all

package com.alcatel.debug;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.codec.binary.Base64;

public class Main {

	public Main(){
		// input file to read
		String inputFile = "C:\\Projets\\Alcatel\\XML_API\\Images de test\\terre2.jpg";
		
		// output file to write data
		String outputFile = "C:\\log\\test\\terre2_JAVA.txt";
		
		// read non-encoded data from file
		byte[] dataIn = null;
		try{
			dataIn = readByteFromFile( inputFile );
		} catch( Exception e ){
			e.printStackTrace();
		}
		
		// encode data
		byte[] encodedData = encode( dataIn );
		
		// write data into file
		try{
			writeByteIntoFile( outputFile, encodedData );
		} catch( Exception e ){
			e.printStackTrace();
		}
		
		System.out.println("Finish");
	}
	


	/**
	 * read the file byte by byte
	 * @param p_file
	 * @return
	 * @throws Exception
	 */
	public byte[] readByteFromFile( String p_filename ) throws Exception{
		//load
		FileInputStream l_file = null;
		try {
			l_file = new FileInputStream( p_filename );
		} catch (FileNotFoundException e) {
			throw e;
		}
		
		//read
		if( l_file == null ){
			System.out.println("FileInputStream null");
			throw new Exception( "FileInputStream null" );
		}
		else{
			byte[] l_array = null;
			try {
				l_array = new byte[l_file.available()];
				l_file.read( l_array );
				return l_array;
			} catch (IOException e) {
				throw e;
			}
		}
	}
	
	/**
	 * write byte by byte the given byte array into the given file
	 * @param p_filenameOut
	 * @param p_data
	 * @throws Exception
	 */
	public void writeByteIntoFile( String p_filenameOut, byte[] p_data ) throws Exception {
		FileOutputStream fout;
		try {
			fout = new FileOutputStream(p_filenameOut);
			fout.flush();
			fout.write( p_data );
			fout.close();
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		}
	}
	
	/**
	 * encode the given data byte array
	 * @param data
	 * @return
	 */
	public byte[] encode( byte[] data ){
		return Base64.encodeBase64( data );
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new Main();
	}
}
The Java lib. to encode data in Base64 can be found here : http://apache.cict.fr/commons/codec/bin ... ec-1.3.zip
Post Reply

Return to “XML Presentation Server & TAPI Server”