管道基础大数据平台系统开发-【后端】-Server
1.5
13693261870
2023-01-05 e5f61db766b3eaa28506835e3581cbd2fdc8533d
src/main/java/com/lf/server/service/show/MarkService.java
@@ -1,27 +1,22 @@
package com.lf.server.service.show;
import com.lf.server.entity.all.HttpStatus;
import com.lf.server.entity.ctrl.MarkJsonEntity;
import com.lf.server.entity.data.DownloadEntity;
import com.lf.server.entity.data.MetaEntity;
import com.lf.server.entity.show.MarkEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.*;
import com.lf.server.mapper.show.MarkMapper;
import com.lf.server.service.data.UploaderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.lf.server.service.data.DownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
/**
 * 标绘
@@ -34,6 +29,9 @@
    @Autowired
    PathHelper pathHelper;
    @Autowired
    DownloadService downloadService;
    @Override
    public Integer selectCount(Integer uid) {
@@ -85,32 +83,58 @@
        return markMapper.updates(list);
    }
    public void downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception {
        String parent = pathHelper.getTempPath(ue.getId());
    /**
     * 下载ShapeFile文件
     *
     * @param ue   用户实体
     * @param list 标绘JSON实体类集合
     * @param req  请求
     * @param res  响应
     * @return GUID
     * @throws Exception 异常
     */
    public String downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception {
        String parent = pathHelper.getTempPath();
        String path = createShapeFiles(ue, list, parent);
        File[] files = new File(path).listFiles();
        if (files == null || files.length == 0) {
            return null;
        }
        String zip = getZip();
        ZipHelper.zip(zip, parent);
        FileHelper.deleteDir(parent);
        String guid = FileHelper.getFileMd5(zip);
        DownloadEntity entity = downloadService.selectByGuid(guid);
        if (entity != null) {
            return entity.getGuid();
        }
        DownloadEntity de = getDownloadEntity(ue, zip);
        int rows = downloadService.insert(de);
        return rows > 0 ? de.getGuid() : null;
    }
    private String createShapeFiles(UserEntity ue, List<MarkJsonEntity> list, String parent) {
        String path = getShpDir(ue, parent);
        List<MarkJsonEntity> points = getMarkByType(list, "POINT");
        if (points.size() > 0) {
            String pointFile = GdalHelper.createShp(points, path, "POINT");
            String pointFile = ShpHelper.createShp(points, path, "POINT");
        }
        List<MarkJsonEntity> lines = getMarkByType(list, "LINESTRING");
        if (lines.size() > 0) {
            String lineFile = GdalHelper.createShp(lines, path, "LINESTRING");
            String lineFile = ShpHelper.createShp(lines, path, "LINESTRING");
        }
        List<MarkJsonEntity> polygons = getMarkByType(list, "POLYGON");
        if (polygons.size() > 0) {
            String polygonFile = GdalHelper.createShp(polygons, path, "POLYGON");
            String polygonFile = ShpHelper.createShp(polygons, path, "POLYGON");
        }
        File[] files = new File(path).listFiles();
        if (files == null || files.length == 0) {
            WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.BAD_REQUEST, "Shp文件生成失败"));
            return;
        }
        String zip = getZip(parent);
        ZipHelper.zip(zip, parent);
        download(res, zip);
        return path;
    }
    private String getShpDir(UserEntity ue, String parent) {
@@ -138,8 +162,8 @@
        return rs;
    }
    private String getZip(String parent) {
        String path = parent + File.separator + WebHelper.getRandomInt(100000, 1000000) + ".zip";
    private String getZip() {
        String path = pathHelper.getDownloadFullPath() + File.separator + StringHelper.YMDHMS2_FORMAT.format(new Date()) + ".zip";
        File file = new File(path);
        if (file.exists() && !file.isDirectory()) {
@@ -149,34 +173,54 @@
        return path;
    }
    private void download(HttpServletResponse res, String file) throws Exception {
        String fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8");
    private DownloadEntity getDownloadEntity(UserEntity ue, String file) throws Exception {
        DownloadEntity de = new DownloadEntity();
        de.setName(FileHelper.getFileName(file));
        de.setType(1);
        de.setSizes(FileHelper.sizeToMb(new File(file).length()));
        de.setDepid(ue.getDepid());
        de.setDcount(0);
        // de.setPwd(null)
        de.setUrl(FileHelper.getRelativePath(file));
        de.setDescr("ShapeFile文件");
        de.setGuid(FileHelper.getFileMd5(file));
        de.setCreateUser(ue.getId());
        // de.setGeom(null)
        // 设置响应头中文件的下载方式为附件方式,以及设置文件名
        res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        // 设置响应头的编码格式为UTF-8
        res.setCharacterEncoding("UTF-8");
        return de;
    }
        // 通过response对象设置响应数据格式(如:"text/plain; charset=utf-8")
        String ext = FileHelper.getExtension(file);
        String mime = FileHelper.getMime(ext);
        res.setContentType(mime);
    /**
     * 获取下载文件路径
     *
     * @param de 下载实体类
     * @return 下载文件路径
     */
    public String getDownloadFilePath(DownloadEntity de) {
        return pathHelper.getConfig().getDownloadPath() + File.separator + de.getUrl();
    }
        // 通过response对象,获取到输出流
        ServletOutputStream outputStream = res.getOutputStream();
        // 定义输入流,通过输入流读取文件内容
        FileInputStream 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();
    /**
     * 读取ShapeFile文件获取Mark实体类
     *
     * @param list ShapeFile文件列表
     * @return Mark实体类集合
     */
    public List<MarkJsonEntity> readShpForMarks(List<MetaEntity> list) {
        String fileName = null;
        for (MetaEntity mf : list) {
            if (mf.getName().toLowerCase().indexOf(".shp") > -1) {
                fileName = mf.getPath();
                break;
            }
        }
        if (StringHelper.isEmpty(fileName)) {
            return null;
        }
        // 关闭资源
        outputStream.close();
        fileInputStream.close();
        List<MarkJsonEntity> mjeList = ShpHelper.readShpForMarks(fileName);
        FileHelper.deleteFiles(list);
        return mjeList;
    }
}