You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
2.1 KiB

10 months ago
package com.dsideal.gw.Handler;
import cn.hutool.json.JSONObject;
import com.jfinal.handler.Handler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
10 months ago
import java.util.HashMap;
import java.util.Map;
10 months ago
public class RouterHandler extends Handler {
10 months ago
/**
* JSON
* @param res
* @param jo
*/
public void renderJson(HttpServletResponse res, JSONObject jo) {
10 months ago
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Cache-Control", "no-cache");
res.setCharacterEncoding("UTF-8");
res.setContentType("application/json");
try {
res.getWriter().println(jo);
res.getWriter().flush();
} catch (IOException e) {
throw new RuntimeException(e);
}
10 months ago
}
10 months ago
10 months ago
public void renderJson(HttpServletResponse res, String msg) {
renderJson(res, new JSONObject().put("msg", msg));
}
@Override
public void handle(String target, HttpServletRequest req, HttpServletResponse res, boolean[] isHandled) {
//可以正确获取到URL的完整路径
String servletPath = req.getServletPath();
JSONObject jo = new JSONObject();
Map<String, Object> _map = new HashMap<>();
_map.put("servletPath", servletPath);
_map.put("success", true);
jo.putAll(_map);
if (req.getMethod().equals("GET")) {
renderJson(res, "现在调用是GET");
} else if (req.getMethod().equals("POST")) {
renderJson(res, "现在调用是POST");
} else if (req.getContentType() != null && req.getContentType().contains("multipart/form-data")) {
renderJson(res, "现在调用是上传操作!");
} else {
renderJson(res, "系统只支持GET,POST和文件上传其它的OPTIONS不允许");
}
//输出结果
renderJson(res, jo);
//停止filter
10 months ago
isHandled[0] = true;
}
}