Quantcast
Channel: Mendix Forum Questions
Viewing all articles
Browse latest Browse all 83189

Retrieving Images in Java

$
0
0

Hi all,

I'm building a custom request handler that retrieves an image. I've been able to get it working to the point where I can return the name of the image and other String values. Additionally, when I access the request handler from the browser, the below code doesn't return any errors and seems to work. But the file returned doesn't have any recognizable image extension and contains unrecognizable text, as if when you open an image in a text editor. How do I retrieve the image in a way that it can be viewed by the end user?

Furthermore, how can I retrieve both the thumbnail and full version of the image?

After referencing a couple of blog posts, this is the request handler I've been working with:

class GetImage extends RequestHandler {
@Override
  public void processRequest(IMxRuntimeRequest request, IMxRuntimeResponse response, String parameters) throws Exception
  {
    OutputStream outputStream = response.getOutputStream();

    String[] params = parameters.split("/");
    String imgGUID = params[0];

    response.setContentType("application/octet-stream");
    IContext context = Core.createSystemContext();

    IMendixObject image = Core.retrieveXPathQuery(context, "//System.FileDocument[id='" + imgGUID + "']").get(0);

    InputStream inputStream = Core.getFileDocumentContent(context, image);

    IOUtils.copy(inputStream, outputStream);
    IOUtils.closeQuietly(outputStream);
  }
}

I'm retrieiving from System.FileDocument because System.Image is a specialization of it, and also because I was following this blog post on how exacty to get the file content.


Viewing all articles
Browse latest Browse all 83189

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>