| | |
| | | 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.show.MarkEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.GdalHelper; |
| | | import com.lf.server.helper.PathHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.helper.WebHelper; |
| | | 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.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.List; |
| | | import java.util.Random; |
| | |
| | | return markMapper.updates(list); |
| | | } |
| | | |
| | | public void downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) { |
| | | String path = getShpDir(ue); |
| | | public void downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | String parent = pathHelper.getTempPath(ue.getId()); |
| | | String path = getShpDir(ue, parent); |
| | | |
| | | List<MarkJsonEntity> points = getMarkByType(list, "POINT"); |
| | | if (points.size() > 0) { |
| | |
| | | String polygonFile = GdalHelper.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); |
| | | } |
| | | |
| | | private String getShpDir(UserEntity ue) { |
| | | String path = pathHelper.getTempPath(ue.getId()) + File.separator + WebHelper.getRandomInt(100000, 1000000); |
| | | private String getShpDir(UserEntity ue, String parent) { |
| | | String path = parent + File.separator + WebHelper.getRandomInt(100000, 1000000); |
| | | |
| | | File file = new File(path); |
| | | if (!file.exists() && !file.isDirectory()) { |
| | |
| | | |
| | | return rs; |
| | | } |
| | | |
| | | private String getZip(String parent) { |
| | | String path = parent + File.separator + WebHelper.getRandomInt(100000, 1000000) + ".zip"; |
| | | |
| | | File file = new File(path); |
| | | if (file.exists() && !file.isDirectory()) { |
| | | file.delete(); |
| | | } |
| | | |
| | | return path; |
| | | } |
| | | |
| | | private void download(HttpServletResponse res, String file) throws Exception { |
| | | String fileName = URLEncoder.encode(FileHelper.getFileName(file), "UTF-8"); |
| | | |
| | | // 设置响应头中文件的下载方式为附件方式,以及设置文件名 |
| | | res.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
| | | // 设置响应头的编码格式为UTF-8 |
| | | res.setCharacterEncoding("UTF-8"); |
| | | |
| | | // 通过response对象设置响应数据格式(如:"text/plain; charset=utf-8") |
| | | String ext = FileHelper.getExtension(file); |
| | | String mime = FileHelper.getMime(ext); |
| | | res.setContentType(mime); |
| | | |
| | | // 通过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(); |
| | | } |
| | | |
| | | // 关闭资源 |
| | | outputStream.close(); |
| | | fileInputStream.close(); |
| | | } |
| | | } |