From 5fe2473ba0e9f374da27e919fdce09b0915f5e51 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 31 十月 2022 20:21:56 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/helper/PathHelper.java | 12 ++-- src/main/java/com/lf/server/service/show/MarkService.java | 70 ++++++++++++++++++++--- src/main/java/com/lf/server/helper/FileHelper.java | 14 ++++ 说明.txt | 17 +++-- src/main/java/com/lf/server/config/PropertiesConfig.java | 12 ++-- src/main/resources/application.yml | 12 ++-- 6 files changed, 104 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/lf/server/config/PropertiesConfig.java b/src/main/java/com/lf/server/config/PropertiesConfig.java index ad0ae83..9c20888 100644 --- a/src/main/java/com/lf/server/config/PropertiesConfig.java +++ b/src/main/java/com/lf/server/config/PropertiesConfig.java @@ -9,8 +9,8 @@ */ @Configuration public class PropertiesConfig { - @Value("${sys.path.import}") - private String importPath; + @Value("${sys.path.download}") + private String downloadPath; @Value("${sys.path.export}") private String exportPath; @@ -46,12 +46,12 @@ this.druidPwd = druidPwd; } - public String getImportPath() { - return importPath; + public String getDownloadPath() { + return downloadPath; } - public void setImportPath(String importPath) { - this.importPath = importPath; + public void setDownloadPath(String downloadPath) { + this.downloadPath = downloadPath; } public String getExportPath() { diff --git a/src/main/java/com/lf/server/helper/FileHelper.java b/src/main/java/com/lf/server/helper/FileHelper.java index ed33f93..07b1cf7 100644 --- a/src/main/java/com/lf/server/helper/FileHelper.java +++ b/src/main/java/com/lf/server/helper/FileHelper.java @@ -26,6 +26,20 @@ private final static Log log = LogFactory.getLog(FileHelper.class); /** + * 鑾峰彇鏂囦欢鍚� + * @param file + * @return + */ + public static String getFileName(String file) { + int idx = file.lastIndexOf(File.separator); + if (idx > -1) { + return file.substring(idx + 1); + } + + return ""; + } + + /** * 鑾峰彇鏂囦欢鎵╁睍鍚� */ public static String getExtension(File file) { diff --git a/src/main/java/com/lf/server/helper/PathHelper.java b/src/main/java/com/lf/server/helper/PathHelper.java index edf6839..c1cf9e0 100644 --- a/src/main/java/com/lf/server/helper/PathHelper.java +++ b/src/main/java/com/lf/server/helper/PathHelper.java @@ -19,7 +19,7 @@ @Autowired private PropertiesConfig config; - private static int importPath = 1; + private static int downloadPath = 1; private static int exportPath = 1; @@ -35,8 +35,8 @@ return config; } - public static int getImportPath() { - return importPath; + public static int getDownloadPath() { + return downloadPath; } public static int getExportPath() { @@ -55,7 +55,7 @@ * 鍒濆鍖� */ public void init() { - importPath = getSubPath(config.getImportPath(), importPath); + downloadPath = getSubPath(config.getDownloadPath(), downloadPath); exportPath = getSubPath(config.getExportPath(), exportPath); uploadPath = getSubPath(config.getUploadPath(), uploadPath); sharePath = getSubPath(config.getSharePath(), sharePath); @@ -84,9 +84,9 @@ * 鑾峰彇瀵煎叆鐩綍 */ public String getImportFullPath() { - importPath = getSubPath(config.getImportPath(), importPath); + downloadPath = getSubPath(config.getDownloadPath(), downloadPath); - return config.getImportPath() + File.separator + importPath; + return config.getDownloadPath() + File.separator + downloadPath; } /** diff --git a/src/main/java/com/lf/server/service/show/MarkService.java b/src/main/java/com/lf/server/service/show/MarkService.java index d54f484..1472688 100644 --- a/src/main/java/com/lf/server/service/show/MarkService.java +++ b/src/main/java/com/lf/server/service/show/MarkService.java @@ -1,12 +1,10 @@ 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; @@ -15,9 +13,12 @@ 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; @@ -84,8 +85,9 @@ 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) { @@ -100,11 +102,19 @@ 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()) { @@ -127,4 +137,46 @@ 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); + // 璁剧疆鍝嶅簲澶寸殑缂栫爜鏍煎紡涓篣TF-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(); + } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 906290e..55aa0dc 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -140,13 +140,13 @@ # 绯荤粺閰嶇疆 sys: path: - # 瀵煎叆鐩綍 - import: D:\LF\import - # 鍑哄浘鐩綍 + # 涓嬭浇鐩綍锛堜笅杞芥枃浠讹級 + download: D:\LF\download + # 鍑哄浘鐩綍锛堜笓棰樺浘鍑哄浘锛� export: D:\LF\export - # 涓婁紶鐩綍 + # 涓婁紶鐩綍锛堝ぇ鏁版嵁涓婁紶锛� upload: D:\LF\upload - # 鍏变韩鐩綍 + # 鍏变韩鐩綍锛堟暟鎹彂甯冿級 share: D:\LF\share - # 涓存椂鐩綍 + # 涓存椂鐩綍锛堜复鏃朵笂浼犳枃浠讹級 temp: D:\LF\temp diff --git "a/\350\257\264\346\230\216.txt" "b/\350\257\264\346\230\216.txt" index 7c9c51e..6f0d7ac 100644 --- "a/\350\257\264\346\230\216.txt" +++ "b/\350\257\264\346\230\216.txt" @@ -24,19 +24,24 @@ 23.淇敼閽诲瓟鏁版嵁涓嬭浇宸ュ叿锛屾洿鏂版柊澧炲瓧娈电殑灞炴�� 24.韬唤璁よ瘉鎷︽埅鍣ㄦ坊鍔犻粦鐧藉悕鍗曟鏌ュ姛鑳� 25.寮�鍙戣嚜鍔ㄧ敓鎴怣ybtis Plus浠g爜灏忓伐鍏� -26.鎵归噺鐢熸垚鍩虹鏁版嵁琛ㄧ殑瀹炰綋绫诲拰Mapper绫� < -27. -28. +26.鎵归噺鐢熸垚鍩虹鏁版嵁琛ㄧ殑瀹炰綋绫诲拰Mapper绫� +27.淇敼鎵�鏈夌殑鎵归噺鏂板銆佸垹闄ゃ�佷慨鏀规帴鍙� +28.寮�鍙戝ぇ鏂囦欢涓婁紶鎺ュ彛锛坈ommons-fileupload锛� < 29. 30. ----------------------------------------------- -.娣诲姞鎵归噺淇敼鐢ㄦ埛瀵嗙爜鎺ュ彛 -.淇敼鎵�鏈夌殑鎵归噺鏂板銆佸垹闄ゃ�佷慨鏀规帴鍙� -.寮�鍙戝ぇ鏂囦欢涓婁紶鎺ュ彛锛坈ommons-fileupload锛� +.淇敼鐢ㄦ埛瀵嗙爜鎺ュ彛 .GDAL璇诲彇shp鍚庡垹闄や笂浼犳枃浠� .瀵圭敤鎴枫�佽彍鍗曘�佽鑹层�佽祫婧愯繘琛屾巿鏉� .娣诲姞鏌ヨ瑙掕壊銆佽彍鍗曘�佽祫婧愭潈闄愭帴鍙� +.寮�鍙戞牴鎹疄浣撳悕绉板姩鎬佹煡璇㈣〃鐨勮褰曟暟 +.寮�鍙戞牴鎹疄浣撳悕绉拌繘琛屽姩鎬佸垎椤垫煡璇� +.寮�鍙戞牴鎹疄浣撳悕绉版煡璇㈢┖闂磋〃鐨刉KT璁板綍 +.寮�鍙戞牴鎹疄浣撳悕绉�+瀛楁+鍊兼ā绯婃悳绱㈠墠10鏉¤褰� +.寮�鍙戞牴鎹悕绉版ā绯婃悳绱㈢敤鎴枫�佸崟浣嶇殑鍓�10鏉¤褰� +.寮�鍙戞牴鎹爣缁楯SON鏁版嵁鐢熸垚shp鏂囦欢骞朵笅杞� +.寮�鍙戜笂浼爏hp鏂囦欢杞崲涓篔SON鏁版嵁鎺ュ彛 ---------------------------------------------------------------------------------------------------- java 璋冪敤gdal璇诲彇gdb鏁版嵁锛宭inux涓媕ava璋冪敤gdal锛� -- Gitblit v1.9.3