46600

Display dynamically generated image to the browser using jsp

I am doing a small project with images using jsp/servlets.In that I generate some image dynamically(actually I'll decrypt two image shares as one).That decrypted image must be displayed directly to browser without saving it as file in filesystem.

Crypting c=new Crypting(); BufferedImage imgKey; BufferedImage imgEnc; imgKey = ImageIO.read(new File("E:/Netbeans Projects/banking/web/Key.png")); imgEnc=ImageIO.read(new File("E:/Netbeans Projects/banking/build/web/upload/E.png")); BufferedImage imgDec=Crypting.decryptImage(imgKey,imgEnc);

When I store it in filesystem and display it using <img> it does not show the image.When reloaded it shows the image.So it is problem with the backend work of IDE. Any help pls...

Answer1:

<ol> <li>Make a servlet to generate images.</li> <li>Use html img tag with attribute src, as a path to your genarated resource.</li> </ol>

Example in spring boot (QR Codes). Servlet

public class QRCodeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String url = req.getParameter("url"); String format = req.getParameter("format"); QRCodeFormat formatParam = StringUtils.isNotEmpty(format) && format.equalsIgnoreCase("PDF") ? QRCodeFormat.PDF : QRCodeFormat.JPG; if(formatParam.equals(QRCodeFormat.PDF)) resp.setContentType("application/pdf"); else resp.setContentType("image/jpeg"); if(StringUtils.isNotBlank(url)) { ByteArrayOutputStream stream = QRCodeService.getQRCodeFromUrl(url, formatParam); stream.writeTo(resp.getOutputStream()); } } }

Configuration:

@Configuration public class WebMvcConfig { @Bean public ServletRegistrationBean qrCodeServletRegistrationBean(){ ServletRegistrationBean qrCodeBean = new ServletRegistrationBean(new QRCodeServlet(), "/qrcode"); qrCodeBean.setLoadOnStartup(1); return qrCodeBean; } }

Conroller:

String qrcodeServletPrefix = "http://localhost:8082/qrcode?url=" String encodedUrl = URLEncoder.encode("http://exmaple.com?param1=value1&param2=value2", "UTF-8"); modelAndView.addObject("qrcodepage", qrcodeServletPrefix + encodedUrl); modelAndView.setViewName("contracts/activateProcessVasResult");

activateProcessVasResult.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <img src="<c:url value='${qrcodepage}'/>" />

Recommend

  • How to filter a range by first Character in a cell?
  • Using DOM parser in Android
  • Where and How To Define An Application Property? - JHIpster
  • How to distinguish field that requires null=True when blank=True is set in Django models?
  • How to attach php documentation in eclipse
  • Find substring in string using locale
  • Majority function in SQL
  • PL/SQL on SquirreL SQL Client 3.7.1
  • Extjs, handling success or failure when doing a standard submit in a form
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Python PIL to extract number from image
  • Outputting SharePoint Hyperlink Column as URL
  • Struts 2 TextField Tag with an attribute and no value
  • Disable Kendo Autocomplete
  • Debugging VB6 Code From Visual Studio 2010
  • PLSQL: Get number of records updated vs inserted when a merge statement is used
  • How to view images from protected folder with php?
  • Automatically associate new Sonar project with custom quality profile and quality gate
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Assign variable to the value in HTML
  • Get one-time binding to work for ng-if
  • HTML download movie download link
  • Volley JsonObjectRequest send headers in GET Request
  • Importing jscolor library in angular 2
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Load html files in TinyMce
  • How can I remove ASP.NET Designer.cs files?
  • Add sale price programmatically to product variations
  • JaxB to read class hierarchy
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • How to load view controller without button in storyboard?