| | |
| | | package com.se.simu.controller; |
| | | |
| | | import com.se.simu.domain.Layer; |
| | | import com.se.simu.helper.WebHelper; |
| | | import com.se.simu.service.WaterService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | |
| | | /** |
| | | * 内涝控制器 |
| | |
| | | return; |
| | | } |
| | | |
| | | Layer layer = waterService.getLayer(serviceName); |
| | | byte[] bytes = waterService.getLayerJson(serviceName); |
| | | |
| | | WebHelper.writeJson2Page(res, HttpStatus.OK, layer); |
| | | WebHelper.writeBytes(bytes, res); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); |
| | |
| | | } |
| | | |
| | | String file = waterService.getTerraMap(serviceName, width, height); |
| | | writeFile(file, res); |
| | | |
| | | WebHelper.writePng(file, res); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); |
| | |
| | | return; |
| | | } |
| | | |
| | | String file = waterService.getWaterMap(serviceName, width, height); |
| | | writeFile(file, res); |
| | | String file = waterService.getWaterMap(serviceName, width, height, timestamp); |
| | | |
| | | WebHelper.writePng(file, res); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); |
| | |
| | | } |
| | | |
| | | String file = waterService.getFlowMap(serviceName, width, height, timestamp); |
| | | writeFile(file, res); |
| | | |
| | | WebHelper.writePng(file, res); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | WebHelper.writeStr2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage()); |
| | |
| | | if (null == timestamp || timestamp < Y2000) { |
| | | return WebHelper.writeJson2Page(res, HttpStatus.BAD_REQUEST, "时间不能为空且大于2000年"); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 写文件 |
| | | */ |
| | | private boolean writeFile(String path, HttpServletResponse res) throws Exception { |
| | | if (WebHelper.isEmpty(path)) { |
| | | return WebHelper.writeJson2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, "生成PNG失败"); |
| | | } |
| | | File file = new File(path); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | return WebHelper.writeJson2Page(res, HttpStatus.INTERNAL_SERVER_ERROR, "PNG文件找不到"); |
| | | } |
| | | |
| | | WebHelper.download(path, file.getName(), res); |
| | | |
| | | return true; |
| | | } |
| | |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.FileInputStream; |
| | | import java.io.PrintWriter; |
| | | import java.io.*; |
| | | import java.net.InetAddress; |
| | | import java.net.URLEncoder; |
| | | import java.net.UnknownHostException; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 输出字节数组 |
| | | */ |
| | | public static void writeBytes(byte[] bytes, HttpServletResponse res) throws IOException { |
| | | res.setContentType("application/octet-stream"); |
| | | |
| | | if (null == bytes) { |
| | | res.setStatus(HttpStatus.NOT_FOUND.value()); |
| | | return; |
| | | } |
| | | |
| | | OutputStream os = res.getOutputStream(); |
| | | os.write(bytes, 0, bytes.length); |
| | | os.close(); |
| | | } |
| | | |
| | | /** |
| | | * 输出Png文件 |
| | | */ |
| | | public static void writePng(String filePath, HttpServletResponse res) throws IOException { |
| | | File file = new File(filePath); |
| | | if (!file.exists() || file.isDirectory()) { |
| | | res.setStatus(HttpStatus.NOT_FOUND.value()); |
| | | return; |
| | | } |
| | | |
| | | |
| | | String fileName = URLEncoder.encode(filePath, "UTF-8").replace("+", "%20"); |
| | | res.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName); |
| | | res.setCharacterEncoding("UTF-8"); |
| | | res.setContentType("image/png"); |
| | | |
| | | writeFile(filePath, res); |
| | | } |
| | | |
| | | /** |
| | | * 获取随机整数 |
| | | */ |
| | | public static int getRandomInt(int min, int max) { |
| | |
| | | String mime = getMime(ext); |
| | | res.setContentType(mime); |
| | | |
| | | writeFile(file, res); |
| | | } |
| | | |
| | | /** |
| | | * 写文件 |
| | | */ |
| | | private static void writeFile(String file, HttpServletResponse res) throws IOException { |
| | | // 通过response对象,获取到输出流 |
| | | ServletOutputStream outputStream = res.getOutputStream(); |
| | | // 定义输入流,通过输入流读取文件内容 |
| | |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | |
| | | |
| | | @Value("${sys.path.data}") |
| | | String dataPath; |
| | | |
| | | /** |
| | | * 获取元数据信息 |
| | | */ |
| | | public byte[] getLayerJson(String serviceName) { |
| | | try { |
| | | String filePath = dataPath + File.separator + serviceName + File.separator + "layer.json"; |
| | | |
| | | File dat = new File(filePath); |
| | | if (!dat.exists()) { |
| | | return null; |
| | | } |
| | | |
| | | byte[] bytes = new byte[(int) dat.length()]; |
| | | |
| | | FileInputStream fs = new FileInputStream(filePath); |
| | | fs.read(bytes); |
| | | fs.close(); |
| | | |
| | | return bytes; |
| | | } catch (Exception ex) { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取地形高度图 |
| | | */ |
| | | public String getTerraMap(String serviceName, Integer width, Integer height) { |
| | | return dataPath + File.separator + serviceName + File.separator + "terrain" + File.separator + width + "_" + height + ".png"; |
| | | } |
| | | |
| | | /** |
| | | * 获取水面高度图 |
| | | */ |
| | | public String getWaterMap(String serviceName, Integer width, Integer height, Long timestamp) { |
| | | return dataPath + File.separator + serviceName + File.separator + "waters" + File.separator + timestamp+ File.separator + width + "_" + height + ".png"; |
| | | } |
| | | |
| | | /** |
| | | * 获取水流向流速图 |
| | | */ |
| | | public String getFlowMap(String serviceName, Integer width, Integer height, Long timestamp) { |
| | | return dataPath + File.separator + serviceName + File.separator + "flow" + File.separator + timestamp+ File.separator + width + "_" + height + ".png"; |
| | | } |
| | | |
| | | public Layer getLayer(String serviceName) { |
| | | Layer layer = new Layer(); |
| | |
| | | layer.setWaters(new Water(data)); |
| | | |
| | | return layer; |
| | | } |
| | | |
| | | public String getTerraMap(String serviceName, Integer width, Integer height) { |
| | | return "D:/simu/test/geo.png"; |
| | | } |
| | | |
| | | public String getWaterMap(String serviceName, Integer width, Integer height) { |
| | | return null; |
| | | } |
| | | |
| | | public String getFlowMap(String serviceName, Integer width, Integer height, Long timestamp) { |
| | | //com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; |
| | | |
| | | |
| | | |
| | | return null; |
| | | } |
| | | } |
| | |
| | | sys: |
| | | ver: 0.1 |
| | | path: |
| | | data: D:/simu/ |
| | | data: D:\simu |