| | |
| | | }) |
| | | @ResponseBody |
| | | @PostMapping(value = "/downloadShp", produces = "application/json; charset=UTF-8") |
| | | public void downloadShp(@RequestBody List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) { |
| | | public ResponseMsg<String> downloadShp(@RequestBody List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | UserEntity ue = tokenService.getCurrentUser(req); |
| | | if (ue == null) { |
| | | WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.UNAUTHORIZED, "用户未登录")); |
| | | return fail("用户未登录", null); |
| | | } |
| | | if (list == null || list.isEmpty()) { |
| | | WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.BAD_REQUEST, "找不到标绘实体")); |
| | | return fail("找不到标绘数据", null); |
| | | } |
| | | |
| | | markService.downloadShp(ue, list, req, res); |
| | | String str = markService.downloadShp(ue, list, req, res); |
| | | |
| | | return success(str); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage(), ex); |
| | | return fail(ex.getMessage(), null); |
| | | } |
| | | } |
| | | |
| | | public void downloadFile(String guid) { |
| | | |
| | | } |
| | | } |
| | |
| | | import org.springframework.web.context.request.ServletRequestAttributes; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.Cookie; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.servlet.http.HttpSession; |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.io.PrintWriter; |
| | | import java.net.URLEncoder; |
| | | import java.sql.Timestamp; |
| | | import java.util.Calendar; |
| | | import java.util.Random; |
| | |
| | | public static int getRandomInt(int min, int max) { |
| | | return new Random().nextInt(max) % (max - min + 1) + min; |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param file 文件 |
| | | * @param res 响应 |
| | | * @throws Exception 异常 |
| | | */ |
| | | public static void download(String file, HttpServletResponse res) 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(); |
| | | } |
| | | } |
| | |
| | | } catch (Exception ex) { |
| | | return ctrl.fail(ex.getMessage(), null); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取附件实体类 |
| | |
| | | return; |
| | | } |
| | | |
| | | setDownloadResponse(entity, res); |
| | | String filePath = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); |
| | | |
| | | // 通过response对象,获取到输出流 |
| | | ServletOutputStream outputStream = res.getOutputStream(); |
| | | // 定义输入流,通过输入流读取文件内容 |
| | | FileInputStream fileInputStream = new FileInputStream(filePath); |
| | | |
| | | 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(); |
| | | String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); |
| | | WebHelper.download(file, res); |
| | | } catch (Exception ex) { |
| | | try { |
| | | String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "文件下载出错")); |
| | |
| | | } |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置下载响应信息 |
| | | */ |
| | | private void setDownloadResponse(AttachEntity entity, HttpServletResponse res) throws IOException { |
| | | String fileName = URLEncoder.encode(entity.getName(), "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(entity.getName()); |
| | | String mime = FileHelper.getMime(ext); |
| | | res.setContentType(mime); |
| | | } |
| | | } |
| | |
| | | return markMapper.updates(list); |
| | | } |
| | | |
| | | public void downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | public String downloadShp(UserEntity ue, List<MarkJsonEntity> list, HttpServletRequest req, HttpServletResponse res) throws Exception { |
| | | String parent = pathHelper.getTempPath(ue.getId()); |
| | | String path = getShpDir(ue, parent); |
| | | |
| | |
| | | |
| | | File[] files = new File(path).listFiles(); |
| | | if (files == null || files.length == 0) { |
| | | WebHelper.write2Page(res, WebHelper.getErrJson(HttpStatus.BAD_REQUEST, "Shp文件生成失败")); |
| | | return; |
| | | return "Shp文件生成失败"; |
| | | } |
| | | |
| | | String zip = getZip(parent); |
| | | ZipHelper.zip(zip, parent); |
| | | download(res, zip); |
| | | //download(res, zip); |
| | | |
| | | return ""; |
| | | } |
| | | |
| | | private String getShpDir(UserEntity ue, String parent) { |
| | |
| | | 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(); |
| | | } |
| | | } |