Categories
Lotus

How to get a resource from a java agent in Lotus

In this post I explain how to get a resource, for example an image, from a java agent even if this file is not saved on the file system.
The agent is tested in lotus client 8.5.x.

    1. Create a java agent

 

    1. Import an image as a resource by using the button Import -> Resource

 

    1. Edit the class JavaAgent:
      import lotus.domino.*;
      
      import java.awt.image.BufferedImage;
      import java.io.ByteArrayInputStream;
      import java.io.File;
      import java.io.InputStream;
      import javax.imageio.ImageIO;
      import org.apache.commons.io.IOUtils;
      
      public class JavaAgent extends AgentBase {
      
      	public void NotesMain() {
      
      		try {
      			Session session = getSession();
      			AgentContext agentContext = session.getAgentContext();
      
      			// (Your code goes here)
      			
      			// first example: from image resource to bytes
      			// you need org.apache.commons.io.IOUtils
      			InputStream is = getClass().getClassLoader().getResourceAsStream("myPicture.jpg");
      			byte[] bytes = IOUtils.toByteArray(is);
      
      			// second example: from image resource to file
      			InputStream in = (ByteArrayInputStream)getClass().getClassLoader().getResourceAsStream("myPicture.jpg");
      			BufferedImage img = ImageIO.read(in);   
      			ImageIO.write(img, "jpg", new File("c:\\myPicture.jpg"));
      
      		} catch(Exception e) {
      			e.printStackTrace();
      		}
      	}
      

 

In the first example the image is converted to bytes (you need to import org.apache.commons.io.IOUtils and to import the relative .jar file under Archive)
In the second example the image is saved on the disk.

2 replies on “How to get a resource from a java agent in Lotus”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.