3
13693261870
2024-07-18 893998916eab354d4002fbcdcb0a242a36a49684
3
已修改4个文件
144 ■■■■■ 文件已修改
src/main/java/com/se/simu/controller/WaterController.java 34 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/WebHelper.java 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/WaterService.java 63 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/WaterController.java
@@ -1,6 +1,5 @@
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;
@@ -12,7 +11,6 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
/**
 * 内涝控制器
@@ -51,9 +49,9 @@
                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());
@@ -71,7 +69,8 @@
            }
            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());
@@ -88,8 +87,9 @@
                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());
@@ -107,7 +107,8 @@
            }
            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());
@@ -142,23 +143,6 @@
        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;
    }
src/main/java/com/se/simu/helper/WebHelper.java
@@ -9,8 +9,7 @@
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;
@@ -189,6 +188,41 @@
    }
    /**
     * 输出字节数组
     */
    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) {
@@ -227,6 +261,13 @@
        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();
        // 定义输入流,通过输入流读取文件内容
src/main/java/com/se/simu/service/WaterService.java
@@ -5,6 +5,8 @@
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;
@@ -23,6 +25,51 @@
    @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();
@@ -43,21 +90,5 @@
        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;
    }
}
src/main/resources/application.yml
@@ -13,4 +13,4 @@
sys:
  ver: 0.1
  path:
    data: D:/simu/
    data: D:\simu