| | |
| | | |
| | | import com.lf.server.annotation.SysLog; |
| | | import com.lf.server.config.PropertiesConfig; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.helper.WebHelper; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiImplicitParam; |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.core.io.ClassPathResource; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.server.ServerHttpRequest; |
| | | import org.springframework.http.server.ServerHttpResponse; |
| | | import org.springframework.http.server.ServletServerHttpRequest; |
| | | import org.springframework.http.server.ServletServerHttpResponse; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.io.InputStream; |
| | | import java.io.OutputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.time.Duration; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * WMTS服务 |
| | |
| | | stream.read(data); |
| | | stream.close(); |
| | | |
| | | String url = req.getRequestURL().toString().replace("WMTSCapabilities.xml", "tile"); |
| | | String url = req.getRequestURL().toString().replace("WMTSCapabilities.xml", "tile?"); |
| | | |
| | | String result = new String(data, StandardCharsets.UTF_8); |
| | | result = result.replace("{url}", url); |
| | |
| | | @ApiOperation(value = "获取WMTS瓦片") |
| | | @ApiImplicitParams({ |
| | | @ApiImplicitParam(name = "token", value = "令牌", required = true, dataType = "String", defaultValue = "token", paramType = "path"), |
| | | @ApiImplicitParam(name = "layer", value = "图层类型", required = true, dataType = "String", defaultValue = "TDTIMG"), |
| | | @ApiImplicitParam(name = "layer", value = "图层类型", required = true, dataType = "String", defaultValue = "img"), |
| | | @ApiImplicitParam(name = "tilematrix", value = "层级", required = true, dataType = "Integer"), |
| | | @ApiImplicitParam(name = "tilerow", value = "行号", required = true, dataType = "Integer"), |
| | | @ApiImplicitParam(name = "tilecol", value = "列号", required = true, dataType = "Integer") |
| | | }) |
| | | @GetMapping("select/{token}/tile") |
| | | public void selectWmtsTile(@RequestParam("layer") String layer, @RequestParam("tilematrix") Integer l, @RequestParam("tilerow") Integer r, |
| | | @RequestParam("tilecol") Integer c, @RequestParam("format") String format, @PathVariable(name = "token") String token, |
| | | HttpServletRequest req, HttpServletResponse res) { |
| | | public void selectWmtsTile(@PathVariable(name = "token") String token, HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | // ServletServerHttpRequest ssRequest = new ServletServerHttpRequest(request) |
| | | String base = config.getTilePath(); |
| | | String layer = WebHelper.getReqParamVal(req, "layer"); |
| | | String matrix = WebHelper.getReqParamVal(req, "tilematrix"); |
| | | String row = WebHelper.getReqParamVal(req, "tilerow"); |
| | | String col = WebHelper.getReqParamVal(req, "tilecol"); |
| | | if (StringHelper.isEmpty(layer) || StringHelper.isEmpty(matrix) || StringHelper.isEmpty(layer) || StringHelper.isEmpty(row) || StringHelper.isEmpty(col)) { |
| | | return; |
| | | } |
| | | |
| | | int z = Integer.parseInt(matrix); |
| | | int x = Integer.parseInt(row); |
| | | int y = Integer.parseInt(col); |
| | | |
| | | getWmtsTile(layer, z, x, y, req, res); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取WMTS瓦片 |
| | | */ |
| | | private void getWmtsTile(String layer, int z, int x, int y, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | ServletServerHttpRequest ssReq = new ServletServerHttpRequest(req); |
| | | ServletServerHttpResponse ssRes = new ServletServerHttpResponse(res); |
| | | |
| | | // 检查缓存是否过期 |
| | | if (checkIfNotModify(ssReq, ssRes)) { |
| | | // 设置缓存头 |
| | | setBrowerCache(ssRes); |
| | | return; |
| | | } |
| | | |
| | | // 设置缓存参数 |
| | | setBrowerCache(ssRes); |
| | | //res.setContentType("image/png") |
| | | |
| | | // 通过response对象,获取到输出流 |
| | | ServletOutputStream outputStream = res.getOutputStream(); |
| | | // 定义输入流,通过输入流读取文件内容 |
| | | FileInputStream fileInputStream; |
| | | |
| | | // y = (int) Math.pow(2, z) - y - 1; |
| | | String path = config.getTilePath() + File.separator + layer + File.separator + z + File.separator + y + File.separator + x + ".png"; |
| | | System.out.println(path); |
| | | |
| | | File file = new File(path); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | ClassPathResource resource = new ClassPathResource("wmts/nofound.png"); |
| | | fileInputStream = new FileInputStream(resource.getFile()); |
| | | } else { |
| | | System.out.println(path); |
| | | fileInputStream = new FileInputStream(file); |
| | | } |
| | | |
| | | int len = 0; |
| | | byte[] bytes = new byte[1024]; |
| | | while ((len = fileInputStream.read(bytes)) != -1) { |
| | | outputStream.write(bytes, 0, len); |
| | | outputStream.flush(); |
| | | } |
| | | //outputStream.close() |
| | | fileInputStream.close(); |
| | | |
| | | // 设置返回图片类型 |
| | | ssRes.getHeaders().set("Content-Type", "image/png"); |
| | | |
| | | OutputStream os = ssRes.getBody(); |
| | | os.write(bytes); |
| | | os.flush(); |
| | | } |
| | | |
| | | /** |
| | | * 设置浏览器缓存参数 |
| | | */ |
| | | private void setBrowerCache(ServerHttpResponse res) { |
| | | HttpHeaders headers = res.getHeaders(); |
| | | headers.setCacheControl("public, must-revalidate"); |
| | | |
| | | headers.setExpires(System.currentTimeMillis() + 24 * 60 * 60 * 1000); |
| | | headers.setAccessControlMaxAge(Duration.ofHours(24)); |
| | | headers.setETag("\"" + new Date().toString() + "\""); |
| | | // must-revalidate |
| | | // response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); |
| | | } |
| | | |
| | | /** |
| | | * 验证缓存参数 |
| | | */ |
| | | boolean checkIfNotModify(ServerHttpRequest req, ServerHttpResponse res) { |
| | | List<String> etags = req.getHeaders().getIfNoneMatch(); |
| | | if (0 == etags.size() || StringHelper.isEmpty(etags.get(0))) { |
| | | return false; |
| | | } |
| | | |
| | | res.setStatusCode(HttpStatus.NOT_MODIFIED); |
| | | |
| | | return true; |
| | | } |
| | | } |