use of com.jfinal.render.RenderException in project jfinal by jfinal.
the class QrCodeRender method render.
public void render() {
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/png");
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//去掉白色边框,极度重要,否则二维码周围的白边会很宽
hints.put(EncodeHintType.MARGIN, 0);
if (errorCorrectionLevel != null) {
hints.put(EncodeHintType.ERROR_CORRECTION, errorCorrectionLevel);
}
try {
// MultiFormatWriter 可支持多种格式的条形码,在此直接使用 QRCodeWriter,通过查看源码可知少创建一个对象
// BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
QRCodeWriter writer = new QRCodeWriter();
BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
// 经测试 200 X 200 大小的二维码使用 "png" 格式只有 412B,而 "jpg" 却达到 15KB
// format: "jpg"、"png"
MatrixToImageWriter.writeToStream(bitMatrix, "png", response.getOutputStream());
} catch (Exception e) {
throw new RenderException(e);
}
}
use of com.jfinal.render.RenderException in project jfinal by jfinal.
the class ActionHandler method handle.
/**
* handle
* 1: Action action = actionMapping.getAction(target)
* 2: new Invocation(...).invoke()
* 3: render(...)
*/
public final void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
if (target.indexOf('.') != -1) {
return;
}
isHandled[0] = true;
String[] urlPara = { null };
Action action = actionMapping.getAction(target, urlPara);
if (action == null) {
if (log.isWarnEnabled()) {
String qs = request.getQueryString();
log.warn("404 Action Not Found: " + (qs == null ? target : target + "?" + qs));
}
renderManager.getRenderFactory().getErrorRender(404).setContext(request, response).render();
return;
}
try {
Controller controller = action.getControllerClass().newInstance();
controller.init(request, response, urlPara[0]);
if (devMode) {
if (ActionReporter.isReportAfterInvocation(request)) {
new Invocation(action, controller).invoke();
ActionReporter.report(target, controller, action);
} else {
ActionReporter.report(target, controller, action);
new Invocation(action, controller).invoke();
}
} else {
new Invocation(action, controller).invoke();
}
Render render = controller.getRender();
if (render instanceof ForwardActionRender) {
String actionUrl = ((ForwardActionRender) render).getActionUrl();
if (target.equals(actionUrl)) {
throw new RuntimeException("The forward action url is the same as before.");
} else {
handle(actionUrl, request, response, isHandled);
}
return;
}
if (render == null) {
render = renderManager.getRenderFactory().getDefaultRender(action.getViewPath() + action.getMethodName());
}
render.setContext(request, response, action.getViewPath()).render();
} catch (RenderException e) {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
}
} catch (ActionException e) {
int errorCode = e.getErrorCode();
String msg = null;
if (errorCode == 404) {
msg = "404 Not Found: ";
} else if (errorCode == 401) {
msg = "401 Unauthorized: ";
} else if (errorCode == 403) {
msg = "403 Forbidden: ";
}
if (msg != null) {
if (log.isWarnEnabled()) {
String qs = request.getQueryString();
log.warn(msg + (qs == null ? target : target + "?" + qs));
}
} else {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
}
}
e.getErrorRender().setContext(request, response, action.getViewPath()).render();
} catch (Exception e) {
if (log.isErrorEnabled()) {
String qs = request.getQueryString();
log.error(qs == null ? target : target + "?" + qs, e);
}
renderManager.getRenderFactory().getErrorRender(500).setContext(request, response, action.getViewPath()).render();
}
}
use of com.jfinal.render.RenderException in project my_curd by qinyou.
the class PoiRender method render.
@Override
public void render() {
response.reset();
response.setHeader("Content-disposition", "attachment; filename=" + fileName);
response.setContentType(CONTENT_TYPE);
OutputStream os = null;
try {
os = response.getOutputStream();
PoiKit.with(data).sheetName(sheetName).headerRow(headerRow).headers(headers).columns(columns).cellWidth(cellWidth).export().write(os);
} catch (Exception e) {
throw new RenderException(e);
} finally {
try {
if (os != null) {
os.flush();
os.close();
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
Aggregations