2

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...

SKJ
  • 91
  • 1
  • 14
  • Why do you save the image on the file system in the first place, since it's exactly what you don't want to do? Write it to the HTTP response output stream instead, and set the appropriate content type. – JB Nizet Feb 19 '17 at 22:14
  • You may try to get final image bytestream into `base64` and embed it into the page, see [Embedding Base64 Images](http://stackoverflow.com/questions/1207190/embedding-base64-images) In this case you won't have to save image in filesystem – Anton Dovzhenko Feb 20 '17 at 00:52
  • @JB Nizet Can you provide some example code snippet related to my code presented above or any link?? – SKJ Feb 20 '17 at 17:34
  • http://stackoverflow.com/questions/2340406/how-to-retrieve-and-display-images-from-a-database-in-a-jsp-page. Your images are not coming from a database, but the principle is the same: you need to use ImageIO.write() to encode your BufferedImage to png (for example), and write the encoded image to the response output stream, with the proper content type, as explained in the link I just posted. – JB Nizet Feb 20 '17 at 17:45

1 Answers1

0
  1. Make a servlet to generate images.
  2. Use html img tag with attribute src, as a path to your genarated resource.

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("view");

view.jsp

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