package com.onemap.file.controller; import com.onemap.common.core.domain.UploadZipDTO; import com.onemap.common.core.web.domain.RequestResult; import com.onemap.file.domain.TUploadGeomDTO; import com.onemap.file.service.ISpaceFileRecordService; import com.onemap.file.service.SaveFileService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.AntPathMatcher; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import com.onemap.common.core.domain.R; import com.onemap.common.core.utils.file.FileUtils; import com.onemap.file.service.ISysFileService; import com.onemap.system.api.domain.SysFile; import org.springframework.web.servlet.HandlerMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; /** * 文件请求处理 * * @author onemap */ @RestController public class SysFileController { private static final Logger log = LoggerFactory.getLogger(SysFileController.class); @Autowired private ISysFileService sysFileService; @Autowired private ISpaceFileRecordService spaceFileRecordService; @Autowired private SaveFileService saveFileService; private AntPathMatcher antPathMatcher = new AntPathMatcher(); /** * 文件上传 * * @param file 文件 * @param businessType 业务类型 */ @PostMapping("/upload") public R upload(MultipartFile file, String businessType) { try { // 上传并返回访问地址 String url = sysFileService.uploadFile(file, businessType); SysFile sysFile = new SysFile(); sysFile.setName(FileUtils.getName(url)); sysFile.setUrl(url); return R.ok(sysFile); } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } } /** * 文件上传(大文件分片上传) * * @param file * @param chunk * @param chunks * @param name * @param md5 * @return */ @PostMapping("/splitFileUpload") public R splitFileUpload(@RequestParam("file") MultipartFile file, @RequestParam("chunk") int chunk, @RequestParam("chunks") int chunks, @RequestParam("name") String name, @RequestParam("md5") String md5) { try { // 上传并返回访问地址 String url = sysFileService.splitFileUpload(file, chunk, chunks, name, md5); SysFile sysFile = new SysFile(); sysFile.setName(FileUtils.getName(url)); sysFile.setUrl(url); return R.ok(sysFile); } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } } /** * 上传附件,图片 * * @param files 文件列表 * @return 文件地址集合 */ @PostMapping("/uploadAppendix") public R> uploadAppendix(MultipartFile[] files) { try { List sysFileList = new ArrayList<>(); // 上传并返回访问地址 for (MultipartFile file : files) { String url = sysFileService.uploadAppendix(file); SysFile sysFile = new SysFile(); sysFile.setName(FileUtils.getName(url)); sysFile.setUrl(url); sysFileList.add(sysFile); } return R.ok(sysFileList); } catch (Exception e) { log.error("上传文件失败", e); return R.fail(e.getMessage()); } } /** * shp压缩包文件上传 * * @param file 文件 */ @PostMapping("/saveGeom") public RequestResult saveGeom(MultipartFile file, Integer fromType, String geom, String fromRoute) { try { RequestResult res = null; if (fromType == 1) { String id = saveFileService.saveDraw(geom, fromRoute); res = RequestResult.success("上传成功", saveFileService.getById(id)); } else { res = sysFileService.uploadShpZip(file, fromRoute); } return res; } catch (Exception e) { e.printStackTrace(); return RequestResult.error("上传失败!", null); } } // /** // * shp压缩包文件上传****智慧选址 // * // * @param file 文件 // */ // @PostMapping("/uploadZhxzZip") // public RequestResult uploadZhxzZip(MultipartFile file) { // try { // RequestResult res = sysFileService.uploadZhxzZip(file); // return res; // } catch (Exception e) { // e.printStackTrace(); // return RequestResult.error("上传失败!", null); // } // } /** * 压缩包文件上传 * * @param file 文件 */ @PostMapping("/uploadZip") public UploadZipDTO uploadZip(MultipartFile file) { try { UploadZipDTO res = sysFileService.uploadZip(file); return res; } catch (Exception e) { e.printStackTrace(); return new UploadZipDTO(); } } // /** // * shp压缩包文件上传 // * // * @param file 文件 // */ // @PostMapping("/uploadTgcl") // public GhtgCl uploadTgcl(MultipartFile file, Integer type) { // try { // GhtgCl res = sysFileService.uploadTgcl(file, type); // return res; // } catch (Exception e) { // e.printStackTrace(); // return null; // } // } /** * 获取shp文件相应的geojson */ @GetMapping("/getShpGeoJson") public String getShpGeoJson(String shpfile) { try { String res = sysFileService.getShpGeoJson(shpfile); return res; } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根空间相关的上传出来 * 现支持shp的zip包,ewkt字符串 * * @param file shp的zip文件 * @param fromType 上传的类型 1 ewkt 2 shp的zip文件 * @param geom wkt字符串(如果没设置srid,默认为4326) * @param fromRoute 来源 */ @PostMapping("/upload/geom") public RequestResult uploadGeom(MultipartFile file, Integer fromType, String geom, String fromRoute) { try { RequestResult res = null; if (fromType == 1) { res = spaceFileRecordService.uploadAddShpDraw(geom, fromRoute); } else { res = spaceFileRecordService.uploadAddShpFile(file, fromRoute); } return res; } catch (Exception e) { e.printStackTrace(); return RequestResult.error("上传失败!", null); } } @GetMapping("/upload/geom/query") public RequestResult uploadGeomQuery(String id, Integer isDetail) { return RequestResult.success("执行成功!", spaceFileRecordService.querySpaceFileRecord(id, isDetail)); } /** * @param storeStats 是否入库 0入库 其他不入库 * @return * @throws Exception */ @PostMapping("/upload/geom/v1") public RequestResult uploadGeomV1(MultipartFile file, Integer fromType, String geom, String fromRoute, String storeStats, Integer followup) { try { String id; if (fromType == 1) { id = spaceFileRecordService.uploadAddShpDrawV1(geom, fromRoute, fromType, storeStats); } else { id = spaceFileRecordService.uploadAddShpFileV1(file, fromRoute, fromType, storeStats); } TUploadGeomDTO dto = spaceFileRecordService.selectTUploadGeomById(id, 0); if (null != followup) { spaceFileRecordService.handleUploadGeomFollowUp(id, followup); } if (!"0".equals(storeStats)) { spaceFileRecordService.deleteTUploadGeomById(id); } return RequestResult.success(dto); } catch (Exception e) { return RequestResult.error("上传失败!", null); } } /** * 根据ID查询空间信息, * * @param id * @param isDetail 是否查询详情,1是 其他否 * @return * @throws Exception */ @GetMapping("/upload/geom/v1/query") public RequestResult uploadGeomQueryV1(String id, Integer isDetail) { return RequestResult.success("执行成功!", spaceFileRecordService.selectTUploadGeomById(id, isDetail)); } @GetMapping("/upload/geom/v1/query/details") public RequestResult uploadGeomQueryV1(String id) { return RequestResult.success("执行成功!", spaceFileRecordService.selectTUploadGeomDetailsById(id)); } /** * 文件预览 * * @param request * @param response * @return */ @GetMapping("/upload/inline/**") public ResponseEntity uploadInline(HttpServletRequest request, HttpServletResponse response) { try { String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString(); String bestMatchingPattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(); // 提取与模式相匹配的部分(不包括通配符) String extractedPath = antPathMatcher.extractPathWithinPattern(bestMatchingPattern, path); String uploadFile = spaceFileRecordService.uploadAbsolutePath(URLDecoder.decode(extractedPath, "UTF-8")); // 构建文件的完整路径 Path filePath = Paths.get(uploadFile); Resource resource = new FileSystemResource(filePath.toFile()); // 如果资源不存在,返回404 if (!resource.exists()) { return ResponseEntity.notFound().build(); } // 获取文件MIME类型 String contentType = Files.probeContentType(filePath); if (contentType == null) { // 如果无法确定MIME类型,则使用默认值 contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE; } // 设置HTTP头部信息 HttpHeaders headers = new HttpHeaders(); headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + URLEncoder.encode(resource.getFilename(), "UTF-8") + "\""); // headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\""); headers.setContentType(MediaType.parseMediaType(contentType)); headers.setAccessControlAllowOrigin("*"); // 返回文件资源 return ResponseEntity.ok().headers(headers).contentLength(Files.size(filePath)) // 可选:设置内容长度 .body(resource); } catch (IOException e) { // 处理IO异常,例如文件不存在或无法读取 // e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } /** * 文件下载 * * @param request * @param response * @return */ @GetMapping("/upload/attachment/**") public ResponseEntity uploadAttachment(HttpServletRequest request, HttpServletResponse response) { try { String path = request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString(); String bestMatchingPattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(); // 提取与模式相匹配的部分(不包括通配符) String extractedPath = antPathMatcher.extractPathWithinPattern(bestMatchingPattern, path); String uploadFile = spaceFileRecordService.uploadAbsolutePath(URLDecoder.decode(extractedPath, "UTF-8")); // 构建文件的完整路径 Path filePath = Paths.get(uploadFile); Resource resource = new FileSystemResource(filePath.toFile()); // 如果资源不存在,返回404 if (!resource.exists()) { return ResponseEntity.notFound().build(); } // 获取文件MIME类型 String contentType = Files.probeContentType(filePath); if (contentType == null) { // 如果无法确定MIME类型,则使用默认值 contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE; } // 设置HTTP头部信息 HttpHeaders headers = new HttpHeaders(); // headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + URLEncoder.encode(resource.getFilename(), "UTF-8") + "\""); headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + URLEncoder.encode(resource.getFilename() + "\"")); headers.setContentType(MediaType.parseMediaType(contentType)); headers.setAccessControlAllowOrigin("*"); // 返回文件资源 return ResponseEntity.ok().headers(headers).contentLength(Files.size(filePath)) // 可选:设置内容长度 .body(resource); } catch (IOException e) { // 处理IO异常,例如文件不存在或无法读取 // e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } }