public class QRCodeServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String qrtext = request.getParameter("qrtext");
ByteArrayOutputStream out = QRCode.from(qrtext).to(ImageType.PNG).stream();
response.setContentType("image/png");
response.setContentLength(out.size());
OutputStream outStream = response.getOutputStream();
outStream.write(out.toByteArray());
outStream.flush();
outStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
这是我用于生成 QRCode 的代码。我使用 outputStream 将 QRCode 显示到浏览器。但是如何将该 QRCode 保存为服务器上的图像文件并使用标签将其显示在另一个 html 文件中?我在这里使用 iText 库生成 QrCode。