Attach a file from a stream or byte array?

I am working on an webQuerySave agent for a photo gallary database. The agent will take an uploaded file and create a thumbnail to display in a view. I’ve got this agent working except that will be in a hosted environment and I don’t have access to the file system. I have worked around this by using streams and a byte array when I get the original image from the document but I can’t find any why other than to write the thumbnail to the file system to attach it to the document. The code for the agent is below. Right now the createThumbnail() writes to a file I would rather have it return a byte array or a stream that I could somehow attach to the document. If anyone has any ideas Please let me know…

import lotus.domino.*;

import java.io.*;

import java.awt.*;

import java.awt.Image;

import java.awt.Graphics2D;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class JavaAgent extends AgentBase {

public void NotesMain() {

	try {

		Session session = getSession();

		AgentContext agentContext = session.getAgentContext();

		// (Your code goes here)

		PrintWriter pw = getAgentOutput();

		Database db = agentContext.getCurrentDatabase();

		Document doc = agentContext.getDocumentContext();

		RichTextItem rtitem = doc.createRichTextItem("Thumbnail");

		EmbeddedObject obj = doc.getAttachment(doc.getItemValueString("Attachment"));

		if (obj != null) {

			int ch;

			pw.println("Found " + obj.getName());

			InputStream is = obj.getInputStream();

			BufferedInputStream bis = new BufferedInputStream(is);

			ByteArrayOutputStream baos = new ByteArrayOutputStream();

			while((ch = bis.read()) != -1){

				baos.write(ch);

			}

			byte[] imageData = baos.toByteArray();

			baos.close();

			bis.close();

			String file = "c:\\thumb.jpg";

			createThumbnail(imageData, file, 120);	

			

rtitem.embedObject(EmbeddedObject.EMBED_ATTACHMENT,null, file, null);

			File f = new File(file);

			f.delete();

			

			

		 }else{

		 	pw.println("Not Found ");

		}

	}catch(Exception e) {

		e.printStackTrace();

	}

}



public static void createThumbnail(byte[] imageData, String thumb, int maxDim) {

	try {

		// Get the image from the byte array.

		Image inImage = new ImageIcon(imageData).getImage();

		// Determine the scale.

		double scale = (double)maxDim/(double)inImage.getHeight(null);

		if (inImage.getWidth(null) > inImage.getHeight(null)) {

			scale = (double)maxDim/(double)inImage.getWidth(null);

		}



		// Determine size of new image.

		//One of them

		// should equal maxDim.

		int scaledW = (int)(scale*inImage.getWidth(null));

		int scaledH = (int)(scale*inImage.getHeight(null));



		// Create an image buffer in

		//which to paint on.

		BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);



		// Set the scale.

		AffineTransform tx = new AffineTransform();



		// If the image is smaller than

		//the desired image size,

		// don't bother scaling.

		if (scale < 1.0d) {

			tx.scale(scale, scale);

		}



		// Paint image.

		Graphics2D g2d = outImage.createGraphics();

		g2d.drawImage(inImage, tx, null);

		g2d.dispose();



		// JPEG-encode the image

		//and write to file.

		OutputStream os = new FileOutputStream(thumb);

		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);

		encoder.encode(outImage);

		os.close();

	} catch (IOException e) {

		e.printStackTrace();

	}

}

}

Subject: I got !!! - attach a file from a stream or byte array?

my main problem was I didn’t have access to the file system and when I got a handle on the Embeded file it writes a temp file to the file system (this fact is documented in the help file). so what I did was add a body field to the form set it to store content as html and mime and then get a handle on the MIMEEntity with the file attachment. Then I copy it and resize it then create a new MIMEEntity with the new attachment and save the document. It works great It will generate a thumbnail for both gif and jpg images… email me if you would like a copy of a sample db I’m working on…

Subject: RE: I got !!! - attach a file from a stream or byte array?

That sounds like a good solution to a thorny problem. Nice one!