| | |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.ServletRequest; |
| | | import javax.servlet.ServletResponse; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | |
| | | 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); |
| | |
| | | int x = Integer.parseInt(row); |
| | | int y = Integer.parseInt(col); |
| | | |
| | | ServletServerHttpRequest ssReq = new ServletServerHttpRequest(req); |
| | | ServletServerHttpResponse ssRes = new ServletServerHttpResponse(res); |
| | | |
| | | // 检查缓存是否过期 |
| | | if (checkIfNotModify(ssReq, ssRes)) { |
| | | // 设置缓存头 |
| | | setBrowerCache(ssRes); |
| | | return; |
| | | } |
| | | |
| | | // 设置返回图片类型 |
| | | res.setContentType("image/png"); |
| | | // 设置缓存参数 |
| | | setBrowerCache(ssRes); |
| | | // 通过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(); |
| | | 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) { |