jinpengyong
2024-03-07 36844dfeea0914de1138be9ebdf27c92d745d73a
screen-api/src/main/java/com/moral/api/controller/ChartController.java
@@ -16,6 +16,7 @@
import com.moral.util.WebUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -173,7 +174,8 @@
            return ResultMessage.ok(resultMap);
        }
        if (type.equals("daily")){
            Object time = parameters.get("time");
            Object time = parameters.get("startTime");
            parameters.put("time",time);
            if (ObjectUtils.isEmpty(time)){
                return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(), ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
            }
@@ -251,16 +253,29 @@
        }
        List<Map<String, Object>> resultList = servicesScopeService.honeycombDiagram(Integer.parseInt(serviceScopeId.toString()), Integer.parseInt(distance.toString()), type.toString(), time.toString(), sensor_code.toString());
        //List<Map<String, Object>> resultList = servicesScopeService.honeycombDiagram(6, 40, "monthly", "2022-02", "a34004");
        File file = File.createTempFile("test",".tsv");
        //获取jar包所在目录
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String path = applicationHome.getSource().getParentFile().toString() + "/static/tsv";
        //File file = File.createTempFile("test",".tsv");
        /*if (!new File(path).exists()){
            new File(path).mkdir();
        }*/
        File file = null;
        File dir = new File(path);
        file = File.createTempFile("honeycombDiagram",".tsv",dir);
        //构建输出流,同时指定编码
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
        //tsv文件是空格分隔,除第一个外,每次写入一个单元格数据后需要输入空格
        ow.write("lon");
        ow.write("lng");
        ow.write("  ");
        ow.write("lat");
        ow.write("  ");
        ow.write("value");
        ow.write("count");
        //写完文件头换行
        ow.write("\r\n");
        //写内容
@@ -276,39 +291,18 @@
        ow.flush();
        ow.close();
        //1、设置response 响应头
        response.reset(); //设置页面不缓存,清空buffer
        response.setCharacterEncoding("UTF-8"); //字符编码
        response.setContentType("multipart/form-data"); //二进制传输数据
        //设置响应头
        response.setHeader("Content-Disposition",
                "attachment;fileName="+ URLEncoder.encode(file.getName(), "UTF-8"));
        /*String localAddr = httpServletRequest.getLocalAddr();
        int serverPort = httpServletRequest.getServerPort();*/
        File file1 = new File(file.getParent(),file.getName());
        System.out.println(file1.getParent());
        //2、 读取文件--输入流
        InputStream input=new FileInputStream(file1);
        //3、 写出文件--输出流
        OutputStream out = response.getOutputStream();
        Map<String,Object> res = new HashMap<>();
        res.put("path","http://47.99.64.149:8081"+"/static/tsv/"+file.getName());
        byte[] buff =new byte[1024];
        int index=0;
        //4、执行 写出操作
        while((index= input.read(buff))!= -1){
            out.write(buff, 0, index);
            out.flush();
        }
        out.close();
        input.close();
        file.delete();
        return ResultMessage.ok();
        return ResultMessage.ok(res);
    }
    @RequestMapping(value = "returnDataTest", method = RequestMethod.GET)
    @ResponseBody
    public void returnDataTest(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
    public ResultMessage returnDataTest(HttpServletRequest request, HttpServletResponse response) throws ParseException, IOException {
        Map<String, Object> params = WebUtils.getParametersStartingWith(request, null);
        Object serviceScopeId = params.get("serviceScopeId");
        Object sensor_code = params.get("sensor_code");
@@ -320,7 +314,19 @@
        }*/
        List<Map<String, Object>> resultList = servicesScopeService.honeycombDiagram(Integer.parseInt(serviceScopeId.toString()), Integer.parseInt(distance.toString()), type.toString(), time.toString(), sensor_code.toString());
        //List<Map<String, Object>> resultList = servicesScopeService.honeycombDiagram(6, 40, "monthly", "2022-02", "a34004");
        File file = File.createTempFile("test",".tsv");
        //获取jar包所在目录
        ApplicationHome applicationHome = new ApplicationHome(getClass());
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String path = applicationHome.getSource().getParentFile().toString() + "/static/tsv";
        //File file = File.createTempFile("test",".tsv");
        if (!new File(path).exists()){
            new File(path).mkdir();
        }
        File file = null;
        File dir = new File(path);
        file = File.createTempFile("honeycombDiagram",".tsv",dir);
        //构建输出流,同时指定编码
        OutputStreamWriter ow = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
@@ -375,7 +381,7 @@
        }
        bis.close();*/
        //1、设置response 响应头
        /*//1、设置response 响应头
        response.reset(); //设置页面不缓存,清空buffer
        response.setCharacterEncoding("UTF-8"); //字符编码
        response.setContentType("multipart/form-data"); //二进制传输数据
@@ -398,10 +404,11 @@
            out.flush();
        }
        out.close();
        input.close();
        input.close();*/
        file.delete();
        //return ResultMessage.ok();
        Map<String,Object> res = new HashMap<>();
        res.put("path",file.getPath());
        return ResultMessage.ok(res);
    }
}