3
13693261870
2024-07-18 893998916eab354d4002fbcdcb0a242a36a49684
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;
    }