How to Download BLOB file from Oracle Database

Hi Guys,

I have encrypted images saved in Oracle database, and through web interface developed using Java I can download the images.

I have a decryption key and I would like to do the same thing direct from oracle database but using uipath.

===============================
Below is the table used.

===============================
The java code that is currently working.

import javax.servlet.ServletContext;
import com.sun.media.jai.codec.ImageEncoder;
import javax.servlet.ServletOutputStream;
import java.awt.image.RenderedImage;
import java.io.InputStream;
import tz.co.infowise.registration.model.document.DocumentVO;
import java.io.IOException;
import com.sun.media.jai.codec.ImageEncodeParam;
import java.io.OutputStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.JPEGEncodeParam;
import javax.media.jai.JAI;
import com.sun.media.jai.codec.SeekableStream;
import java.io.ByteArrayInputStream;
import tz.co.infowise.utils.encrypt.AESEncryptor;
import tz.co.infowise.registration.model.utils.ConfigurationWrapper;
import tz.co.infowise.registration.ServiceLocator;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServlet;

public class DisplayImageServlet extends HttpServlet
{
    protected void service(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
        try {
            final String docId = request.getParameter("d");
            final String action = request.getParameter("ac");
            final Long imageId = Long.parseLong(docId);
            String fileName = null;
            byte[] df = null;
            final byte[] decryptedData = ServiceLocator.instance().getDocumentService().loadImage(imageId);
            try {
                AESEncryptor.setKey(ConfigurationWrapper.getKeyString(), ConfigurationWrapper.getKeyIteration());
                df = AESEncryptor.decrypt(decryptedData);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            final DocumentVO vo = ServiceLocator.instance().getDocumentService().load(imageId);
            fileName = vo.getMsisdn() + "." + vo.getFormat().toLowerCase();
            if ("Preview".equals(action)) {
                if (fileName.contains(".tif")) {
                    response.setContentType("image/jpg");
                    final InputStream ins = new ByteArrayInputStream(df);
                    final SeekableStream s = SeekableStream.wrapInputStream(ins, true);
                    final RenderedImage objImage = (RenderedImage)JAI.create("stream", (Object)s);
                    final JPEGEncodeParam encodeParam = new JPEGEncodeParam();
                    final ServletOutputStream out = response.getOutputStream();
                    final ImageEncoder encoderImage = ImageCodec.createImageEncoder("JPEG", (OutputStream)out, (ImageEncodeParam)encodeParam);
                    encoderImage.encode(objImage);
                }
                else {
                    final ServletContext context = this.getServletContext();
                    String mimeType = context.getMimeType(fileName);
                    if (mimeType == null) {
                        mimeType = "application/octet-stream";
                    }
                    response.setContentType(mimeType);
                    if (df != null) {
                        response.setContentLength(df.length);
                        response.getOutputStream().write(df);
                    }
                }
            }
            else {
                final String headerKey = "Content-Disposition";
                final String headerValue = String.format("attachment; filename=\"%s\"", fileName);
                response.setHeader(headerKey, headerValue);
                final OutputStream outStream = (OutputStream)response.getOutputStream();
                final byte[] buffer = new byte[4096];
                int bytesRead = -1;
                final InputStream inStream = new ByteArrayInputStream(df);
                while ((bytesRead = inStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, bytesRead);
                }
                inStream.close();
                outStream.close();
            }
        }
        catch (IOException ex) {
            ex.printStackTrace();
            throw ex;
        }
        catch (Exception e2) {
            e2.printStackTrace();
        }
    }
}

Hello @wilbardmtei!

It seems that you have trouble getting an answer to your question in the first 24 hours.
Let us give you a few hints and helpful links.

First, make sure you browsed through our Forum FAQ Beginner’s Guide. It will teach you what should be included in your topic.

You can check out some of our resources directly, see below:

  1. Always search first. It is the best way to quickly find your answer. Check out the image icon for that.
    Clicking the options button will let you set more specific topic search filters, i.e. only the ones with a solution.

  2. Topic that contains most common solutions with example project files can be found here.

  3. Read our official documentation where you can find a lot of information and instructions about each of our products:

  4. Watch the videos on our official YouTube channel for more visual tutorials.

  5. Meet us and our users on our Community Slack and ask your question there.

Hopefully this will let you easily find the solution/information you need. Once you have it, we would be happy if you could share your findings here and mark it as a solution. This will help other users find it in the future.

Thank you for helping us build our UiPath Community!

Cheers from your friendly
Forum_Staff