使用jsp将动态生成的图像显示给浏览器 [英] Display dynamically generated image to the browser using jsp
问题描述
我正在使用jsp/servlets处理图像的小项目,因为我动态生成了一些图像(实际上我将两个图像共享解密为一个),解密后的图像必须直接显示给浏览器而不保存为文件在文件系统中.
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);
当我将其存储在文件系统中并使用<img>
显示时,它不会显示图像.重新加载时,它会显示图像.因此,IDE的后端工作存在问题.
任何帮助请...
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...
推荐答案
- 制作一个servlet来生成图像.
- 使用属性为src的html img标签作为生成资源的路径.
弹簧靴示例(QR码). Servlet
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
public class WebMvcConfig {
@Bean
public ServletRegistrationBean qrCodeServletRegistrationBean(){
ServletRegistrationBean qrCodeBean =
new ServletRegistrationBean(new QRCodeServlet(), "/qrcode");
qrCodeBean.setLoadOnStartup(1);
return qrCodeBean;
}
}
控制器:
String qrcodeServletPrefix = "http://localhost:8082/qrcode?url="
String encodedUrl = URLEncoder.encode("http://exmaple.com?param1=value1¶m2=value2", "UTF-8");
modelAndView.addObject("qrcodepage", qrcodeServletPrefix + encodedUrl);
modelAndView.setViewName("contracts/activateProcessVasResult");
activateProcessVasResult.jsp
activateProcessVasResult.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<img src="<c:url value='${qrcodepage}'/>" />
这篇关于使用jsp将动态生成的图像显示给浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!