我正在使用jsp / servlets做一个带图像的小项目。我动态生成一些图像(实际上我将两个图像共享解密为一个)。解密图像必须直接显示到浏览器而不保存作为文件系统中的文件。
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的后端工作存在问题。
任何帮助请...
答案 0 :(得分:0)
春季靴子示例(QR码)。 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
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<img src="<c:url value='${qrcodepage}'/>" />